Merge pull request #28 from balaskoa/master
[deliverable/titan.core.git] / compiler2 / functionref.c
CommitLineData
d44e3c4f 1/******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Baranyi, Botond
11 * Beres, Szabolcs
12 * Delic, Adam
13 * Kovacs, Ferenc
14 * Raduly, Csaba
15 * Szabados, Kristof
16 * Szabo, Janos Zoltan – initial implementation
17 *
18 ******************************************************************************/
970ed795
EL
19#include <string.h>
20#include "../common/memory.h"
21#include "functionref.h"
22#include "encdec.h"
23
24#include "main.hh"
25#include "error.h"
26
27void defFunctionrefClass(const funcref_def *fdef, output_struct *output)
28{
29 char *def = NULL, *src = NULL;
30 const char *name = fdef->name;
31 const char *dispname = fdef->dispname;
32
33 const char *return_type, *fat_string;
34
35 if (fdef->return_type != NULL) return_type = fdef->return_type;
36 else return_type = "void";
37
38 switch (fdef->type) {
39 case FUNCTION:
40 fat_string = "function";
41 break;
42 case ALTSTEP:
43 fat_string = "altstep";
44 break;
45 case TESTCASE:
46 fat_string = "testcase";
47 break;
48 default:
49 fat_string = NULL;
50 FATAL_ERROR("defFunctionrefClass(): invalid type");
51 }
52
53 /* class declaration code */
54 output->header.class_decls = mputprintf(output->header.class_decls,
55 "class %s;\n", name);
56
57 /* class definition */
58 def = mputprintf(def,
59#ifndef NDEBUG
60 "// written by %s in " __FILE__ " at %d\n"
61#endif
62 "class %s : public Base_Type {\n"
63 "public:\n"
64#ifndef NDEBUG
65 , __FUNCTION__, __LINE__
66#endif
67 , name);
68
69 switch(fdef->type){
70 case FUNCTION:
71 def = mputstr(def, "typedef ");
72 /* work-around for GCC versions earlier than 3.4:
73 * parse error occurs within the typedef if the return type is the same
74 * as the function type itself */
75 if (!strcmp(name, return_type)) def = mputstr(def, "class ");
76 def = mputprintf(def, "%s (*function_pointer)(%s);\n"
77 "typedef void (*start_pointer)(const COMPONENT& "
78 "component_reference", return_type, fdef->formal_par_list);
79 if (fdef->formal_par_list[0] != '\0') def = mputstr(def, ", ");
80 def = mputprintf(def, "%s);\n", fdef->formal_par_list);
81 break;
82 case ALTSTEP:
83 def = mputprintf(def, "typedef void (*standalone_pointer)(%s);\n"
84 "typedef Default_Base* (*activate_pointer)(%s);\n"
85 "typedef alt_status (*function_pointer)(%s);\n", fdef->formal_par_list,
86 fdef->formal_par_list, fdef->formal_par_list);
87 break;
88 case TESTCASE:
89 def = mputprintf(def, "typedef verdicttype (*function_pointer)(%s);\n",
90 fdef->formal_par_list);
91 break;
92 }
93
94 def = mputprintf(def,
95 "private:\n"
96 "friend class %s_template;\n"
97 "friend boolean operator==(%s::function_pointer value, "
98 "const %s& other_value);\n"
99 "function_pointer referred_function;\n"
100 , name, name, name);
101
102 /* default constructor */
103 def = mputprintf(def,"public:\n"
104 "%s();\n", name);
105 src = mputprintf(src,"%s::%s()\n"
106 "{\n"
107 "referred_function = NULL;\n"
108 "}\n\n", name, name);
109
110 def = mputprintf(def,"%s(function_pointer other_value);\n", name);
111 src = mputprintf(src,"%s::%s(function_pointer other_value)\n"
112 "{\n"
113 "referred_function = other_value;\n"
114 "}\n\n", name, name);
115
116 /* copy constructor */
117 def = mputprintf(def,"%s(const %s& other_value);\n", name, name);
118 src = mputprintf(src,"%s::%s(const %s& other_value)\n"
119 ": Base_Type()" /* call the *default* constructor as before */
120 "{\n"
121 "other_value.must_bound(\"Copying an unbound %s value.\");\n"
122 "referred_function = other_value.referred_function;\n"
123 "}\n\n", name, name, name, dispname);
124
125 /* operator= */
126 def =mputprintf(def,"%s& operator=(function_pointer other_value);\n", name);
127 src =mputprintf(src,"%s& %s::operator=(function_pointer other_value)\n"
128 "{\n"
129 "referred_function = other_value;\n"
130 "return *this;\n"
131 "}\n\n", name, name);
132
133 def = mputprintf(def,"%s& operator=(const %s& other_value);\n",
134 name, name);
135 src = mputprintf(src,"%s& %s::operator=(const %s& other_value)\n"
136 "{\n"
137 "other_value.must_bound(\"Assignment of an unbound value.\");\n"
138 "referred_function = other_value.referred_function;\n"
139 "return *this;\n"
140 "}\n\n", name, name, name);
141
142 /* operator ==*/
143 def = mputstr(def,"boolean operator==(function_pointer other_value) "
144 "const;\n");
145 src = mputprintf(src,"boolean %s::operator==(function_pointer other_value) "
146 "const\n"
147 "{\n"
148 "must_bound(\"Unbound left operand of %s comparison.\");\n"
149 "return referred_function == other_value;\n"
150 "}\n\n", name, dispname);
151 def = mputprintf(def,"boolean operator==(const %s& other_value) const;\n"
152 , name);
153 src = mputprintf(src,"boolean %s::operator==(const %s& other_value) const\n"
154 "{\n"
155 "must_bound(\"Unbound left operand of %s comparison.\");\n"
156 "other_value.must_bound(\"Unbound right operand of %s comparison.\");\n"
157 "return referred_function == other_value.referred_function;\n"
158 "}\n\n", name, name, dispname, dispname);
159
160 /* operator != */
161 def = mputprintf(def,"inline boolean operator!=(function_pointer other_value)"
162 " const\n"
163 "{ return !(*this == other_value); }\n"
164 "inline boolean operator!=(const %s& other_value) const\n"
165 "{ return !(*this == other_value); }\n\n", name);
166
167 switch(fdef->type) {
168 case FUNCTION:
169 def = mputprintf(def,"%s invoke(%s) const;\n"
170 , return_type, fdef->formal_par_list);
171 src = mputprintf(src,"%s %s::invoke(%s) const\n"
172 "{\n"
173 "must_bound(\"Call of unbound function.\");\n"
174 "if(referred_function == "
175 "(%s::function_pointer)Module_List::get_fat_null())\n"
176 "TTCN_error(\"null reference cannot be invoked.\");\n"
177 "%sreferred_function(%s);\n"
178 "}\n\n", return_type, name, fdef->formal_par_list, name
179 , fdef->return_type!= NULL? "return ": "", fdef->actual_par_list);
180 if(fdef->is_startable) {
181 def = mputprintf(def,"void start(const COMPONENT& component_reference%s"
182 "%s) const;\n", strcmp(fdef->formal_par_list,"")?", ":""
183 , fdef->formal_par_list);
184 src = mputprintf(src,"void %s::start(const COMPONENT& "
185 "component_reference%s%s) const\n{\n"
186 "((%s::start_pointer)Module_List::lookup_start_by_function_address"
187 "((genericfunc_t)referred_function))(component_reference%s%s);\n"
188 "}\n\n", name, strcmp(fdef->formal_par_list,"")?", ":""
189 , fdef->formal_par_list, name, strcmp(fdef->formal_par_list,"")?", ":""
190 , fdef->actual_par_list);
191 }
192 break;
193 case ALTSTEP:
194 def = mputprintf(def,"void invoke_standalone(%s) const;\n",
195 fdef->formal_par_list);
196 src = mputprintf(src,"void %s::invoke_standalone(%s) const\n"
197 "{\n"
198 "((%s::standalone_pointer)"
199 "Module_List::lookup_standalone_address_by_altstep_address("
200 "(genericfunc_t)referred_function))(%s);\n"
201 "}\n\n", name, fdef->formal_par_list, name, fdef->actual_par_list);
202
203 def = mputprintf(def,"Default_Base *activate(%s) const;\n"
204 , fdef->formal_par_list);
205 src = mputprintf(src,"Default_Base *%s::activate(%s) const\n"
206 "{\n"
207 "return ((%s::activate_pointer)"
208 "Module_List::lookup_activate_address_by_altstep_address("
209 "(genericfunc_t)referred_function))(%s);\n"
210 "}\n\n", name, fdef->formal_par_list, name, fdef->actual_par_list);
211
212 def = mputprintf(def,"alt_status invoke(%s) const;\n"
213 , fdef->formal_par_list);
214 src = mputprintf(src,"alt_status %s::invoke(%s) const\n"
215 "{\n"
216 "must_bound(\"Call of an unbound altstep.\");\n"
217 "if(referred_function == "
218 "(%s::function_pointer)Module_List::get_fat_null())\n"
219 "TTCN_error(\"null reference cannot be invoked.\");\n"
220 "return referred_function(%s);\n"
221 "}\n", name, fdef->formal_par_list, name, fdef->actual_par_list);
222
223 break;
224 case TESTCASE:
225 def = mputprintf(def,"verdicttype execute(%s) const;\n",
226 fdef->formal_par_list);
227 src = mputprintf(src,"verdicttype %s::execute(%s) const\n"
228 "{\n"
229 "must_bound(\"Call of unbound testcase.\");\n"
230 "if(referred_function == "
231 "(%s::function_pointer)Module_List::get_fat_null())\n"
232 "TTCN_error(\"null reference cannot be executed.\");\n"
233 "return referred_function(%s);\n"
234 "}\n\n", name, fdef->formal_par_list, name, fdef->actual_par_list);
235 break;
236 }
237
238 /* bound check */
239 def = mputstr(def,"inline boolean is_bound() "
240 "const { return referred_function != NULL; }\n");
241 /* value check */
242 def = mputstr(def,"inline boolean is_value() "
243 "const { return referred_function != NULL; }\n");
244 def = mputstr(def,"inline void clean_up() "
245 "{ referred_function = NULL; }\n");
246 def = mputstr(def,"inline void must_bound(const char *err_msg) const\n"
247 "{ if (referred_function == NULL) TTCN_error(\"%s\", err_msg); }\n\n");
248
249
250
251
252 if (use_runtime_2) {
253 /* functions in alternative runtime */
254 def = mputstr(def,
255 "boolean is_equal(const Base_Type* other_value) const;\n"
256 "void set_value(const Base_Type* other_value);\n"
257 "Base_Type* clone() const;\n"
258 "const TTCN_Typedescriptor_t* get_descriptor() const;\n");
259 src = mputprintf(src,
260 "boolean %s::is_equal(const Base_Type* other_value) const "
261 "{ return *this == *(static_cast<const %s*>(other_value)); }\n"
262 "void %s::set_value(const Base_Type* other_value) "
263 "{ *this = *(static_cast<const %s*>(other_value)); }\n"
264 "Base_Type* %s::clone() const { return new %s(*this); }\n"
265 "const TTCN_Typedescriptor_t* %s::get_descriptor() const "
266 "{ return &%s_descr_; }\n",
267 name, name,
268 name, name,
269 name, name,
270 name, name);
271 } else {
272 def = mputstr(def,
273 "inline boolean is_present() const { return is_bound(); }\n");
274 }
275
276 /* log */
277 def = mputstr(def,"void log() const;\n");
278 src = mputprintf(src,"void %s::log() const\n"
279 "{\n"
280 "Module_List::log_%s((genericfunc_t)referred_function);\n"
281 "}\n\n",name, fat_string);
282
283 /* set_param */
284 def = mputstr(def,"void set_param(Module_Param& param);\n");
285 src = mputprintf(src,"void %s::set_param(Module_Param& param)\n"
286 "{\n"
287 " param.error(\"Not supported.\");\n"
288 "}\n\n", name);
3abe9331 289
290 /* get_param */
291 def = mputstr(def,"Module_Param* get_param(Module_Param_Name& param_name) const;\n");
292 src = mputprintf(src,"Module_Param* %s::get_param(Module_Param_Name& /* param_name */) const\n"
293 "{\n"
294 " return NULL;\n"
295 "}\n\n", name);
970ed795
EL
296
297 /* encode_text / decode_text */
298 def = mputstr(def,"void encode_text(Text_Buf& text_buf) const;\n");
299 src = mputprintf(src,"void %s::encode_text(Text_Buf&", name);
300 if (fdef->runs_on_self) {
301 src = mputprintf(src, ") const\n"
302 "{\n"
303 "TTCN_error(\"Values of type %s cannot be sent to "
304 "other test components.\");\n", dispname);
305 } else {
306 src = mputprintf(src, " text_buf) const\n"
307 "{\n"
308 "Module_List::encode_%s(text_buf,"
309 "(genericfunc_t)referred_function);\n", fat_string);
310 }
311 src = mputstr(src,"}\n\n");
312 def = mputstr(def,"void decode_text(Text_Buf& text_buf);\n");
313 src = mputprintf(src,"void %s::decode_text(Text_Buf&", name);
314 if (fdef->runs_on_self) {
315 src = mputprintf(src, ")\n"
316 "{\n"
317 "TTCN_error(\"Values of type %s cannot be received "
318 "from other test components.\");\n", dispname);
319 } else {
320 src = mputprintf(src, " text_buf)\n"
321 "{\n"
322 "Module_List::decode_%s(text_buf,"
323 "(genericfunc_t*)&referred_function);\n", fat_string);
324 }
325 src = mputstr(src,"}\n\n");
326 def = mputstr(def, "};\n\n");
327
328 def = mputprintf(def,"extern boolean operator==(%s::function_pointer value,"
329 " const %s& other_value);\n", name, name);
330 src = mputprintf(src,"boolean operator==(%s::function_pointer value, "
331 "const %s& other_value)\n"
332 "{\n"
333 "other_value.must_bound(\"Unbound right operand of %s comparison.\");\n"
334 "return value == other_value.referred_function;\n"
335 "}\n\n", name, name, dispname);
336 def = mputprintf(def,"inline boolean operator!=(%s::function_pointer value,"
337 " const %s& other_value)\n"
338 "{ return !(value == other_value); } \n\n", name, name);
339
340 output->header.class_defs = mputstr(output->header.class_defs, def);
341 Free(def);
342
343 output->source.methods = mputstr(output->source.methods, src);
344 Free(src);
345}
346
347void defFunctionrefTemplate(const funcref_def *fdef, output_struct *output)
348{
349 char *def = NULL, *src = NULL;
350 const char *name = fdef->name;
351 const char *dispname = fdef->dispname;
352 char *fat_string = NULL;
353
354 switch(fdef->type) {
355 case FUNCTION:
356 fat_string = mputstr(fat_string, "function");
357 break;
358 case ALTSTEP:
359 fat_string = mputstr(fat_string, "altstep");
360 break;
361 case TESTCASE:
362 fat_string = mputstr(fat_string, "testcase");
363 break;
364 }
365
366 /* class declaration */
367 output->header.class_decls = mputprintf(output->header.class_decls,
368 "class %s_template;\n", name);
369
370 /* class definition */
371 def = mputprintf(def,"class %s_template : public Base_Template {\n"
372 "union {\n"
373 "%s::function_pointer single_value;\n"
374 "struct {\n"
375 "unsigned int n_values;\n"
376 "%s_template *list_value;\n"
377 "} value_list;\n"
378 "};\n\n", name, name, name);
379
380 /* copy template */
381 def = mputprintf(def," void copy_template(const %s_template& other_value);\n"
382 , name);
383 src = mputprintf(src,"void %s_template::copy_template(const %s_template& "
384 "other_value)\n"
385 "{\n"
386 "switch(other_value.template_selection) {\n"
387 "case SPECIFIC_VALUE:\n"
388 "single_value = other_value.single_value;\n"
389 "break;\n"
390 "case OMIT_VALUE:\n"
391 "case ANY_VALUE:\n"
392 "case ANY_OR_OMIT:\n"
393 "break;\n"
394 "case VALUE_LIST:\n"
395 "case COMPLEMENTED_LIST:\n"
396 "value_list.n_values = other_value.value_list.n_values;\n"
397 "value_list.list_value = new %s_template[value_list.n_values];\n"
398 "for(unsigned int i = 0; i < value_list.n_values; i++)\n"
399 "value_list.list_value[i] = other_value.value_list.list_value[i];\n"
400 "break;\n"
401 "default:\n"
402 "TTCN_error(\"Copying an unitialized/unsupported %s template.\");\n"
403 "}\n"
404 "set_selection(other_value);\n"
405 "}\n\n", name, name, name, dispname);
406
407 /* constructors */
408 def = mputprintf(def,"public:\n"
409 "%s_template();\n", name);
410 src = mputprintf(src,"%s_template::%s_template()\n"
411 "{\n}\n\n", name, name);
412 def = mputprintf(def,"%s_template(template_sel other_value);\n", name);
413 src = mputprintf(src,"%s_template::%s_template(template_sel other_value)\n"
414 " : Base_Template(other_value)\n"
415 "{\n"
416 "check_single_selection(other_value);\n"
417 "}\n\n", name, name);
418 def = mputprintf(def,"%s_template(%s::function_pointer other_value);\n"
419 , name, name);
420 src = mputprintf(src,"%s_template::%s_template(%s::function_pointer "
421 "other_value)\n"
422 " : Base_Template(SPECIFIC_VALUE)\n"
423 "{\n"
424 "single_value = other_value;\n"
425 "}\n\n", name, name, name);
426 def = mputprintf(def,"%s_template(const %s& other_value);\n", name, name);
427 src = mputprintf(src,"%s_template::%s_template(const %s& other_value)\n"
428 " :Base_Template(SPECIFIC_VALUE)\n"
429 "{\n"
430 "other_value.must_bound(\"Creating a template from an unbound %s value."
431 "\");\n"
432 "single_value = other_value.referred_function;\n"
433 "}\n\n", name, name, name, dispname);
434 def = mputprintf(def,"%s_template(const OPTIONAL<%s>& other_value);\n"
435 , name, name);
436 src = mputprintf(src,"%s_template::%s_template(const OPTIONAL<%s>& "
437 "other_value)\n"
438 "{\n"
439 "if(other_value.ispresent()) {\n"
440 "set_selection(SPECIFIC_VALUE);\n"
441 "single_value = ((const %s&)other_value()).referred_function;\n"
442 "} else set_selection(OMIT_VALUE);\n"
443 "}\n\n", name, name, name, name);
444 def = mputprintf(def,"%s_template(const %s_template& other_value);\n"
445 , name, name);
446 src = mputprintf(src,"%s_template::%s_template(const %s_template& "
447 "other_value)\n"
448 " :Base_Template()\n" /* yes, the default constructor */
449 "{\n"
450 "copy_template(other_value);\n"
451 "}\n\n", name, name, name);
452
453 /* destructor */
454 def = mputprintf(def,"~%s_template();\n", name);
455 src = mputprintf(src,"%s_template::~%s_template()\n"
456 "{\n"
457 " clean_up();\n"
458 "}\n\n", name, name);
459
460 /* clean up */
461 def = mputstr(def,"void clean_up();\n");
462 src = mputprintf(src,"void %s_template::clean_up()"
463 "{\n"
464 "if(template_selection == VALUE_LIST ||\n"
465 "template_selection == COMPLEMENTED_LIST)\n"
466 "delete[] value_list.list_value;\n"
467 "template_selection = UNINITIALIZED_TEMPLATE;\n"
468 "}\n\n", name);
469
470 /* operator = */
471 def = mputprintf(def,"%s_template& operator=(template_sel other_value);\n"
472 , name);
473 src = mputprintf(src,"%s_template& %s_template::operator=(template_sel "
474 "other_value)\n"
475 "{\n"
476 "check_single_selection(other_value);\n"
477 "clean_up();\n"
478 "set_selection(other_value);\n"
479 "return *this;\n"
480 "}\n\n", name, name);
481 def = mputprintf(def,"%s_template& operator=(%s::function_pointer "
482 "other_value);\n", name, name);
483 src = mputprintf(src,"%s_template& %s_template::operator="
484 "(%s::function_pointer other_value)\n"
485 "{\n"
486 "clean_up();\n"
487 "set_selection(SPECIFIC_VALUE);\n"
488 "single_value = other_value;\n"
489 "return *this;"
490 "}\n\n", name, name, name);
491 def = mputprintf(def,"%s_template& operator=(const %s& other_value);\n"
492 , name, name);
493 src = mputprintf(src,"%s_template& %s_template::operator="
494 "(const %s& other_value)\n"
495 "{\n"
496 "other_value.must_bound(\"Assignment of an unbound %s value to a "
497 "template.\");\n"
498 "clean_up();\n"
499 "set_selection(SPECIFIC_VALUE);\n"
500 "single_value = other_value.referred_function;\n"
501 "return *this;\n"
502 "}\n\n", name, name, name, dispname);
503 def = mputprintf(def,"%s_template& operator=(const OPTIONAL<%s>& "
504 "other_value);\n", name, name);
505 src = mputprintf(src,"%s_template& %s_template::operator=(const "
506 "OPTIONAL<%s>& other_value)\n"
507 "{\n"
508 "clean_up();\n"
509 "if(other_value.ispresent()) { \n"
510 "set_selection(SPECIFIC_VALUE);\n"
511 "single_value = ((const %s&)other_value()).referred_function;\n"
512 "} else set_selection(OMIT_VALUE);\n"
513 "return *this;"
514 "}\n\n", name, name, name, name);
515 def = mputprintf(def,"%s_template& operator=(const %s_template& "
516 "other_value);\n", name, name);
517 src = mputprintf(src,"%s_template& %s_template::operator=(const %s_template& "
518 "other_value)\n"
519 "{\n"
520 "if(&other_value != this) {\n"
521 "clean_up();"
522 "copy_template(other_value);\n"
523 "}\n"
524 "return *this;\n"
525 "}\n\n", name ,name, name);
526
527 /* match functions */
528 def = mputprintf(def,"boolean match(%s::function_pointer "
3abe9331 529 "other_value, boolean legacy = FALSE) const;\n", name);
970ed795 530 src = mputprintf(src,"boolean %s_template::match(%s::function_pointer "
3abe9331 531 "other_value, boolean) const\n"
970ed795
EL
532 "{\n"
533 "switch(template_selection) {\n"
534 "case SPECIFIC_VALUE:\n"
535 "return single_value == other_value;\n"
536 "case OMIT_VALUE:\n"
537 "return FALSE;\n"
538 "case ANY_VALUE:\n"
539 "case ANY_OR_OMIT:\n"
540 "return TRUE;\n"
541 "case VALUE_LIST:\n"
542 "case COMPLEMENTED_LIST:\n"
543 "for(unsigned int i = 0; i < value_list.n_values; i++)\n"
544 "if(value_list.list_value[i].match(other_value))\n"
545 "return template_selection == VALUE_LIST;\n"
546 "return template_selection == COMPLEMENTED_LIST;\n"
547 "default:\n"
548 "TTCN_error(\"Matching with an unitialized/unsupported %s template."
549 "\");\n"
550 "};\n"
551 "return FALSE;\n"
552 "}\n\n", name, name, dispname);
3abe9331 553 def = mputprintf(def,"boolean match(const %s& other_value, boolean legacy "
554 "= FALSE) const;\n", name);
555 src = mputprintf(src,"boolean %s_template::match(const %s& other_value, "
556 "boolean) const\n"
970ed795 557 "{\n"
3abe9331 558 " if (!other_value.is_bound()) return FALSE;\n"
970ed795
EL
559 "return match(other_value.referred_function);\n"
560 "}\n\n", name, name);
561
562 /* value of function */
563 def = mputprintf(def,"%s valueof() const;\n", name);
564 src = mputprintf(src,"%s %s_template::valueof() const\n"
565 "{\n"
566 "if(template_selection != SPECIFIC_VALUE || is_ifpresent)\n"
567 "TTCN_error(\"Performing a valueof or send operation on a "
568 "non-specific %s template.\");\n"
569 "return single_value;\n}\n\n", name, name, dispname);
570
571 /* set type */
572 def = mputstr(def,"void set_type(template_sel template_type, "
573 "unsigned int list_length);\n");
574 src = mputprintf(src,"void %s_template::set_type(template_sel template_type, "
575 "unsigned int list_length)\n"
576 "{\n"
577 "if(template_type != VALUE_LIST && "
578 "template_type != COMPLEMENTED_LIST)\n"
579 "TTCN_error(\"Setting an invalid type for an %s template.\");\n"
580 "clean_up();\n"
581 "set_selection(template_type);\n"
582 "value_list.n_values = list_length;\n"
583 "value_list.list_value = new %s_template[list_length];\n"
584 "}\n\n", name, dispname, name);
585
586 /* list item */
587 def = mputprintf(def,"%s_template& list_item(unsigned int list_index) "
588 "const;\n", name);
589 src = mputprintf(src,"%s_template& %s_template::list_item("
590 "unsigned int list_index) const\n"
591 "{\n"
592 "if(template_selection != VALUE_LIST && "
593 "template_selection != COMPLEMENTED_LIST)\n"
594 "TTCN_error(\"Accessing a list element of a non-list template of "
595 "type %s\");\n"
596 "if(list_index >= value_list.n_values)\n"
597 "TTCN_error(\"Index overflow in a value list template of type %s."
598 "\");\n"
599 "return value_list.list_value[list_index];\n"
600 "}\n\n", name, name, dispname, dispname);
601
602 if (use_runtime_2) {
603 /* functions in alternative runtime */
604 def = mputstr(def,
605 "void valueofv(Base_Type* value) const;\n"
606 "void set_value(template_sel other_value);\n"
607 "void copy_value(const Base_Type* other_value);\n"
608 "Base_Template* clone() const;\n"
609 "const TTCN_Typedescriptor_t* get_descriptor() const;\n"
3abe9331 610 "boolean matchv(const Base_Type* other_value, boolean legacy) const;\n"
611 "void log_matchv(const Base_Type* match_value, boolean legacy) const;\n");
970ed795
EL
612 src = mputprintf(src,
613 "void %s_template::valueofv(Base_Type* value) const "
614 "{ *(static_cast<%s*>(value)) = valueof(); }\n"
615 "void %s_template::set_value(template_sel other_value) "
616 "{ *this = other_value; }\n"
617 "void %s_template::copy_value(const Base_Type* other_value) "
618 "{ *this = *(static_cast<const %s*>(other_value)); }\n"
619 "Base_Template* %s_template::clone() const "
620 "{ return new %s_template(*this); }\n"
621 "const TTCN_Typedescriptor_t* %s_template::get_descriptor() const "
622 "{ return &%s_descr_; }\n"
3abe9331 623 "boolean %s_template::matchv(const Base_Type* other_value, "
624 "boolean legacy) const "
625 "{ return match(*(static_cast<const %s*>(other_value)), legacy); }\n"
626 "void %s_template::log_matchv(const Base_Type* match_value, "
627 "boolean legacy) const "
628 " { log_match(*(static_cast<const %s*>(match_value)), legacy); }\n",
970ed795
EL
629 name, name,
630 name,
631 name, name,
632 name, name,
633 name, name,
634 name, name,
635 name, name);
636 }
637
638 /* log function */
639 def = mputstr(def,"void log() const;\n");
640 src = mputprintf(src,"void %s_template::log() const\n"
641 "{\n"
642 "switch(template_selection) {\n"
643 "case SPECIFIC_VALUE:\n"
644 "Module_List::log_%s((genericfunc_t)single_value);\n"
645 "break;\n"
646 "case COMPLEMENTED_LIST:\n"
647 "TTCN_Logger::log_event_str(\"complement \");\n"
648 "case VALUE_LIST:\n"
649 "TTCN_Logger::log_char('(');\n"
650 "for(unsigned int i = 0; i < value_list.n_values; i++) {\n"
651 "if(i > 0) TTCN_Logger::log_event_str(\", \");\n"
652 "value_list.list_value[i].log();\n"
653 "}\n"
654 "TTCN_Logger::log_char(')');\n"
655 "break;\n"
656 "default:\n"
657 "log_generic();\n"
658 "}\n"
659 "log_ifpresent();\n"
660 "}\n\n", name, fat_string);
661
662 /* log_match function */
3abe9331 663 def = mputprintf(def,"void log_match(const %s& match_value, "
664 "boolean legacy = FALSE) const;\n", name);
665 src = mputprintf(src,"void %s_template::log_match(const %s& match_value, "
666 "boolean legacy) const\n"
970ed795
EL
667 "{\n"
668 "log();\n"
669 "TTCN_Logger::log_event_str(\" with \");\n"
670 "match_value.log();\n"
3abe9331 671 "if(match(match_value, legacy)) TTCN_Logger::log_event_str(\" matched\");\n"
970ed795
EL
672 "else TTCN_Logger::log_event_str(\" unmatched\");\n"
673 "}\n\n", name, name);
674
675 /* encode_text / decode_text */
676 def = mputstr(def,"void encode_text(Text_Buf& text_buf) const;\n");
677 src = mputprintf(src,"void %s_template::encode_text(Text_Buf&", name);
678 if (fdef->runs_on_self) {
679 src = mputprintf(src, ") const\n"
680 "{\n"
681 "TTCN_error(\"Templates of type %s cannot be sent to "
682 "other test components.\");\n", dispname);
683 } else {
684 src = mputprintf(src, " text_buf) const\n"
685 "{\n"
686 "encode_text_base(text_buf);\n"
687 "switch(template_selection) {\n"
688 "case OMIT_VALUE:\n"
689 "case ANY_VALUE:\n"
690 "case ANY_OR_OMIT:\n"
691 "break;\n"
692 "case SPECIFIC_VALUE:\n"
693 "Module_List::encode_%s(text_buf, (genericfunc_t)single_value);\n"
694 "break;\n"
695 "case VALUE_LIST:\n"
696 "case COMPLEMENTED_LIST:\n"
697 "text_buf.push_int(value_list.n_values);\n"
698 "for(unsigned int i = 0; i < value_list.n_values; i++)\n"
699 "value_list.list_value[i].encode_text(text_buf);\n"
700 "break;\n"
701 "default:\n"
702 "TTCN_error(\"Text encoder: Encoding an uninitialized/unsupported template "
703 "of type %s.\");\n"
704 "}\n", fat_string, dispname);
705 }
706 src = mputstr(src,"}\n\n");
707 def = mputstr(def,"void decode_text(Text_Buf& text_buf);\n");
708 src = mputprintf(src,"void %s_template::decode_text(Text_Buf&", name);
709 if (fdef->runs_on_self) {
710 src = mputprintf(src, ")\n"
711 "{\n"
712 "TTCN_error(\"Templates of type %s cannot be received "
713 "from other test components.\");\n", dispname);
714 } else {
715 src = mputprintf(src, " text_buf)\n"
716 "{\n"
717 "clean_up();\n"
718 "decode_text_base(text_buf);\n"
719 "switch(template_selection) {\n"
720 "case OMIT_VALUE:\n"
721 "case ANY_VALUE:\n"
722 "case ANY_OR_OMIT:\n"
723 "break;\n"
724 "case SPECIFIC_VALUE:\n"
725 "Module_List::decode_%s(text_buf,(genericfunc_t*)&single_value);\n"
726 "break;\n"
727 "case VALUE_LIST:\n"
728 "case COMPLEMENTED_LIST:\n"
729 "value_list.n_values = text_buf.pull_int().get_val();\n"
730 "value_list.list_value = new %s_template[value_list.n_values];\n"
731 "for(unsigned int i = 0; i < value_list.n_values; i++)\n"
732 "value_list.list_value[i].decode_text(text_buf);\n"
733 "default:\n"
734 "TTCN_error(\"Text decoder: An unknown/unsupported selection was received "
735 "in a template of type %s.\");\n"
736 "}\n", fat_string, name, dispname);
737 }
738 src = mputstr(src,"}\n\n");
739
740 /* TTCN-3 ispresent() function */
3abe9331 741 def = mputstr(def, "boolean is_present(boolean legacy = FALSE) const;\n");
970ed795 742 src = mputprintf(src,
3abe9331 743 "boolean %s_template::is_present(boolean legacy) const\n"
970ed795
EL
744 "{\n"
745 "if (template_selection==UNINITIALIZED_TEMPLATE) return FALSE;\n"
3abe9331 746 "return !match_omit(legacy);\n"
970ed795
EL
747 "}\n\n", name);
748
749 /* match_omit() */
3abe9331 750 def = mputstr(def, "boolean match_omit(boolean legacy = FALSE) const;\n");
970ed795 751 src = mputprintf(src,
3abe9331 752 "boolean %s_template::match_omit(boolean legacy) const\n"
970ed795
EL
753 "{\n"
754 "if (is_ifpresent) return TRUE;\n"
755 "switch (template_selection) {\n"
756 "case OMIT_VALUE:\n"
757 "case ANY_OR_OMIT:\n"
758 "return TRUE;\n"
759 "case VALUE_LIST:\n"
760 "case COMPLEMENTED_LIST:\n"
3abe9331 761 "if (legacy) {\n"
970ed795
EL
762 "for (unsigned int i=0; i<value_list.n_values; i++)\n"
763 "if (value_list.list_value[i].match_omit())\n"
764 "return template_selection==VALUE_LIST;\n"
765 "return template_selection==COMPLEMENTED_LIST;\n"
3abe9331 766 "} // else fall through\n"
970ed795
EL
767 "default:\n"
768 "return FALSE;\n"
769 "}\n"
770 "return FALSE;\n"
771 "}\n\n", name);
772
773 /* set_param */
774 def = mputstr(def,"void set_param(Module_Param& param);\n");
775 src = mputprintf(src,"void %s_template::set_param(Module_Param& param)\n"
776 "{\n"
777 " param.error(\"Not supported.\");\n"
778 "}\n\n", name);
3abe9331 779
780 /* get_param */
781 def = mputstr(def,"Module_Param* get_param(Module_Param_Name& param_name) const;\n");
782 src = mputprintf(src,"Module_Param* %s_template::get_param(Module_Param_Name& /* param_name */) const\n"
783 "{\n"
784 " return NULL;\n"
785 "}\n\n", name);
970ed795
EL
786
787 if (!use_runtime_2) {
788 /* check template restriction */
789 def = mputstr(def, "void check_restriction(template_res t_res, "
3abe9331 790 "const char* t_name=NULL, boolean legacy = FALSE) const;\n");
970ed795
EL
791 src = mputprintf(src,
792 "void %s_template::check_restriction"
3abe9331 793 "(template_res t_res, const char* t_name, boolean legacy) const\n"
970ed795
EL
794 "{\n"
795 "if (template_selection==UNINITIALIZED_TEMPLATE) return;\n"
796 "switch ((t_name&&(t_res==TR_VALUE))?TR_OMIT:t_res) {\n"
797 "case TR_VALUE:\n"
798 "if (!is_ifpresent && template_selection==SPECIFIC_VALUE) return;\n"
799 "break;\n"
800 "case TR_OMIT:\n"
801 "if (!is_ifpresent && (template_selection==OMIT_VALUE || "
802 "template_selection==SPECIFIC_VALUE)) return;\n"
803 "break;\n"
804 "case TR_PRESENT:\n"
3abe9331 805 "if (!match_omit(legacy)) return;\n"
970ed795
EL
806 "break;\n"
807 "default:\n"
808 "return;\n"
809 "}\n"
810 "TTCN_error(\"Restriction `%%s' on template of type %%s violated.\", "
811 "get_res_name(t_res), t_name ? t_name : \"%s\");\n"
812 "}\n\n", name, dispname);
813 }
814
815 def = mputstr(def,"};\n\n");
816
817 Free(fat_string);
818
819 output->header.class_defs = mputstr(output->header.class_defs, def);
820 Free(def);
821
822 output->source.methods = mputstr(output->source.methods, src);
823 Free(src);
824}
This page took 0.073127 seconds and 5 git commands to generate.