Titan Core Initial Contribution
[deliverable/titan.core.git] / compiler2 / ttcn3 / TtcnTemplate.cc
CommitLineData
970ed795
EL
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 "TtcnTemplate.hh"
9#include "../Identifier.hh"
10#include "Templatestuff.hh"
11#include "../Type.hh"
12#include "../TypeCompat.hh"
13#include "../SigParam.hh"
14#include "../CompField.hh"
15#include "../Valuestuff.hh"
16#include "ArrayDimensions.hh"
17#include "PatternString.hh"
18#include "../main.hh"
19#include "../../common/dbgnew.hh"
20#include "Attributes.hh"
21
22namespace Ttcn {
23
24 // =================================
25 // ===== Template
26 // =================================
27
28 Template::Template(const Template& p) : GovernedSimple(p),
29 templatetype(p.templatetype), my_governor(p.my_governor),
30 is_ifpresent(p.is_ifpresent), specific_value_checked(false),
31 has_permutation(p.has_permutation), base_template(p.base_template)
32 {
33 switch (templatetype) {
34 case TEMPLATE_ERROR:
35 case TEMPLATE_NOTUSED:
36 case OMIT_VALUE:
37 case ANY_VALUE:
38 case ANY_OR_OMIT:
39 break;
40 case SPECIFIC_VALUE:
41 u.specific_value = p.u.specific_value->clone();
42 break;
43 case TEMPLATE_REFD:
44 u.ref.ref = p.u.ref.ref->clone();
45 u.ref.refd = 0;
46 u.ref.refd_last = 0;
47 break;
48 case TEMPLATE_INVOKE:
49 u.invoke.v = p.u.invoke.v->clone();
50 u.invoke.t_list = p.u.invoke.t_list ? p.u.invoke.t_list->clone() : 0;
51 u.invoke.ap_list = p.u.invoke.ap_list ? p.u.invoke.ap_list->clone() : 0;
52 break;
53 case ALL_FROM:
54 case VALUE_LIST_ALL_FROM:
55 u.all_from = p.u.all_from->clone();
56 break;
57 case TEMPLATE_LIST:
58 case VALUE_LIST:
59 case COMPLEMENTED_LIST:
60 case SUPERSET_MATCH:
61 case SUBSET_MATCH:
62 case PERMUTATION_MATCH:
63 u.templates = p.u.templates->clone(); // FATAL_ERROR
64 break;
65 case NAMED_TEMPLATE_LIST:
66 u.named_templates = p.u.named_templates->clone(); // FATAL_ERROR
67 break;
68 case INDEXED_TEMPLATE_LIST:
69 u.indexed_templates = p.u.indexed_templates->clone(); // FATAL_ERROR
70 break;
71 case VALUE_RANGE:
72 u.value_range = p.u.value_range->clone();
73 break;
74 case BSTR_PATTERN:
75 case HSTR_PATTERN:
76 case OSTR_PATTERN:
77 u.pattern = new string(*p.u.pattern);
78 break;
79 case CSTR_PATTERN:
80 case USTR_PATTERN:
81 u.pstring = p.u.pstring->clone();
82 break;
83// default:
84// FATAL_ERROR("Template::Template()");
85 }
86 length_restriction =
87 p.length_restriction ? p.length_restriction->clone() : 0; // FATAL_ERR
88 }
89
90 void Template::clean_up()
91 {
92 switch(templatetype) {
93 case TEMPLATE_ERROR:
94 case TEMPLATE_NOTUSED:
95 case OMIT_VALUE:
96 case ANY_VALUE:
97 case ANY_OR_OMIT:
98 break;
99 case SPECIFIC_VALUE:
100 delete u.specific_value;
101 break;
102 case TEMPLATE_REFD:
103 delete u.ref.ref;
104 break;
105 case TEMPLATE_INVOKE:
106 delete u.invoke.v;
107 delete u.invoke.t_list;
108 delete u.invoke.ap_list;
109 break;
110 case TEMPLATE_LIST:
111 case VALUE_LIST:
112 case COMPLEMENTED_LIST:
113 case SUPERSET_MATCH:
114 case SUBSET_MATCH:
115 case PERMUTATION_MATCH:
116 delete u.templates;
117 break;
118 case ALL_FROM:
119 case VALUE_LIST_ALL_FROM:
120 delete u.all_from;
121 break;
122 case NAMED_TEMPLATE_LIST:
123 delete u.named_templates;
124 break;
125 case INDEXED_TEMPLATE_LIST:
126 delete u.indexed_templates;
127 break;
128 case VALUE_RANGE:
129 delete u.value_range;
130 break;
131 case BSTR_PATTERN:
132 case HSTR_PATTERN:
133 case OSTR_PATTERN:
134 delete u.pattern;
135 break;
136 case CSTR_PATTERN:
137 case USTR_PATTERN:
138 delete u.pstring;
139 break;
140// default:
141// FATAL_ERROR("Template::clean_up()");
142 }
143 }
144
145 string Template::create_stringRepr()
146 {
147 string ret_val;
148 switch (templatetype) {
149 case TEMPLATE_ERROR:
150 ret_val += "<erroneous template>";
151 break;
152 case TEMPLATE_NOTUSED:
153 ret_val += "-";
154 break;
155 case OMIT_VALUE:
156 ret_val += "omit";
157 break;
158 case ANY_VALUE:
159 ret_val += "?";
160 break;
161 case ANY_OR_OMIT:
162 ret_val += "*";
163 break;
164 case SPECIFIC_VALUE:
165 ret_val += u.specific_value->get_stringRepr();
166 break;
167 case TEMPLATE_REFD: {
168 Template *t_last = get_template_refd_last();
169 if (t_last->templatetype == TEMPLATE_REFD)
170 ret_val += u.ref.ref->get_dispname();
171 else ret_val += t_last->get_stringRepr();
172 break; }
173 case TEMPLATE_INVOKE: {
174 ret_val += u.invoke.v->get_stringRepr();
175 ret_val += ".invoke(";
176 if(u.invoke.ap_list)
177 for(size_t i = 0; i < u.invoke.ap_list->get_nof_pars(); i++) {
178 if(i>0) ret_val += ", ";
179 ret_val += u.invoke.ap_list->get_par(i)->get_fullname();
180 }
181 ret_val += ")";
182 break; }
183 case TEMPLATE_LIST:
184 if (u.templates->get_nof_ts() > 0) {
185 ret_val += "{ ";
186 u.templates->append_stringRepr(ret_val);
187 ret_val += " }";
188 } else ret_val += "{ }";
189 break;
190 case NAMED_TEMPLATE_LIST:
191 ret_val += "{";
192 for (size_t i = 0; i < u.named_templates->get_nof_nts(); i++) {
193 if (i > 0) ret_val += ", ";
194 else ret_val += " ";
195 NamedTemplate *nt = u.named_templates->get_nt_byIndex(i);
196 ret_val += nt->get_name().get_dispname();
197 ret_val += " := ";
198 ret_val += nt->get_template()->get_stringRepr();
199 }
200 ret_val += " }";
201 break;
202 case INDEXED_TEMPLATE_LIST:
203 ret_val += "{";
204 for (size_t i = 0; i < u.indexed_templates->get_nof_its(); i++) {
205 if (i > 0) ret_val += ", ";
206 else ret_val += " [";
207 IndexedTemplate *it = u.indexed_templates->get_it_byIndex(i);
208 (it->get_index()).append_stringRepr(ret_val);
209 ret_val += "] := ";
210 ret_val += it->get_template()->get_stringRepr();
211 }
212 ret_val += "}";
213 break;
214 case VALUE_LIST:
215 ret_val += "(";
216 u.templates->append_stringRepr(ret_val);
217 ret_val += ")";
218 break;
219 case COMPLEMENTED_LIST:
220 ret_val += "complement(";
221 u.templates->append_stringRepr(ret_val);
222 ret_val += ")";
223 break;
224 case VALUE_RANGE:
225 u.value_range->append_stringRepr(ret_val);
226 break;
227 case SUPERSET_MATCH:
228 ret_val += "superset(";
229 u.templates->append_stringRepr(ret_val);
230 ret_val += ")";
231 break;
232 case SUBSET_MATCH:
233 ret_val += "subset(";
234 u.templates->append_stringRepr(ret_val);
235 ret_val += ")";
236 break;
237 case PERMUTATION_MATCH:
238 ret_val += "permutation(";
239 u.templates->append_stringRepr(ret_val);
240 ret_val += ")";
241 break;
242 case BSTR_PATTERN:
243 ret_val += "'";
244 ret_val += *u.pattern;
245 ret_val += "'B";
246 break;
247 case HSTR_PATTERN:
248 ret_val += "'";
249 ret_val += *u.pattern;
250 ret_val += "'H";
251 break;
252 case OSTR_PATTERN:
253 ret_val += "'";
254 ret_val += *u.pattern;
255 ret_val += "'O";
256 break;
257 case CSTR_PATTERN:
258 case USTR_PATTERN:
259 ret_val += "pattern \"";
260 ret_val += u.pstring->get_full_str();
261 ret_val += "\"";
262 break;
263 default:
264 ret_val += "<unknown template>";
265 break;
266 }
267 if (length_restriction) length_restriction->append_stringRepr(ret_val);
268 if (is_ifpresent) ret_val += " ifpresent";
269 return ret_val;
270 }
271
272 Template::Template(templatetype_t tt)
273 : GovernedSimple(S_TEMPLATE),
274 templatetype(tt), my_governor(0), length_restriction(0),
275 is_ifpresent(false), specific_value_checked(false),
276 has_permutation(false), flattened(true), base_template(0)
277 {
278 switch (tt) {
279 case TEMPLATE_ERROR:
280 case TEMPLATE_NOTUSED:
281 case OMIT_VALUE:
282 case ANY_VALUE:
283 case ANY_OR_OMIT:
284 break;
285 default:
286 FATAL_ERROR("Template::Template()");
287 }
288 }
289
290 Template::Template(Value *v)
291 : GovernedSimple(S_TEMPLATE),
292 templatetype(SPECIFIC_VALUE), my_governor(0), length_restriction(0),
293 is_ifpresent(false), specific_value_checked(false),
294 has_permutation(false), flattened(true), base_template(0)
295 {
296 if (!v) FATAL_ERROR("Template::Template()");
297 u.specific_value = v;
298 }
299
300 Template::Template(Ref_base *p_ref)
301 : GovernedSimple(S_TEMPLATE),
302 templatetype(TEMPLATE_REFD), my_governor(0), length_restriction(0),
303 is_ifpresent(false), specific_value_checked(false),
304 has_permutation(false), base_template(0)
305 {
306 if(!p_ref) FATAL_ERROR("Template::Template()");
307 u.ref.ref=p_ref;
308 u.ref.refd=0;
309 u.ref.refd_last=0;
310 }
311
312 Template::Template(templatetype_t tt, Templates *ts)
313 : GovernedSimple(S_TEMPLATE),
314 templatetype(tt), my_governor(0), length_restriction(0),
315 is_ifpresent(false), specific_value_checked(false),
316 has_permutation(false), flattened(true), base_template(0)
317 {
318 switch (tt) {
319 case TEMPLATE_LIST:
320 case VALUE_LIST:
321 case COMPLEMENTED_LIST:
322 case SUPERSET_MATCH:
323 case SUBSET_MATCH:
324 case PERMUTATION_MATCH:
325 break;
326 default:
327 FATAL_ERROR("Template::Template()");
328 }
329 if (!ts) FATAL_ERROR("Template::Template()");
330 u.templates = ts;
331 if (tt == TEMPLATE_LIST) {
332 size_t nof_ts = ts->get_nof_ts();
333 for (size_t i = 0; i < nof_ts; i++) {
334 if (ts->get_t_byIndex(i)->templatetype == PERMUTATION_MATCH) {
335 has_permutation = true;
336 break;
337 }
338 }
339 }
340 }
341
342 Template::Template(Template *t)
343 : GovernedSimple(S_TEMPLATE)
344 , templatetype(ALL_FROM), my_governor(0), length_restriction(0)
345 , is_ifpresent(false), specific_value_checked(false)
346 , has_permutation(false), flattened(true), base_template(0)
347 {
348 u.all_from = t;
349 // t is usually a SPECIFIC_VALUE
350 // t->u.specific_value is a V_UNDEF_LOWERID
351 // calling set_lowerid_to_ref is too soon (my_scope is not set yet)
352 }
353
354 Template::Template(templatetype_t tt, Template *t)
355 : GovernedSimple(S_TEMPLATE)
356 , templatetype(VALUE_LIST_ALL_FROM), my_governor(0), length_restriction(0)
357 , is_ifpresent(false), specific_value_checked(false)
358 , has_permutation(false), flattened(true), base_template(0)
359 {
360 if (tt != VALUE_LIST_ALL_FROM) FATAL_ERROR("Template::Template()");
361 u.all_from = t->u.all_from; // take it over
362 t->u.all_from = NULL;
363 delete t;
364 }
365
366 Template::Template(NamedTemplates *nts)
367 : GovernedSimple(S_TEMPLATE),
368 templatetype(NAMED_TEMPLATE_LIST), my_governor(0), length_restriction(0),
369 is_ifpresent(false), specific_value_checked(false),
370 has_permutation(false), flattened(true), base_template(0)
371 {
372 if (!nts) FATAL_ERROR("Template::Template()");
373 u.named_templates = nts;
374 }
375
376 Template::Template(IndexedTemplates *its)
377 : GovernedSimple(S_TEMPLATE),
378 templatetype(INDEXED_TEMPLATE_LIST), my_governor(0),
379 length_restriction(0), is_ifpresent(false),
380 specific_value_checked(false), has_permutation(false), flattened(true),
381 base_template(0)
382 {
383 if (!its) FATAL_ERROR("Template::Template()");
384 u.indexed_templates = its;
385 size_t nof_its = its->get_nof_its();
386 for (size_t i = 0; i < nof_its; i++) {
387 if (its->get_it_byIndex(i)->get_template()->templatetype ==
388 PERMUTATION_MATCH) {
389 has_permutation = true;
390 break;
391 }
392 }
393 }
394
395 Template::Template(ValueRange *vr)
396 : GovernedSimple(S_TEMPLATE),
397 templatetype(VALUE_RANGE), my_governor(0), length_restriction(0),
398 is_ifpresent(false), specific_value_checked(false),
399 has_permutation(false), flattened(true), base_template(0)
400 {
401 if (!vr) FATAL_ERROR("Template::Template()");
402 u.value_range = vr;
403 }
404
405 Template::Template(templatetype_t tt, string *p_patt)
406 : GovernedSimple(S_TEMPLATE),
407 templatetype(tt), my_governor(0), length_restriction(0),
408 is_ifpresent(false), specific_value_checked(false),
409 has_permutation(false), flattened(true), base_template(0)
410 {
411 switch (tt) {
412 case BSTR_PATTERN:
413 case HSTR_PATTERN:
414 case OSTR_PATTERN:
415 break;
416 default:
417 FATAL_ERROR("Template::Template()");
418 }
419 if (!p_patt) FATAL_ERROR("Template::Template()");
420 u.pattern = p_patt;
421 }
422
423 Template::Template(PatternString *p_ps)
424 : GovernedSimple(S_TEMPLATE),
425 templatetype(CSTR_PATTERN), my_governor(0), length_restriction(0),
426 is_ifpresent(false), specific_value_checked(false),
427 has_permutation(false), flattened(true), base_template(0)
428 {
429 if (!p_ps) FATAL_ERROR("Template::Template()");
430 u.pstring = p_ps;
431 }
432
433 Template::~Template()
434 {
435 clean_up();
436 delete length_restriction;
437 }
438
439 Template *Template::clone() const
440 {
441 return new Template(*this);
442 }
443
444 void Template::set_fullname(const string& p_fullname)
445 {
446 GovernedSimple::set_fullname(p_fullname);
447 switch (templatetype) {
448 case TEMPLATE_ERROR:
449 case TEMPLATE_NOTUSED:
450 case OMIT_VALUE:
451 case ANY_VALUE:
452 case ANY_OR_OMIT:
453 case BSTR_PATTERN:
454 case HSTR_PATTERN:
455 case OSTR_PATTERN:
456 break;
457 case SPECIFIC_VALUE:
458 u.specific_value->set_fullname(p_fullname);
459 break;
460 case TEMPLATE_REFD:
461 u.ref.ref->set_fullname(p_fullname);
462 break;
463 case TEMPLATE_INVOKE:
464 u.invoke.v->set_fullname(p_fullname);
465 if(u.invoke.t_list) u.invoke.t_list->set_fullname(p_fullname);
466 if(u.invoke.ap_list) u.invoke.ap_list->set_fullname(p_fullname);
467 break;
468 case TEMPLATE_LIST:
469 u.templates->set_fullname(p_fullname);
470 for (size_t i = 0; i < u.templates->get_nof_ts(); i++)
471 u.templates->get_t_byIndex(i)->set_fullname(
472 p_fullname + "[" + Int2string(i) + "]");
473 break;
474 case INDEXED_TEMPLATE_LIST:
475 u.indexed_templates->set_fullname(p_fullname);
476 for (size_t i = 0; i < u.indexed_templates->get_nof_its(); i++)
477 u.indexed_templates->get_it_byIndex(i)->set_fullname(
478 p_fullname + "[" + Int2string(i) + "]");
479 break;
480 case NAMED_TEMPLATE_LIST:
481 u.named_templates->set_fullname(p_fullname);
482 break;
483 case ALL_FROM:
484 case VALUE_LIST_ALL_FROM:
485 u.all_from->set_fullname(p_fullname);
486 break;
487 case VALUE_LIST:
488 case COMPLEMENTED_LIST:
489 case SUPERSET_MATCH:
490 case SUBSET_MATCH:
491 case PERMUTATION_MATCH:
492 u.templates->set_fullname(p_fullname);
493 for (size_t i = 0; i < u.templates->get_nof_ts(); i++)
494 u.templates->get_t_byIndex(i)->set_fullname(
495 p_fullname + ".list_item(" + Int2string(i) + ")");
496 break;
497 case VALUE_RANGE:
498 u.value_range->set_fullname(p_fullname);
499 break;
500 case CSTR_PATTERN:
501 case USTR_PATTERN:
502 u.pstring->set_fullname(p_fullname);
503 break;
504// default:
505// FATAL_ERROR("Template::set_fullname()");
506 }
507 if (length_restriction)
508 length_restriction->set_fullname(p_fullname + ".<length_restriction>");
509 }
510
511 void Template::set_my_scope(Scope *p_scope)
512 {
513 GovernedSimple::set_my_scope(p_scope);
514 switch (templatetype) {
515 case TEMPLATE_ERROR:
516 case TEMPLATE_NOTUSED:
517 case OMIT_VALUE:
518 case ANY_VALUE:
519 case ANY_OR_OMIT:
520 case BSTR_PATTERN:
521 case HSTR_PATTERN:
522 case OSTR_PATTERN:
523 break;
524 case SPECIFIC_VALUE:
525 u.specific_value->set_my_scope(p_scope);
526 break;
527 case TEMPLATE_REFD:
528 u.ref.ref->set_my_scope(p_scope);
529 break;
530 case TEMPLATE_INVOKE:
531 u.invoke.v->set_my_scope(p_scope);
532 if(u.invoke.t_list) u.invoke.t_list->set_my_scope(p_scope);
533 if(u.invoke.ap_list) u.invoke.ap_list->set_my_scope(p_scope);
534 break;
535 case ALL_FROM:
536 case VALUE_LIST_ALL_FROM:
537 u.all_from->set_my_scope(p_scope);
538 break;
539 case TEMPLATE_LIST:
540 case VALUE_LIST:
541 case COMPLEMENTED_LIST:
542 case SUPERSET_MATCH:
543 case SUBSET_MATCH:
544 case PERMUTATION_MATCH:
545 u.templates->set_my_scope(p_scope);
546 break;
547 case NAMED_TEMPLATE_LIST:
548 u.named_templates->set_my_scope(p_scope);
549 break;
550 case INDEXED_TEMPLATE_LIST:
551 u.indexed_templates->set_my_scope(p_scope);
552 break;
553 case VALUE_RANGE:
554 u.value_range->set_my_scope(p_scope);
555 break;
556 case CSTR_PATTERN:
557 case USTR_PATTERN:
558 u.pstring->set_my_scope(p_scope);
559 break;
560// default:
561// FATAL_ERROR("Template::set_my_scope()");
562 }
563 if (length_restriction) length_restriction->set_my_scope(p_scope);
564 }
565
566 void Template::set_genname_recursive(const string& p_genname)
567 {
568 set_genname(p_genname);
569 switch (templatetype) {
570 case TEMPLATE_LIST: {
571 if (!my_governor) return; // error recovery
572 Type *type = my_governor->get_type_refd_last();
573 Int offset;
574 if (type->get_typetype() == Type::T_ARRAY)
575 offset = type->get_dimension()->get_offset();
576 else offset = 0;
577 size_t nof_ts = u.templates->get_nof_ts();
578 for (size_t i = 0; i < nof_ts; i++) {
579 string embedded_genname(p_genname);
580 embedded_genname += '[';
581 embedded_genname += Int2string(offset + i);
582 embedded_genname += ']';
583 u.templates->get_t_byIndex(i)->set_genname_recursive(embedded_genname);
584 }
585 break; }
586 case NAMED_TEMPLATE_LIST: {
587 if (!my_governor) return; // error recovery
588 Type *type = my_governor->get_type_refd_last();
589 size_t nof_nts = u.named_templates->get_nof_nts();
590 for (size_t i = 0; i < nof_nts; i++) {
591 NamedTemplate *nt = u.named_templates->get_nt_byIndex(i);
592 string embedded_genname(p_genname);
593 embedded_genname += '.';
594 if (type->get_typetype() == Type::T_ANYTYPE)
595 embedded_genname += "AT_";
596 embedded_genname += nt->get_name().get_name();
597 embedded_genname += "()";
598 nt->get_template()->set_genname_recursive(embedded_genname);
599 }
600 break; }
601 default:
602 break;
603 }
604 }
605
606 void Template::set_genname_prefix(const char *p_genname_prefix)
607 {
608 GovernedSimple::set_genname_prefix(p_genname_prefix);
609 switch (templatetype) {
610 case TEMPLATE_LIST:
611 for (size_t i = 0; i < u.templates->get_nof_ts(); i++)
612 u.templates->get_t_byIndex(i)->set_genname_prefix(p_genname_prefix);
613 break;
614 case NAMED_TEMPLATE_LIST:
615 for (size_t i = 0; i < u.named_templates->get_nof_nts(); i++)
616 u.named_templates->get_nt_byIndex(i)->get_template()
617 ->set_genname_prefix(p_genname_prefix);
618 break;
619 case INDEXED_TEMPLATE_LIST:
620 for (size_t i = 0; i < u.indexed_templates->get_nof_its(); i++)
621 u.indexed_templates->get_it_byIndex(i)->get_template()
622 ->set_genname_prefix(p_genname_prefix);
623 break;
624 default:
625 break;
626 }
627 }
628
629 void Template::set_code_section(code_section_t p_code_section)
630 {
631 GovernedSimple::set_code_section(p_code_section);
632 switch (templatetype) {
633 case SPECIFIC_VALUE:
634 u.specific_value->set_code_section(p_code_section);
635 break;
636 case TEMPLATE_REFD:
637 u.ref.ref->set_code_section(p_code_section);
638 break;
639 case TEMPLATE_INVOKE:
640 u.invoke.v->set_code_section(p_code_section);
641 if(u.invoke.t_list) u.invoke.t_list->set_code_section(p_code_section);
642 if(u.invoke.ap_list)
643 for(size_t i = 0; i < u.invoke.ap_list->get_nof_pars(); i++)
644 u.invoke.ap_list->get_par(i)->set_code_section(p_code_section);
645 break;
646 case TEMPLATE_LIST:
647 case VALUE_LIST:
648 case COMPLEMENTED_LIST:
649 case SUPERSET_MATCH:
650 case SUBSET_MATCH:
651 case PERMUTATION_MATCH:
652 for (size_t i = 0; i < u.templates->get_nof_ts(); i++)
653 u.templates->get_t_byIndex(i)->set_code_section(p_code_section);
654 break;
655 case NAMED_TEMPLATE_LIST:
656 for (size_t i = 0; i < u.named_templates->get_nof_nts(); i++)
657 u.named_templates->get_nt_byIndex(i)->get_template()
658 ->set_code_section(p_code_section);
659 break;
660 case INDEXED_TEMPLATE_LIST:
661 for (size_t i = 0; i <u.indexed_templates->get_nof_its(); i++)
662 u.indexed_templates->get_it_byIndex(i)
663 ->set_code_section(p_code_section);
664 break;
665 case VALUE_RANGE:
666 u.value_range->set_code_section(p_code_section);
667 break;
668 case CSTR_PATTERN:
669 case USTR_PATTERN:
670 u.pstring->set_code_section(p_code_section);
671 break;
672 default:
673 break;
674 }
675 if (length_restriction)
676 length_restriction->set_code_section(p_code_section);
677 }
678
679 void Template::set_templatetype(templatetype_t p_templatetype)
680 {
681 if (p_templatetype == templatetype) return;
682 if (p_templatetype == TEMPLATE_ERROR) {
683 clean_up();
684 templatetype = TEMPLATE_ERROR;
685 return;
686 }
687 switch (templatetype) {
688 case SPECIFIC_VALUE: // current type
689 switch(p_templatetype) {
690 case TEMPLATE_REFD: {
691 Value *v = u.specific_value;
692 u.ref.ref = v->steal_ttcn_ref_base();
693 u.ref.refd = 0;
694 u.ref.refd_last = 0;
695 delete v;
696 break; }
697 case TEMPLATE_INVOKE: {
698 Value *v = u.specific_value;
699 v->steal_invoke_data(u.invoke.v, u.invoke.t_list, u.invoke.ap_list);
700 delete v;
701 break; }
702 default:
703 FATAL_ERROR("Template::set_templatetype()");
704 }
705 break;
706 case TEMPLATE_LIST: // current type
707 if (p_templatetype == NAMED_TEMPLATE_LIST) {
708 // TEMPLATE_LIST -> NAMED_TEMPLATE_LIST:
709 // value list -> assignment notation
710 // applicable to record types, signatures and empty set types
711 if (!my_governor) FATAL_ERROR("Template::set_templatetype()");
712 Type *t = my_governor->get_type_refd_last();
713 size_t nof_comps = t->get_nof_comps(); // "expected" nr of components
714 Templates *sts = u.templates;
715 size_t nof_temps = sts->get_nof_ts(); // "actual" nr in the list
716 Type::typetype_t tt = t->get_typetype();
717 switch (tt) {
718 case Type::T_SEQ_T:
719 case Type::T_SEQ_A:
720 case Type::T_SIGNATURE:
721 break;
722 case Type::T_SET_A:
723 case Type::T_SET_T:
724 if (nof_temps == 0 && nof_comps == 0) break;
725 default:
726 FATAL_ERROR("Template::set_templatetype()");
727 }
728 // If it's a record or set template, allow fewer elements than
729 // what is required, because implicit omit may take care of it.
730 // If it's a signature template, be precise.
731 bool allow_fewer = false;
732 switch (my_governor->get_typetype()) {
733 case Type::T_SEQ_T: case Type::T_SET_T:
734 allow_fewer = true;
735 break;
736 case Type::T_SIGNATURE: // be precise
737 break;
738 default: // not possible, fatal error ?
739 break;
740 }
741 if ( nof_temps > nof_comps
742 || (!allow_fewer && (nof_temps < nof_comps))) {
743 error("Too %s elements in value list notation for type `%s': "
744 "%lu was expected instead of %lu",
745 nof_temps > nof_comps ? "many" : "few",
746 t->get_typename().c_str(),
747 (unsigned long) nof_comps, (unsigned long) nof_temps);
748 }
749 size_t upper_limit; // min(nof_temps, nof_comps)
750 bool all_notused;
751 if (nof_temps <= nof_comps) {
752 upper_limit = nof_temps;
753 all_notused = true;
754 } else {
755 upper_limit = nof_comps;
756 all_notused = false;
757 }
758 u.named_templates = new NamedTemplates;
759 for (size_t i = 0; i < upper_limit; i++) {
760 Template*& temp = sts->get_t_byIndex(i);
761 if (temp->templatetype == TEMPLATE_NOTUSED) continue;
762 all_notused = false;
763 NamedTemplate *nt = new
764 NamedTemplate(t->get_comp_id_byIndex(i).clone(), temp);
765 nt->set_location(*temp);
766 u.named_templates->add_nt(nt);
767 temp = 0;
768 }
769 u.named_templates->set_my_scope(get_my_scope());
770 u.named_templates->set_fullname(get_fullname());
771 delete sts;
772 if (all_notused && nof_temps > 0 && tt != Type::T_SIGNATURE)
773 warning("All elements of value list notation for type `%s' are "
774 "not used symbols (`-')", t->get_typename().c_str());
775 } else FATAL_ERROR("Template::set_templatetype()");
776 break;
777 case CSTR_PATTERN: // current type
778 if (p_templatetype == USTR_PATTERN)
779 templatetype = USTR_PATTERN;
780 else
781 FATAL_ERROR("Template::set_templatetype()");
782 break;
783 default:
784 FATAL_ERROR("Template::set_templatetype()");
785 }
786 templatetype = p_templatetype;
787 }
788
789 const char *Template::get_templatetype_str() const
790 {
791 switch(templatetype) {
792 case TEMPLATE_ERROR:
793 return "erroneous template";
794 case TEMPLATE_NOTUSED:
795 return "not used symbol";
796 case OMIT_VALUE:
797 return "omit value";
798 case ANY_VALUE:
799 return "any value";
800 case ANY_OR_OMIT:
801 return "any or omit";
802 case SPECIFIC_VALUE:
803 return "specific value";
804 case TEMPLATE_REFD:
805 return "referenced template";
806 case TEMPLATE_INVOKE:
807 return "template returning invoke";
808 case ALL_FROM:
809 case VALUE_LIST_ALL_FROM:
810 return "template with 'all from'";
811 case TEMPLATE_LIST:
812 return "value list notation";
813 case NAMED_TEMPLATE_LIST:
814 return "assignment notation";
815 case INDEXED_TEMPLATE_LIST:
816 return "assignment notation with array indices";
817 case VALUE_RANGE:
818 return "value range match";
819 case VALUE_LIST:
820 return "value list match";
821 case COMPLEMENTED_LIST:
822 return "complemented list match";
823 case SUPERSET_MATCH:
824 return "superset match";
825 case SUBSET_MATCH:
826 return "subset match";
827 case PERMUTATION_MATCH:
828 return "permutation match";
829 case BSTR_PATTERN:
830 return "bitstring pattern";
831 case HSTR_PATTERN:
832 return "hexstring pattern";
833 case OSTR_PATTERN:
834 return "octetstring pattern";
835 case CSTR_PATTERN:
836 return "character string pattern";
837 case USTR_PATTERN:
838 return "universal string pattern";
839 default:
840 return "unknown template";
841 }
842 }
843
844 bool Template::is_undef_lowerid()
845 {
846 return templatetype == SPECIFIC_VALUE &&
847 u.specific_value->is_undef_lowerid();
848 }
849
850 void Template::set_lowerid_to_ref()
851 {
852 switch (templatetype) {
853 case SPECIFIC_VALUE:
854 u.specific_value->set_lowerid_to_ref();
855 if (u.specific_value->get_valuetype() == Value::V_REFD) {
856 Common::Assignment *t_ass =
857 u.specific_value->get_reference()->get_refd_assignment(false);
858 if (t_ass) {
859 switch (t_ass->get_asstype()) {
860 case Common::Assignment::A_MODULEPAR_TEMP:
861 case Common::Assignment::A_TEMPLATE:
862 case Common::Assignment::A_VAR_TEMPLATE:
863 case Common::Assignment::A_PAR_TEMPL_IN:
864 case Common::Assignment::A_PAR_TEMPL_OUT:
865 case Common::Assignment::A_PAR_TEMPL_INOUT:
866 case Common::Assignment::A_FUNCTION_RTEMP:
867 case Common::Assignment::A_EXT_FUNCTION_RTEMP:
868 set_templatetype(TEMPLATE_REFD);
869 default:
870 break;
871 }
872 } else set_templatetype(TEMPLATE_ERROR);
873 }
874 break;
875 case VALUE_LIST:
876 case COMPLEMENTED_LIST:
877 for (size_t i = 0; i < u.templates->get_nof_ts(); i++)
878 u.templates->get_t_byIndex(i)->set_lowerid_to_ref();
879 break;
880 case VALUE_RANGE:
881 u.value_range->set_lowerid_to_ref();
882 break;
883 default:
884 break;
885 }
886 }
887
888 Type::typetype_t Template::get_expr_returntype(Type::expected_value_t exp_val)
889 {
890 switch (templatetype) {
891 case TEMPLATE_ERROR:
892 return Type::T_ERROR;
893 case SPECIFIC_VALUE:
894 return u.specific_value->get_expr_returntype(exp_val);
895 case TEMPLATE_REFD: {
896 Type *t = get_expr_governor(exp_val);
897 if (t) return t->get_type_refd_last()->get_typetype_ttcn3();
898 else return Type::T_ERROR; }
899 case TEMPLATE_INVOKE: {
900 Type *t = get_expr_governor(exp_val);
901 if(t) return t->get_type_refd_last()->get_typetype_ttcn3();
902 else return Type::T_ERROR; }
903 case VALUE_LIST:
904 case COMPLEMENTED_LIST:
905 for (size_t i = 0; i < u.templates->get_nof_ts(); i++) {
906 Type::typetype_t tt = u.templates->get_t_byIndex(i)
907 ->get_expr_returntype(exp_val);
908 if (tt != Type::T_UNDEF) return tt;
909 }
910 return Type::T_UNDEF;
911 case VALUE_RANGE:
912 return u.value_range->get_expr_returntype(exp_val);
913 case SUPERSET_MATCH:
914 case SUBSET_MATCH:
915 return Type::T_SETOF;
916 case BSTR_PATTERN:
917 return Type::T_BSTR;
918 case HSTR_PATTERN:
919 return Type::T_HSTR;
920 case OSTR_PATTERN:
921 return Type::T_OSTR;
922 case CSTR_PATTERN:
923 return Type::T_CSTR;
924 case USTR_PATTERN:
925 return Type::T_USTR;
926 default:
927 return Type::T_UNDEF;
928 }
929 }
930
931 Type *Template::get_expr_governor(Type::expected_value_t exp_val)
932 {
933 if (my_governor) return my_governor;
934 switch (templatetype) {
935 case SPECIFIC_VALUE:
936 return u.specific_value->get_expr_governor(exp_val);
937 case VALUE_LIST:
938 case COMPLEMENTED_LIST:
939 for (size_t i = 0; i < u.templates->get_nof_ts(); i++) {
940 Type *t = u.templates->get_t_byIndex(i)->get_expr_governor(exp_val);
941 if (t) return t;
942 }
943 return 0;
944 case VALUE_RANGE:
945 return u.value_range->get_expr_governor(exp_val);
946 case TEMPLATE_REFD: {
947 Type *t = u.ref.ref->get_refd_assignment()->get_Type()
948 ->get_field_type(u.ref.ref->get_subrefs(), exp_val);
949 if (!t) set_templatetype(TEMPLATE_ERROR);
950 return t;
951 }
952 case TEMPLATE_INVOKE: {
953 Type *t = u.invoke.v->get_expr_governor(Type::EXPECTED_DYNAMIC_VALUE);
954 if(!t) {
955 if(u.invoke.v->get_valuetype() != Value::V_ERROR)
956 u.invoke.v->error("A value of type function expected");
957 goto error;
958 }
959 t = t->get_type_refd_last();
960 switch(t->get_typetype()) {
961 case Type::T_FUNCTION:
962 if(exp_val==Type::EXPECTED_DYNAMIC_VALUE && t->get_returns_template()){
963 error("Reference to a value was expected instead of a "
964 "template of type `%s'"
965 , t->get_function_return_type()->get_typename().c_str());
966 goto error;
967 }
968 return t->get_function_return_type();
969 case Type::T_ALTSTEP:
970 goto error;
971 default:
972 u.invoke.v->error("A value of type function expected instead of `%s'",
973 t->get_typename().c_str());
974 goto error;
975 }
976 break; }
977 default:
978 return Type::get_pooltype(get_expr_returntype(exp_val));
979 }
980 error:
981 set_templatetype(TEMPLATE_ERROR);
982 return 0;
983 }
984
985 void Template::set_my_governor(Type *p_gov)
986 {
987 if (!p_gov)
988 FATAL_ERROR("Template::set_my_governor(): NULL parameter");
989 my_governor=p_gov;
990 }
991
992 Type *Template::get_my_governor() const
993 {
994 return my_governor;
995 }
996
997 void Template::set_length_restriction(LengthRestriction *p_lr)
998 {
999 if (length_restriction) FATAL_ERROR("Template::set_length_restriction()");
1000 length_restriction = p_lr;
1001 }
1002
1003 Template::completeness_t
1004 Template::get_completeness_condition_seof(bool incomplete_allowed)
1005 {
1006 if (!incomplete_allowed) return C_MUST_COMPLETE;
1007 else if (!base_template) return C_MAY_INCOMPLETE;
1008 else {
1009 Template *t = base_template->get_template_refd_last();
1010 switch (t->templatetype) {
1011 // partial overwriting is allowed
1012 case TEMPLATE_ERROR: // to suppress more errors
1013 case TEMPLATE_NOTUSED: // modifying a modified template
1014 case ANY_VALUE: // in case of ?
1015 case ANY_OR_OMIT: // in case of *
1016 case TEMPLATE_REFD: // e.g. the actual value of a formal parameter
1017 case TEMPLATE_INVOKE:
1018 case NAMED_TEMPLATE_LIST: // t is erroneous
1019 case INDEXED_TEMPLATE_LIST:
1020 return C_MAY_INCOMPLETE;
1021 case TEMPLATE_LIST:
1022 switch (my_governor->get_type_refd_last()->get_typetype()) {
1023 case Type::T_SEQOF:
1024 case Type::T_SETOF:
1025 // only the first elements can be incomplete
1026 return C_PARTIAL;
1027 default:
1028 // we are in error recovery
1029 return C_MAY_INCOMPLETE;
1030 }
1031 break; // should not get here
1032 default:
1033 // partial overwriting is not allowed for literal specific values,
1034 // matching ranges/lists/sets and patterns
1035 return C_MUST_COMPLETE;
1036 }
1037 }
1038 }
1039
1040 Template::completeness_t Template::get_completeness_condition_choice
1041 (bool incomplete_allowed, const Identifier& p_fieldname)
1042 {
1043 if (!incomplete_allowed) return C_MUST_COMPLETE;
1044 else if (!base_template) return C_MAY_INCOMPLETE;
1045 else {
1046 Template *t = base_template->get_template_refd_last();
1047 switch (t->templatetype) {
1048 // partial overwriting is allowed
1049 case TEMPLATE_ERROR: // to suppress more errors
1050 case TEMPLATE_NOTUSED: // t is erroneous
1051 case ANY_VALUE: // in case of ?
1052 case ANY_OR_OMIT: // in case of *
1053 case TEMPLATE_REFD: // e.g. the actual value of a formal parameter
1054 case TEMPLATE_INVOKE:
1055 case TEMPLATE_LIST: // t is erroneous
1056 return C_MAY_INCOMPLETE;
1057 case NAMED_TEMPLATE_LIST: // some fields may be missing
1058 if (t->u.named_templates->has_nt_withName(p_fieldname))
1059 return C_MAY_INCOMPLETE;
1060 else return C_MUST_COMPLETE;
1061 default:
1062 // partial overwriting is not allowed for literal specific values,
1063 // matching ranges/lists/sets and patterns
1064 return C_MUST_COMPLETE;
1065 }
1066 }
1067 }
1068
1069 void Template::add_named_temp(NamedTemplate* nt) {
1070 if (templatetype != NAMED_TEMPLATE_LIST)
1071 FATAL_ERROR("Template::add_named_temp()");
1072 u.named_templates->add_nt(nt);
1073 }
1074
1075 Value *Template::get_specific_value() const
1076 {
1077 if (templatetype != SPECIFIC_VALUE)
1078 FATAL_ERROR("Template::get_specific_value()");
1079 return u.specific_value;
1080 }
1081
1082 Ref_base *Template::get_reference() const
1083 {
1084 if (templatetype != TEMPLATE_REFD)
1085 FATAL_ERROR("Template::get_reference()");
1086 return u.ref.ref;
1087 }
1088
1089 ValueRange *Template::get_value_range() const
1090 {
1091 if (templatetype != VALUE_RANGE)
1092 FATAL_ERROR("Template::get_value_range()");
1093 return u.value_range;
1094 }
1095
1096 PatternString* Template::get_cstr_pattern() const
1097 {
1098 if (templatetype != CSTR_PATTERN)
1099 FATAL_ERROR("Template::get_cstr_pattern()");
1100 return u.pstring;
1101 }
1102
1103 PatternString* Template::get_ustr_pattern() const
1104 {
1105 if (templatetype != USTR_PATTERN)
1106 FATAL_ERROR("Template::get_ustr_pattern()");
1107 return u.pstring;
1108 }
1109
1110 size_t Template::get_nof_comps() const
1111 {
1112 switch (templatetype) {
1113 case TEMPLATE_LIST:
1114 case VALUE_LIST:
1115 case COMPLEMENTED_LIST:
1116 case SUPERSET_MATCH:
1117 case SUBSET_MATCH:
1118 case PERMUTATION_MATCH:
1119 return u.templates->get_nof_ts();
1120 case NAMED_TEMPLATE_LIST:
1121 return u.named_templates->get_nof_nts();
1122 case INDEXED_TEMPLATE_LIST:
1123 return u.indexed_templates->get_nof_its();
1124 default:
1125 FATAL_ERROR("Template::get_of_comps()");
1126 return 0;
1127 }
1128 }
1129
1130 Template *Template::get_temp_byIndex(size_t n) const
1131 {
1132 switch (templatetype) {
1133 case TEMPLATE_LIST:
1134 case VALUE_LIST:
1135 case COMPLEMENTED_LIST:
1136 case SUPERSET_MATCH:
1137 case SUBSET_MATCH:
1138 case PERMUTATION_MATCH:
1139 return u.templates->get_t_byIndex(n);
1140 default:
1141 FATAL_ERROR("Template::get_temp_byIndex()");
1142 return 0;
1143 }
1144 }
1145
1146 IndexedTemplate *Template::get_indexedtemp_byIndex(size_t n) const
1147 {
1148 if (templatetype != INDEXED_TEMPLATE_LIST)
1149 FATAL_ERROR("Template::get_indexedtemp_byIndex()");
1150 return u.indexed_templates->get_it_byIndex(n);
1151 }
1152
1153 NamedTemplate *Template::get_namedtemp_byIndex(size_t n) const
1154 {
1155 if (templatetype != NAMED_TEMPLATE_LIST)
1156 FATAL_ERROR("Template::get_namedtemp_byIndex()");
1157 return u.named_templates->get_nt_byIndex(n);
1158 }
1159
1160 Template *Template::get_all_from() const
1161 {
1162 if (templatetype != ALL_FROM
1163 &&templatetype != VALUE_LIST_ALL_FROM)
1164 FATAL_ERROR("Template::get_all_from()");
1165 return u.all_from;
1166 }
1167
1168 // Not applicable to INDEXED_TEMPLATE_LIST nodes. The actual number of
1169 // elements is not known.
1170 size_t Template::get_nof_listitems() const
1171 {
1172 if (templatetype != TEMPLATE_LIST)
1173 FATAL_ERROR("Template::get_nof_listitems()");
1174 if (has_permutation) {
1175 size_t nof_ts = u.templates->get_nof_ts(), ret_val = 0;
1176 for (size_t i = 0; i < nof_ts; i++) {
1177 Template *t = u.templates->get_t_byIndex(i);
1178 if (t->templatetype == PERMUTATION_MATCH)
1179 ret_val += t->u.templates->get_nof_ts();
1180 else ret_val++;
1181 }
1182 return ret_val;
1183 } else return u.templates->get_nof_ts();
1184 }
1185
1186 Template *Template::get_listitem_byIndex(size_t n) const
1187 {
1188 if (templatetype != TEMPLATE_LIST)
1189 FATAL_ERROR("Template::get_listitam_byIndex()");
1190 if (has_permutation) {
1191 size_t nof_ts = u.templates->get_nof_ts(), index = 0;
1192 for (size_t i = 0; i < nof_ts; i++) {
1193 Template *t = u.templates->get_t_byIndex(i);
1194 if (t->templatetype == PERMUTATION_MATCH) {
1195 size_t nof_perm_ts = t->u.templates->get_nof_ts();
1196 if (n < index + nof_perm_ts)
1197 return t->u.templates->get_t_byIndex(n - index);
1198 else index += nof_perm_ts;
1199 } else {
1200 if (n == index) return t;
1201 else index++;
1202 }
1203 }
1204 FATAL_ERROR("Template::get_listitem_byIndex(): index overflow");
1205 return 0;
1206 } else return u.templates->get_t_byIndex(n);
1207 }
1208
1209 /** \todo revise and merge with get_template_refd() */
1210 Template* Template::get_template_refd_last(ReferenceChain *refch)
1211 {
1212 // return this for non-referenced templates
1213 if (templatetype != TEMPLATE_REFD) return this;
1214 // use the cached template if present
1215 else if (u.ref.refd_last) return u.ref.refd_last;
1216 else {
1217 Common::Assignment *t_ass = u.ref.ref->get_refd_assignment();
1218 // escape from invalid recursion loops
1219 if (templatetype != TEMPLATE_REFD) return this;
1220 if (!t_ass) FATAL_ERROR("Template::get_template_refd_last()");
1221 if (t_ass->get_asstype() != Common::Assignment::A_TEMPLATE) {
1222 // return this if the reference does not point to a template
1223 u.ref.refd_last = this;
1224 return u.ref.refd_last;
1225 }
1226 }
1227 // otherwise evaluate the reference
1228 bool destroy_refch;
1229 if (refch) {
1230 refch->mark_state();
1231 destroy_refch = false;
1232 } else {
1233 refch = new ReferenceChain(this, "While searching referenced template");
1234 destroy_refch = true;
1235 }
1236 Template *ret_val;
1237 if (refch->add(get_fullname())) {
1238 Template *t_refd = get_template_refd(refch);
1239 // get_template_refd() may set u.ref.refd_last if there are unfoldable
1240 // sub-references in u.ref.ref
1241 if (!u.ref.refd_last) {
1242 u.ref.refd_last = t_refd->get_template_refd_last(refch);
1243 }
1244 ret_val = u.ref.refd_last;
1245 } else {
1246 // a circular reference was found
1247 set_templatetype(TEMPLATE_ERROR);
1248 ret_val = this;
1249 }
1250 if (destroy_refch) delete refch;
1251 else refch->prev_state();
1252 return ret_val;
1253 }
1254
1255 Template* Template::get_refd_sub_template(Ttcn::FieldOrArrayRefs *subrefs,
1256 bool usedInIsbound,
1257 ReferenceChain *refch)
1258 {
1259 if (!subrefs) return this;
1260 Template *t=this;
1261 for (size_t i=0; i<subrefs->get_nof_refs(); i++) {
1262 if(!t) break;
1263 t=t->get_template_refd_last(refch);
1264 t->set_lowerid_to_ref();
1265 switch(t->templatetype) {
1266 case TEMPLATE_ERROR:
1267 return t;
1268 case TEMPLATE_REFD:
1269 case INDEXED_TEMPLATE_LIST:
1270 // unfoldable stuff
1271 return this;
1272 case SPECIFIC_VALUE:
1273 (void)t->u.specific_value->get_refd_sub_value(
1274 subrefs, i, usedInIsbound, refch); // only to report errors
1275 break;
1276 default:
1277 break;
1278 } // switch
1279 Ttcn::FieldOrArrayRef *ref=subrefs->get_ref(i);
1280 if(ref->get_type() == Ttcn::FieldOrArrayRef::FIELD_REF)
1281 t=t->get_refd_field_template(*ref->get_id(), *ref, usedInIsbound,
1282 refch);
1283 else t=t->get_refd_array_template(ref->get_val(), usedInIsbound, refch);
1284 }
1285 return t;
1286 }
1287
1288 Template* Template::get_template_refd(ReferenceChain *refch)
1289 {
1290 unsigned int const prev_err_count = get_error_count();
1291 if (templatetype != TEMPLATE_REFD)
1292 FATAL_ERROR("Template::get_template_refd()");
1293 // use the cached pointer if it is already set
1294 if (u.ref.refd) return u.ref.refd;
1295 Common::Assignment *ass = u.ref.ref->get_refd_assignment();
1296 if (!ass) FATAL_ERROR("Template::get_template_refd()");
1297 if(ass->get_asstype() == Common::Assignment::A_TEMPLATE) {
1298 FieldOrArrayRefs *subrefs = u.ref.ref->get_subrefs();
1299 Template *asst = ass->get_Template();
1300 Template *t = asst->get_refd_sub_template(
1301 subrefs, u.ref.ref->getUsedInIsbound(), refch);
1302 if (t) {
1303 u.ref.refd = t;
1304 // Why do we not set u.ref.refd_last ?
1305 }
1306 else if (subrefs && subrefs->has_unfoldable_index()) {
1307 // some array indices could not be evaluated
1308 u.ref.refd = this;
1309 u.ref.refd_last = this;
1310 } else if (u.ref.ref->getUsedInIsbound()) {
1311 u.ref.refd = this;
1312 u.ref.refd_last = this;
1313 } else {
1314 // an error was found while resolving sub-references
1315 if (get_error_count() == prev_err_count) {
1316 // it was not reported, report it now
1317 error("Using a template which refers to a non-template is not supported");
1318 asst->note("Workaround: change the right hand side refer to a template");
1319 if (ass->is_local()) {
1320 ass->note("Workaround: change the template definition "
1321 "to a var template");
1322 }
1323 }
1324 set_templatetype(TEMPLATE_ERROR);
1325 return this;
1326 }
1327 } else {
1328 // the reference is unfoldable
1329 u.ref.refd = this;
1330 }
1331 return u.ref.refd;
1332 }
1333
1334 Template* Template::get_refd_field_template(const Identifier& field_id,
1335 const Location& loc, bool usedInIsbound, ReferenceChain *refch)
1336 {
1337 switch (templatetype) {
1338 case OMIT_VALUE:
1339 case ANY_VALUE:
1340 case ANY_OR_OMIT:
1341 case VALUE_LIST:
1342 case COMPLEMENTED_LIST:
1343 // the above template types are valid matching mechanisms,
1344 // but they cannot be sub-referenced
1345 loc.error("Reference to field `%s' of %s `%s'",
1346 field_id.get_dispname().c_str(), get_templatetype_str(),
1347 get_fullname().c_str());
1348 break;
1349 default:
1350 break;
1351 }
1352 if(!my_governor) FATAL_ERROR("Template::get_refd_field_template()");
1353 Type *t = my_governor->get_type_refd_last();
1354 const char *typetype_str="set";
1355 switch(t->get_typetype()) {
1356 case Type::T_ERROR:
1357 // remain silent
1358 return 0;
1359 case Type::T_CHOICE_A:
1360 case Type::T_CHOICE_T:
1361 case Type::T_OPENTYPE:
1362 case Type::T_ANYTYPE:
1363 if (!t->has_comp_withName(field_id)) {
1364 loc.error("Reference to non-existent union field `%s' in type `%s'",
1365 field_id.get_dispname().c_str(), t->get_typename().c_str());
1366 return 0;
1367 } else if (templatetype != NAMED_TEMPLATE_LIST) {
1368 // this is an invalid matching mechanism, the error is already reported
1369//error("invalid matching mechanism (not template list) but %d", templatetype);
1370 return 0;
1371 } else if (u.named_templates->get_nof_nts() != 1) {
1372 // this is an invalid union template (more than one active field)
1373 // the error is already reported
1374//error("invalid union template ");
1375 return 0;
1376 } else {
1377 NamedTemplate *nt = u.named_templates->get_nt_byIndex(0);
1378 if (nt->get_name() != field_id) {
1379 if (!usedInIsbound) {
1380 loc.error("Reference to inactive field `%s' in a template of"
1381 " union type `%s'. The active field is `%s'.",
1382 field_id.get_dispname().c_str(),
1383 t->get_typename().c_str(),
1384 nt->get_name().get_dispname().c_str());
1385 }
1386 return 0;
1387 } else {
1388 // everything is OK
1389 return nt->get_template();
1390 }
1391 }
1392 case Type::T_SEQ_A:
1393 case Type::T_SEQ_T:
1394 typetype_str="record";
1395 // no break
1396 case Type::T_SET_A:
1397 case Type::T_SET_T:
1398 if (!t->has_comp_withName(field_id)) {
1399 loc.error("Reference to non-existent %s field `%s' in type `%s'",
1400 typetype_str, field_id.get_dispname().c_str(),
1401 t->get_typename().c_str());
1402 return 0;
1403 } else if (templatetype != NAMED_TEMPLATE_LIST) {
1404 // this is an invalid matching mechanism
1405 // the error should be already reported
1406 return 0;
1407 } else if (u.named_templates->has_nt_withName(field_id)) {
1408 // the field is found, everything is OK
1409 return u.named_templates->get_nt_byName(field_id)->get_template();
1410 } else if (base_template) {
1411 // take the field from the base template (recursively)
1412 return base_template->get_template_refd_last(refch)
1413 ->get_refd_field_template(field_id, loc, usedInIsbound, refch);
1414 } else {
1415 if (!usedInIsbound) {
1416 // this should not happen unless there is an error
1417 // (e.g. missing field)
1418 loc.error("Reference to an unbound field `%s'",
1419 field_id.get_dispname().c_str());
1420 }
1421 return 0;
1422 }
1423 default:
1424 loc.error("Invalid field reference `%s': type `%s' "
1425 "does not have fields", field_id.get_dispname().c_str(),
1426 t->get_typename().c_str());
1427 return 0;
1428 }
1429 }
1430
1431 Template* Template::get_refd_array_template(Value *array_index,
1432 bool usedInIsbound,
1433 ReferenceChain *refch)
1434 {
1435 switch (templatetype) {
1436 case OMIT_VALUE:
1437 case ANY_VALUE:
1438 case ANY_OR_OMIT:
1439 case VALUE_LIST:
1440 case COMPLEMENTED_LIST:
1441 case SUPERSET_MATCH:
1442 case SUBSET_MATCH:
1443 // the above template types are valid matching mechanisms,
1444 // but they cannot be sub-referenced
1445 array_index->error("Reference with index to an element of %s `%s'",
1446 get_templatetype_str(), get_fullname().c_str());
1447 break;
1448 default:
1449 break;
1450 }
1451 Value *v_index = array_index->get_value_refd_last(refch);
1452 Int index = 0;
1453 bool index_available = false;
1454 if (!v_index->is_unfoldable()) {
1455 if (v_index->get_valuetype() == Value::V_INT) {
1456 index = v_index->get_val_Int()->get_val();
1457 index_available = true;
1458 } else {
1459 array_index->error("An integer value was expected as index");
1460 }
1461 }
1462 if (!my_governor) FATAL_ERROR("Template::get_refd_array_template()");
1463 Type *t = my_governor->get_type_refd_last();
1464 const char *typetype_str="set";
1465 switch(t->get_typetype()) {
1466 case Type::T_ERROR:
1467 // remain silent
1468 return 0;
1469 case Type::T_SEQOF:
1470 typetype_str="record";
1471 // no break
1472 case Type::T_SETOF:
1473 if (index_available) {
1474 if(index < 0) {
1475 array_index->error("A non-negative integer value was expected "
1476 "instead of %s for indexing a template of `%s of' type `%s'",
1477 Int2string(index).c_str(), typetype_str,
1478 t->get_typename().c_str());
1479 return 0;
1480 } else if (templatetype != TEMPLATE_LIST) {
1481 // remain silent the error has been already reported
1482 return 0;
1483 } else {
1484 size_t nof_elements = get_nof_listitems();
1485 if (index >= static_cast<Int>(nof_elements)) {
1486 array_index->error("Index overflow in a template of `%s of' type "
1487 "`%s': the index is %s, but the template has only %lu elements",
1488 typetype_str, t->get_typename().c_str(),
1489 Int2string(index).c_str(), (unsigned long) nof_elements);
1490 return 0;
1491 }
1492 }
1493 } else {
1494 // the index is not available or the error has been reported above
1495 return 0;
1496 }
1497 break;
1498 case Type::T_ARRAY:
1499 if (index_available) {
1500 ArrayDimension *dim = t->get_dimension();
1501 dim->chk_index(v_index, Type::EXPECTED_DYNAMIC_VALUE);
1502 if (templatetype == TEMPLATE_LIST && !dim->get_has_error()) {
1503 // perform the index transformation
1504 index -= dim->get_offset();
1505 // check for index underflow/overflow or too few elements in template
1506 if (index < 0 || index >= static_cast<Int>(get_nof_listitems()))
1507 return 0;
1508 } else {
1509 // remain silent, the error has been already reported
1510 return 0;
1511 }
1512 } else {
1513 // the index is not available or the error has been reported above
1514 return 0;
1515 }
1516 break;
1517 default:
1518 array_index->error("Invalid array element reference: type `%s' cannot "
1519 "be indexed", t->get_typename().c_str());
1520 return 0;
1521 }
1522 Template *ret_val = get_listitem_byIndex(index);
1523 if (ret_val->templatetype == TEMPLATE_NOTUSED) {
1524 if (base_template) {
1525 // take the referred element from the base template
1526 return base_template->get_template_refd_last(refch)
1527 ->get_refd_array_template(v_index, usedInIsbound, refch);
1528 } else {
1529 if(ret_val->get_templatetype() == TEMPLATE_NOTUSED)
1530 error("Not used symbol is not allowed in this context");
1531 return 0;
1532 }
1533 } else return ret_val;
1534 }
1535
1536 bool Template::temps_contains_anyornone_symbol() const
1537 {
1538 switch (templatetype) {
1539 case TEMPLATE_LIST:
1540 case SUPERSET_MATCH:
1541 case SUBSET_MATCH:
1542 case PERMUTATION_MATCH:
1543 break;
1544 default:
1545 FATAL_ERROR("Template::temps_contains_anyornone_symbol()");
1546 }
1547 size_t nof_comps = u.templates->get_nof_ts();
1548 for (size_t i = 0; i < nof_comps; i++) {
1549 Template *t = u.templates->get_t_byIndex(i);
1550 switch (t->templatetype) {
1551 case ANY_OR_OMIT:
1552 return true;
1553 case PERMUTATION_MATCH:
1554 // walk recursively
1555 if (t->temps_contains_anyornone_symbol()) return true;
1556 break;
1557 default:
1558 break;
1559 }
1560 }
1561 return false;
1562 }
1563
1564 size_t Template::get_nof_comps_not_anyornone() const
1565 {
1566 switch (templatetype) {
1567 case TEMPLATE_LIST:
1568 case SUPERSET_MATCH:
1569 case SUBSET_MATCH:
1570 case PERMUTATION_MATCH:
1571 break;
1572 default:
1573 FATAL_ERROR("Template::get_nof_comps_not_anyornone()");
1574 }
1575 size_t ret_val = 0;
1576 size_t nof_comps = u.templates->get_nof_ts();
1577 for (size_t i = 0; i < nof_comps; i++) {
1578 Template *t = u.templates->get_t_byIndex(i);
1579 switch (t->templatetype) {
1580 case ANY_OR_OMIT:
1581 // do not count it
1582 break;
1583 case PERMUTATION_MATCH:
1584 // walk recursively
1585 ret_val += t->get_nof_comps_not_anyornone();
1586 break;
1587 default:
1588 // other types are counted as 1
1589 ret_val++;
1590 break;
1591 }
1592 }
1593 return ret_val;
1594 }
1595
1596 bool Template::pattern_contains_anyornone_symbol() const
1597 {
1598 switch (templatetype) {
1599 case BSTR_PATTERN:
1600 case HSTR_PATTERN:
1601 case OSTR_PATTERN:
1602 return u.pattern->find('*') < u.pattern->size();
1603 case CSTR_PATTERN:
1604 case USTR_PATTERN:
1605 return true;
1606 default:
1607 FATAL_ERROR("Template::pattern_contains_anyornone_symbol()");
1608 return false;
1609 }
1610 }
1611
1612 size_t Template::get_min_length_of_pattern() const
1613 {
1614 size_t ret_val = 0;
1615 switch (templatetype) {
1616 case BSTR_PATTERN:
1617 case HSTR_PATTERN: {
1618 size_t pattern_len = u.pattern->size();
1619 const char *pattern_ptr = u.pattern->c_str();
1620 for (size_t i = 0; i < pattern_len; i++)
1621 if (pattern_ptr[i] != '*') ret_val++;
1622 break; }
1623 case OSTR_PATTERN: {
1624 size_t pattern_len = u.pattern->size();
1625 const char *pattern_ptr = u.pattern->c_str();
1626 for (size_t i = 0; i < pattern_len; i++) {
1627 switch (pattern_ptr[i]) {
1628 case '*':
1629 // do not count
1630 break;
1631 case '?':
1632 // count as 1
1633 ret_val++;
1634 break;
1635 default:
1636 // count as 1 and skip over the next hex digit
1637 ret_val++;
1638 i++;
1639 }
1640 }
1641 break; }
1642 case CSTR_PATTERN:
1643 case USTR_PATTERN:
1644 break;
1645 default:
1646 FATAL_ERROR("Template::get_min_length_of_pattern()");
1647 }
1648 return ret_val;
1649 }
1650
1651 bool Template::is_Value() const
1652 {
1653 if (length_restriction || is_ifpresent) return false;
1654 switch (templatetype) {
1655 case TEMPLATE_ERROR:
1656 case TEMPLATE_NOTUSED:
1657 case OMIT_VALUE:
1658 return true;
1659 case SPECIFIC_VALUE:
1660 if(u.specific_value->get_valuetype() == Value::V_INVOKE) {
1661 Type *t = u.specific_value
1662 ->get_invoked_type(Type::EXPECTED_DYNAMIC_VALUE);
1663 if(t && t->get_type_refd_last()->get_typetype() == Type::T_FUNCTION &&
1664 t->get_type_refd_last()->get_returns_template()) return false;
1665 }
1666 return true;
1667 case TEMPLATE_LIST: {
1668 Templates *ts = u.templates;
1669 for (size_t i = 0; i < ts->get_nof_ts(); i++)
1670 if (!ts->get_t_byIndex(i)->is_Value()) return false;
1671 return true;
1672 }
1673 case NAMED_TEMPLATE_LIST: {
1674 NamedTemplates *ts = u.named_templates;
1675 for (size_t i = 0;i < ts->get_nof_nts(); i++)
1676 if (!ts->get_nt_byIndex(i)->get_template()->is_Value()) return false;
1677 return true;
1678 }
1679 case INDEXED_TEMPLATE_LIST: {
1680 IndexedTemplates *ts = u.indexed_templates;
1681 for (size_t i = 0; i < ts->get_nof_its(); i++)
1682 if (!ts->get_it_byIndex(i)->get_template()->is_Value()) return false;
1683 return true;
1684 }
1685 case TEMPLATE_REFD: {
1686 Common::Assignment *ass = u.ref.ref->get_refd_assignment();
1687 switch (ass->get_asstype()) {
1688 case Common::Assignment::A_EXT_CONST:
1689 case Common::Assignment::A_PAR_VAL:
1690 case Common::Assignment::A_PAR_VAL_IN:
1691 case Common::Assignment::A_PAR_VAL_OUT:
1692 case Common::Assignment::A_PAR_VAL_INOUT:
1693 case Common::Assignment::A_VAR:
1694 return true;
1695 default:
1696 return false;
1697 }
1698 }
1699 default:
1700 return false;
1701 }
1702 }
1703
1704 Value *Template::get_Value()
1705 {
1706 Value *ret_val;
1707 switch(templatetype) {
1708 case TEMPLATE_ERROR:
1709 ret_val = new Value(Value::V_ERROR);
1710 break;
1711 case TEMPLATE_NOTUSED:
1712 ret_val = new Value(Value::V_NOTUSED);
1713 break;
1714 case OMIT_VALUE:
1715 ret_val = new Value(Value::V_OMIT);
1716 break;
1717 case SPECIFIC_VALUE:
1718 ret_val = u.specific_value;
1719 u.specific_value = 0;
1720 set_templatetype(TEMPLATE_ERROR);
1721 return ret_val;
1722 case TEMPLATE_LIST: {
1723 Values *vs = new Values;
1724 size_t nof_ts = u.templates->get_nof_ts();
1725 Type* gov = 0;
1726 for (size_t i = 0; i < nof_ts; i++) {
1727 Value* v = u.templates->get_t_byIndex(i)->get_Value();
1728 if (!gov) gov = v->get_my_governor();
1729 vs->add_v(v);
1730 }
1731 ret_val = new Value(Value::V_SEQOF, vs);
1732 if (gov) ret_val->set_my_governor(gov->get_parent_type());
1733 break; }
1734 case NAMED_TEMPLATE_LIST: {
1735 NamedValues *nvs = new NamedValues;
1736 size_t nof_nts = u.named_templates->get_nof_nts();
1737 Type* gov = 0;
1738 for (size_t i = 0; i < nof_nts; i++) {
1739 NamedTemplate *nt = u.named_templates->get_nt_byIndex(i);
1740 Value* v = nt->get_template()->get_Value();
1741 if (!gov) gov = v->get_my_governor();
1742 NamedValue *nv = new NamedValue(nt->get_name().clone(), v);
1743 nv->set_location(*nt);
1744 nvs->add_nv(nv);
1745 }
1746 ret_val = new Value(Value::V_SEQ, nvs);
1747 if (gov) ret_val->set_my_governor(gov->get_parent_type());
1748 break; }
1749 case INDEXED_TEMPLATE_LIST: {
1750 Values *ivs = new Values(true);
1751 size_t nof_its = u.indexed_templates->get_nof_its();
1752 Type* gov = 0;
1753 for (size_t i = 0; i < nof_its; i++) {
1754 IndexedTemplate *it = u.indexed_templates->get_it_byIndex(i);
1755 Value* v = it->get_template()->get_Value();
1756 if (!gov) gov = v->get_my_governor();
1757 IndexedValue *iv = new IndexedValue(it->get_index().clone(), v);
1758 iv->set_location(*it);
1759 ivs->add_iv(iv);
1760 }
1761 ret_val = new Value(Value::V_SEQOF, ivs);
1762 if (gov) ret_val->set_my_governor(gov->get_parent_type());
1763 break; }
1764 default:
1765 FATAL_ERROR("Template::get_Value()");
1766 ret_val = 0;
1767 break;
1768 }
1769 ret_val->set_location(*this);
1770 ret_val->set_my_scope(get_my_scope());
1771 ret_val->set_fullname(get_fullname());
1772 return ret_val;
1773 }
1774
1775 bool Template::is_Ref() const
1776 {
1777 if (length_restriction || is_ifpresent || templatetype != SPECIFIC_VALUE)
1778 return false;
1779 Value *v = u.specific_value;
1780 switch (v->get_valuetype()) {
1781 case Value::V_UNDEF_LOWERID:
1782 return true;
1783 case Value::V_REFD:
1784 if (dynamic_cast<Ref_base*>(v->get_reference())) return true;
1785 else return false;
1786 default:
1787 return false;
1788 }
1789 }
1790
1791 Ref_base *Template::get_Ref()
1792 {
1793 if (templatetype != SPECIFIC_VALUE)
1794 FATAL_ERROR("Template::get_Ref()");
1795 return u.specific_value->steal_ttcn_ref_base();
1796 }
1797
1798 void Template::chk_recursions(ReferenceChain& refch)
1799 {
1800 if (recurs_checked) return;
1801 Template *t = this;
1802 for ( ; ; ) {
1803 if (t->templatetype == SPECIFIC_VALUE) break;
1804 else if (!refch.add(t->get_fullname())) goto end;
1805 else if (t->templatetype != TEMPLATE_REFD) break;
1806 ActualParList *parlist = t->u.ref.ref->get_parlist();
1807 if (parlist) parlist->chk_recursions(refch);
1808 Template *t_refd = t->get_template_refd(&refch);
1809 if (t_refd == t) break;
1810 else t = t_refd;
1811 }
1812 t->set_lowerid_to_ref();
1813 switch (t->templatetype) {
1814 case SPECIFIC_VALUE:
1815 t->u.specific_value->chk_recursions(refch);
1816 break;
1817 case TEMPLATE_LIST:
1818 case VALUE_LIST:
1819 case COMPLEMENTED_LIST:
1820 case SUPERSET_MATCH:
1821 case SUBSET_MATCH:
1822 case PERMUTATION_MATCH:
1823 for (size_t i = 0; i < t->u.templates->get_nof_ts(); i++) {
1824 refch.mark_state();
1825 t->u.templates->get_t_byIndex(i)->chk_recursions(refch);
1826 refch.prev_state();
1827 }
1828 break;
1829 case NAMED_TEMPLATE_LIST:
1830 for (size_t i = 0; i < t->u.named_templates->get_nof_nts(); i++) {
1831 refch.mark_state();
1832 t->u.named_templates->get_nt_byIndex(i)
1833 ->get_template()->chk_recursions(refch);
1834 refch.prev_state();
1835 }
1836 break;
1837 case INDEXED_TEMPLATE_LIST:
1838 for (size_t i = 0; i < t->u.indexed_templates->get_nof_its(); i++) {
1839 refch.mark_state();
1840 t->u.indexed_templates->get_it_byIndex(i)
1841 ->get_template()->chk_recursions(refch);
1842 refch.prev_state();
1843 }
1844 break;
1845 case CSTR_PATTERN:
1846 case USTR_PATTERN:
1847 t->u.pstring->chk_recursions(refch);
1848 break;
1849 default:
1850 break;
1851 }
1852end:
1853 recurs_checked = true;
1854 }
1855
1856 void Template::chk_specific_value(bool allow_omit)
1857 {
1858 Template *t = get_template_refd_last();
1859 if (!allow_omit && t->templatetype==OMIT_VALUE) {
1860 t->error("A specific value was expected instead of omit");
1861 }
1862 chk_specific_value_generic();
1863 }
1864
1865 void Template::chk_specific_value_generic()
1866 {
1867 if (specific_value_checked) return;
1868 Template *t = get_template_refd_last();
1869 if (t->specific_value_checked) return;
1870 switch (t->templatetype) {
1871 case TEMPLATE_ERROR:
1872 case TEMPLATE_NOTUSED:
1873 case TEMPLATE_REFD: // unfoldable reference
1874 break;
1875 case SPECIFIC_VALUE:
1876 if(u.specific_value->get_valuetype() == Value::V_INVOKE) {
1877 Type *t_type = u.specific_value
1878 ->get_invoked_type(Type::EXPECTED_DYNAMIC_VALUE);
1879 if(t_type && t_type->get_type_refd_last()->get_returns_template()) {
1880 set_templatetype(TEMPLATE_INVOKE);
1881 chk_invoke();
1882 }
1883 }
1884 break;
1885 case TEMPLATE_INVOKE:
1886 chk_invoke();
1887 break;
1888 case TEMPLATE_LIST:
1889 for (size_t i = 0; i < t->u.templates->get_nof_ts(); i++)
1890 t->u.templates->get_t_byIndex(i)->chk_specific_value_generic();
1891 break;
1892 case NAMED_TEMPLATE_LIST:
1893 for (size_t i = 0; i < t->u.named_templates->get_nof_nts(); i++)
1894 t->u.named_templates->get_nt_byIndex(i)
1895 ->get_template()->chk_specific_value_generic();
1896 break;
1897 case INDEXED_TEMPLATE_LIST:
1898 for (size_t i = 0; i < t->u.indexed_templates->get_nof_its(); i++)
1899 t->u.indexed_templates->get_it_byIndex(i)
1900 ->get_template()->chk_specific_value_generic();
1901 break;
1902 case OMIT_VALUE:
1903 break;
1904 default:
1905 t->error("A specific value was expected instead of %s",
1906 t->get_templatetype_str());
1907 break;
1908 }
1909 t->specific_value_checked = true;
1910 specific_value_checked = true;
1911 }
1912
1913 void Template::chk_invoke()
1914 {
1915 if(templatetype != TEMPLATE_INVOKE) FATAL_ERROR("Template::chk_invoke()");
1916 if(!u.invoke.t_list) return; //already checked
1917 Error_Context cntxt(this, "In `apply()' operation");
1918 Type *t = u.invoke.v->get_expr_governor_last();
1919 if (t) {
1920 if (t->get_typetype() != Type::T_FUNCTION) {
1921 u.invoke.v->error("A value of type function was expected in the "
1922 "argument instead of `%s'", t->get_typename().c_str());
1923 set_templatetype(TEMPLATE_ERROR);
1924 return;
1925 }
1926 } else {
1927 if (u.invoke.v->get_valuetype() != Value::V_ERROR)
1928 u.invoke.v->error("A value of type function was expected in the "
1929 "argument");
1930 set_templatetype(TEMPLATE_ERROR);
1931 return;
1932 }
1933 my_scope->chk_runs_on_clause(t, *this, "call");
1934 Ttcn::FormalParList *fp_list = t->get_fat_parameters();
1935 Ttcn::ActualParList *parlist = new Ttcn::ActualParList;
1936 bool is_erroneous = fp_list->fold_named_and_chk(u.invoke.t_list,parlist);
1937 delete u.invoke.t_list;
1938 u.invoke.t_list = 0;
1939 if(is_erroneous) {
1940 delete parlist;
1941 u.invoke.ap_list = 0;
1942 } else {
1943 parlist->set_fullname(get_fullname());
1944 parlist->set_my_scope(get_my_scope());
1945 u.invoke.ap_list = parlist;
1946 }
1947 }
1948
1949 Templates *Template::harbinger(Template *t, bool from_permutation, bool killer)
1950 {
1951 Templates *new_templates = new Templates;
1952 switch (t->u.all_from->templatetype) {
1953 case SPECIFIC_VALUE: {
1954 Value *innerv = t->u.all_from->get_specific_value();
1955 //if (v->get_valuetype() == Value::V_UNDEF_LOWERID)
1956 innerv->set_lowerid_to_ref();
1957 // should be a ref now
1958 bool can_flatten = true;
1959 Common::Reference *ref = innerv->get_reference();
1960 if (dynamic_cast<Ttcn::Ref_pard*>(ref)) {
1961 // Cannot flatten at compile time if the template has parameters.
1962 can_flatten = false;
1963 }
1964 Common::Assignment *ass = ref->get_refd_assignment();
1965 if (ass == NULL) { // perhaps erroneous
1966 break;
1967 }
1968
1969 Common::Assignment::asstype_t asst = ass->get_asstype();
1970 switch (asst) {
1971 case Common::Assignment::A_MODULEPAR_TEMP:
1972 case Common::Assignment::A_VAR_TEMPLATE:
1973 case Common::Assignment::A_FUNCTION_RTEMP:
1974 case Common::Assignment::A_EXT_FUNCTION_RTEMP: {
1975 // Cannot flatten at compile time
1976 delete new_templates;
1977 new_templates = 0;
1978 break; }
1979
1980 case Common::Assignment::A_TEMPLATE: {
1981 Template *tpl = ass->get_Template();
1982 // tpl is the template whose name appears after the "all from"
1983 Template::templatetype_t tpltt = tpl->get_templatetype();
1984 switch (tpltt) {
1985 case TEMPLATE_REFD: {
1986 delete new_templates;
1987 new_templates = 0;
1988 killer = false;
1989 break; }
1990 case TEMPLATE_LIST:
1991 // ALL_FROM ?
1992 case VALUE_LIST: {
1993 size_t nvl = tpl->get_nof_comps();
1994 for (size_t ti = 0; ti < nvl; ++ti) {
1995 Template *orig = tpl->get_temp_byIndex(ti);
1996 switch (orig->templatetype) {
1997 case SPECIFIC_VALUE: {
1998 if (can_flatten) {
1999 Value *val = orig->get_specific_value();
2000 if (val->get_valuetype() == Value::V_REFD) {
2001 if (dynamic_cast<Ttcn::Ref_pard*>(val->get_reference())) {
2002 // Cannot flatten at compile time if one of the values or templates has parameters.
2003 can_flatten = false;
2004 }
2005 }
2006 }
2007 break; }
2008 case ANY_OR_OMIT:
2009 if (from_permutation) {
2010 break; // AnyElementOrNone allowed in "all from" now
2011 }
2012 // no break
2013 case PERMUTATION_MATCH:
2014 t->error("'all from' can not refer to permutation or AnyElementsOrNone");
2015 t->set_templatetype(TEMPLATE_ERROR);
2016 default:
2017 break;
2018 }
2019 }
2020 if (can_flatten) {
2021 for (size_t ti = 0; ti < nvl; ++ti) {
2022 Template *orig = tpl->get_temp_byIndex(ti);
2023 Template *copy = orig->clone();
2024 copy->set_my_scope(orig->get_my_scope());
2025 new_templates->add_t(copy);
2026 }
2027 }
2028 else {
2029 // Cannot flatten at compile time
2030 delete new_templates;
2031 new_templates = 0;
2032 killer = false;
2033 }
2034 break; }
2035
2036 case NAMED_TEMPLATE_LIST: {
2037 size_t nvl = tpl->get_nof_comps();
2038 for (size_t ti = 0; ti < nvl; ++ti) {
2039 NamedTemplate *orig = tpl->get_namedtemp_byIndex(ti);
2040 switch (orig->get_template()->get_templatetype()) {
2041 case ANY_OR_OMIT:
2042 break;
2043 case PERMUTATION_MATCH:
2044 t->error("'all from' can not refer to permutation or AnyElementsOrNone");
2045 t->set_templatetype(TEMPLATE_ERROR);
2046 default:
2047 break;
2048 }
2049 }
2050 delete new_templates;
2051 new_templates = 0;
2052 killer = false;
2053 break; }
2054
2055 case ANY_VALUE:
2056 case ANY_OR_OMIT:
2057 tpl->error("Matching mechanism can not be used as target of 'all from'");
2058 break;
2059 default:
2060 FATAL_ERROR("harbinger");
2061 break;
2062 }
2063
2064 if (killer) delete t;
2065 break; }
2066
2067
2068 case Common::Assignment::A_CONST: { // all from a constant definition
2069 Common::Value *val = ass->get_Value();
2070 switch (val->get_valuetype()) {
2071 case Common::Value::V_SEQOF: case Common::Value::V_SETOF:
2072 case Common::Value::V_ARRAY: {
2073 const size_t ncomp = val->get_nof_comps();
2074 for (size_t i = 0; i < ncomp; ++i) {
2075 Value *v = val->get_comp_byIndex(i);
2076 Template *newt = new Template(v->clone());
2077 new_templates->add_t(newt);
2078 }
2079 break; }
2080
2081 default:
2082 val->error("A constant of type `%s' can not be used as target of 'all from'",
2083 val->get_my_governor()->get_typename().c_str());
2084 break;
2085 } // switch(valuetype)
2086 if (killer) delete t;
2087 break; }
2088
2089 case Common::Assignment::A_PAR_TEMPL_IN:
2090 case Common::Assignment::A_PAR_TEMPL_INOUT:
2091//TODO: flatten if the actual par is const template
2092 case Common::Assignment::A_MODULEPAR: // all from a module parameter
2093 case Common::Assignment::A_VAR: // all from a variable
2094 case Common::Assignment::A_PAR_VAL_IN:
2095 case Common::Assignment::A_PAR_VAL_INOUT: {
2096 delete new_templates; // cannot flatten at compile time
2097 new_templates = 0;
2098 break; }
2099
2100 default:
2101 FATAL_ERROR("harbinger asst %d", asst);
2102 break;
2103 }
2104 break; }
2105
2106 case ALL_FROM:
2107 FATAL_ERROR("unexpected all from inside all from");
2108 break;
2109 default:
2110 FATAL_ERROR("tt %d", t->u.all_from->templatetype);
2111 }
2112
2113 return new_templates;
2114 }
2115
2116 bool Template::flatten(bool from_permutation)
2117 {
2118 switch (templatetype) {
2119 case TEMPLATE_LIST:
2120 case VALUE_LIST:
2121 case COMPLEMENTED_LIST:
2122 case SUPERSET_MATCH:
2123 case SUBSET_MATCH:
2124 case PERMUTATION_MATCH: {
2125 size_t num_t = u.templates->get_nof_ts(); // one of these is the "all from"
2126 Templates *new_templates = new Templates;
2127 for (size_t i = 0; i < num_t; ++i) {
2128 Template *& t = u.templates->get_t_byIndex(i);
2129 // the element in the (,,,)
2130 switch (t->templatetype) {
2131 case VALUE_LIST_ALL_FROM: {
2132 // the all from from something like subset(1, (all from...), 99)
2133 // value list: one out of many possible values^^^^^^^^^^^^^
2134 Location tloc(*t); // save the location info
2135 string tname(t->get_fullname());
2136 Templates *ha = harbinger(t, from_permutation, true);
2137 if (ha) {
2138 // Don't touch t from now on, it might have been deleted!
2139 Template *qq = new Template(VALUE_LIST, ha);
2140 qq->set_location(tloc);
2141 qq->set_fullname(tname + ".all_from");
2142 new_templates->add_t(qq);
2143 }
2144 else {
2145 new_templates->add_t(t); // transfer it unchanged
2146 flattened = false;
2147 }
2148 break; }
2149
2150 case ALL_FROM: { // subset(1, all from ..., 99)
2151 // some number of elements--^^^^^^^^^^^^
2152 if (t->checked) FATAL_ERROR("too late");
2153 Templates *af = harbinger(t, from_permutation, true);
2154 if (af) {
2155 for (size_t a = 0, num = af->get_nof_ts(); a < num; ++a) {
2156 Template *& t2 = af->get_t_byIndex(a);
2157 new_templates->add_t(t2);
2158 t2 = 0; // take it away from its current owner
2159 }
2160 delete af;
2161 }
2162 else {
2163 new_templates->add_t(t); // transfer it unchanged
2164 flattened = false;
2165 }
2166 break; } // case ALL_FROM
2167
2168 case PERMUTATION_MATCH: {
2169 // permut inside a value list: { 1, permut(), 2 }
2170 flattened &= t->flatten(true);
2171 new_templates->add_t(t);
2172 break; }
2173 case TEMPLATE_LIST:
2174 // case VALUE_LIST:
2175 // case COMPLEMENTED_LIST:
2176 // case SUPERSET_MATCH:
2177 // case SUBSET_MATCH:
2178 {
2179 flattened &=t->flatten(false);
2180 new_templates->add_t(t);
2181 break; }
2182 default:
2183 new_templates->add_t(t);
2184 }
2185
2186 t = 0; // take it away from the original
2187 }
2188 delete u.templates;
2189 u.templates = new_templates;
2190//printf("!!! flattened from %lu to %lu at line %d\n",
2191// (unsigned long)num_t, (unsigned long)new_templates->get_nof_ts(),
2192// get_first_line());
2193
2194 break; }
2195
2196 case VALUE_LIST_ALL_FROM: {
2197 Templates *new_templates = harbinger(this, from_permutation, false);
2198 if (new_templates) {
2199 delete u.all_from;
2200 // now we can change the type
2201 templatetype = VALUE_LIST;
2202 u.templates = new_templates;
2203 }
2204 break; }
2205
2206 case TEMPLATE_ERROR: case TEMPLATE_NOTUSED:
2207 case OMIT_VALUE: case ANY_VALUE: case ANY_OR_OMIT:
2208 case SPECIFIC_VALUE:
2209 case TEMPLATE_REFD: case TEMPLATE_INVOKE:
2210 case NAMED_TEMPLATE_LIST: case INDEXED_TEMPLATE_LIST:
2211 case VALUE_RANGE:
2212 case ALL_FROM:
2213 case BSTR_PATTERN: case HSTR_PATTERN: case OSTR_PATTERN:
2214 case CSTR_PATTERN: case USTR_PATTERN:
2215 break; // NOP
2216 }
2217
2218 set_fullname(get_fullname()); // recompute fullname (esp. for components)
2219 set_my_scope(get_my_scope());
2220
2221 return flattened;
2222 }
2223
2224 const char* Template::get_restriction_name(template_restriction_t tr)
2225 {
2226 switch (tr) {
2227 case TR_NONE:
2228 break;
2229 case TR_OMIT:
2230 return "omit";
2231 case TR_VALUE:
2232 return "value";
2233 case TR_PRESENT:
2234 return "present";
2235 default:
2236 FATAL_ERROR("Template::get_restriction_name()");
2237 }
2238 return "";
2239 }
2240
2241 template_restriction_t Template::get_sub_restriction(
2242 template_restriction_t tr, Ref_base* ref)
2243 {
2244 if (!ref) FATAL_ERROR("Template::get_sub_restriction()");
2245 if (!ref->get_subrefs()) return tr;
2246 bool is_optional = true; // the referenced field is on an optional path
2247 Common::Assignment* ass = ref->get_refd_assignment();
2248 if (ass) {
2249 Type* t = ass->get_Type();
2250 if (t) {
2251 ReferenceChain refch(ref, "While checking template restriction");
2252 t = t->get_field_type(ref->get_subrefs(), Type::EXPECTED_TEMPLATE,
2253 &refch, true);
2254 if (t) is_optional = false;
2255 }
2256 }
2257 switch (tr) {
2258 case TR_NONE:
2259 return TR_NONE;
2260 case TR_OMIT:
2261 return TR_OMIT;
2262 case TR_VALUE:
2263 return is_optional ? TR_OMIT : TR_VALUE;
2264 case TR_PRESENT:
2265 return is_optional ? TR_NONE : TR_PRESENT;
2266 default:
2267 FATAL_ERROR("Template::get_sub_restriction()");
2268 }
2269 return tr;
2270 }
2271
2272 bool Template::is_less_restrictive(template_restriction_t needed_tr,
2273 template_restriction_t refd_tr)
2274 {
2275 switch (needed_tr) {
2276 case TR_NONE:
2277 return false;
2278 case TR_VALUE:
2279 return refd_tr!=TR_VALUE;
2280 case TR_OMIT:
2281 return refd_tr!=TR_VALUE && refd_tr!=TR_OMIT;
2282 case TR_PRESENT:
2283 return refd_tr!=TR_VALUE && refd_tr!=TR_PRESENT;
2284 default:
2285 FATAL_ERROR("Template::is_less_restrictive()");
2286 }
2287 return true;
2288 }
2289
2290 char* Template::generate_restriction_check_code(char* str, const char* name,
2291 template_restriction_t tr)
2292 {
2293 const char* tr_name;
2294 switch (tr) {
2295 case TR_NONE:
2296 return str;
2297 case TR_OMIT:
2298 tr_name = "TR_OMIT";
2299 break;
2300 case TR_VALUE:
2301 tr_name = "TR_VALUE";
2302 break;
2303 case TR_PRESENT:
2304 tr_name = "TR_PRESENT";
2305 break;
2306 default:
2307 FATAL_ERROR("Template::generate_restriction_check_code()");
2308 }
2309 return mputprintf(str, "%s.check_restriction(%s);\n", name, tr_name);
2310 }
2311
2312 // embedded templates -> check needed only for case of TR_OMIT
2313 bool Template::chk_restriction_named_list(const char* definition_name,
2314 map<string, void>& checked_map, size_t needed_checked_cnt)
2315 {
2316 bool needs_runtime_check = false;
2317 if (checked_map.size()>=needed_checked_cnt) return needs_runtime_check;
2318 switch (templatetype) {
2319 case NAMED_TEMPLATE_LIST:
2320 for (size_t i = 0;i < u.named_templates->get_nof_nts(); i++) {
2321 Template* tmpl = u.named_templates->get_nt_byIndex(i)->get_template();
2322 const string& name =
2323 u.named_templates->get_nt_byIndex(i)->get_name().get_name();
2324 if (!checked_map.has_key(name)) {
2325 bool nrc = tmpl->chk_restriction(definition_name, TR_OMIT);
2326 needs_runtime_check = needs_runtime_check || nrc;
2327 checked_map.add(name, 0);
2328 }
2329 }
2330 if (base_template) {
2331 bool nrc = base_template->chk_restriction_named_list(definition_name,
2332 checked_map, needed_checked_cnt);
2333 needs_runtime_check = needs_runtime_check || nrc;
2334 }
2335 break;
2336 case ANY_VALUE:
2337 case ANY_OR_OMIT:
2338 error("Restriction on %s does not allow usage of %s",
2339 definition_name, get_templatetype_str());
2340 break;
2341 default:
2342 // others will be converted to specific value
2343 break;
2344 }
2345 return needs_runtime_check;
2346 }
2347
2348 bool Template::chk_restriction_refd(const char* definition_name,
2349 template_restriction_t template_restriction)
2350 {
2351 bool needs_runtime_check = false;
2352 Common::Assignment* ass = u.ref.ref->get_refd_assignment();
2353 if (!ass) FATAL_ERROR("Template::chk_restriction_refd()");
2354 // get the restriction on the referenced template
2355 template_restriction_t refd_tr = TR_NONE;
2356 bool is_var_template = false;
2357 switch (ass->get_asstype()) {
2358 case Common::Assignment::A_TEMPLATE: {
2359 Template* t_last = get_template_refd_last();
2360 if (t_last!=this) {
2361 bool nrc = t_last->chk_restriction(definition_name,
2362 template_restriction);
2363 needs_runtime_check = needs_runtime_check || nrc;
2364 }
2365 break; }
2366 case Common::Assignment::A_MODULEPAR_TEMP:
2367 is_var_template = true;
2368 refd_tr = TR_NONE; // can't place restriction on this thing
2369 break;
2370 case Common::Assignment::A_VAR_TEMPLATE: {
2371 Def_Var_Template* dvt = dynamic_cast<Def_Var_Template*>(ass);
2372 if (!dvt) FATAL_ERROR("Template::chk_restriction_refd()");
2373 is_var_template = true;
2374 refd_tr = dvt->get_template_restriction();
2375 break; }
2376 case Common::Assignment::A_EXT_FUNCTION_RTEMP:
2377 // we do not trust external functions because there is no generated
2378 // restriction check on their return value
2379 if (template_restriction!=TR_NONE) needs_runtime_check = true;
2380 // no break
2381 case Common::Assignment::A_FUNCTION_RTEMP: {
2382 Def_Function_Base* dfb = dynamic_cast<Def_Function_Base*>(ass);
2383 if (!dfb) FATAL_ERROR("Template::chk_restriction_refd()");
2384 is_var_template = true;
2385 refd_tr = dfb->get_template_restriction();
2386 break; }
2387 case Common::Assignment::A_PAR_TEMPL_IN:
2388 case Common::Assignment::A_PAR_TEMPL_OUT:
2389 case Common::Assignment::A_PAR_TEMPL_INOUT: {
2390 FormalPar* fp = dynamic_cast<FormalPar*>(ass);
2391 if (!fp) FATAL_ERROR("Template::chk_restriction_refd()");
2392 is_var_template = true;
2393 refd_tr = fp->get_template_restriction();
2394 break; }
2395 default:
2396 break;
2397 }
2398 if (is_var_template) {
2399 // check the restriction
2400 refd_tr = get_sub_restriction(refd_tr, u.ref.ref);
2401 // if restriction not satisfied issue warning
2402 if (is_less_restrictive(template_restriction, refd_tr)) {
2403 needs_runtime_check = true;
2404 warning("Inadequate restriction on the referenced %s `%s', this may "
2405 "cause a dynamic test case error at runtime", ass->get_assname(),
2406 u.ref.ref->get_dispname().c_str());
2407 ass->note("Referenced %s is here", ass->get_assname());
2408 }
2409 }
2410 return needs_runtime_check;
2411 }
2412
2413 bool Template::chk_restriction(const char* definition_name,
2414 template_restriction_t template_restriction)
2415 {
2416 bool needs_runtime_check = false;
2417 switch (template_restriction) {
2418 case TR_NONE:
2419 break;
2420 case TR_OMIT:
2421 case TR_VALUE:
2422 if (length_restriction)
2423 error("Restriction on %s does not allow usage of length restriction",
2424 definition_name);
2425 if (is_ifpresent)
2426 error("Restriction on %s does not allow usage of `ifpresent'",
2427 definition_name);
2428 switch(templatetype) {
2429 case TEMPLATE_ERROR:
2430 break;
2431 case TEMPLATE_NOTUSED:
2432 if (base_template) {
2433 bool nrc = base_template->chk_restriction(definition_name,
2434 template_restriction);
2435 needs_runtime_check = needs_runtime_check || nrc;
2436 }
2437 else needs_runtime_check = true;
2438 break;
2439 case SPECIFIC_VALUE:
2440 case TEMPLATE_INVOKE:
2441 break;
2442 case TEMPLATE_REFD: {
2443 bool nrc = chk_restriction_refd(definition_name, template_restriction);
2444 needs_runtime_check = needs_runtime_check || nrc;
2445 } break;
2446 case TEMPLATE_LIST:
2447 for (size_t i = 0; i < u.templates->get_nof_ts(); i++) {
2448 bool nrc = u.templates->get_t_byIndex(i)->
2449 chk_restriction(definition_name, TR_OMIT);
2450 needs_runtime_check = needs_runtime_check || nrc;
2451 }
2452 break;
2453 case NAMED_TEMPLATE_LIST: {
2454 map<string, void> checked_map;
2455 size_t needed_checked_cnt = 0;
2456 if (base_template && my_governor) {
2457 switch (my_governor->get_typetype()) {
2458 case Type::T_SEQ_A:
2459 case Type::T_SEQ_T:
2460 case Type::T_SET_A:
2461 case Type::T_SET_T:
2462 case Type::T_SIGNATURE:
2463 needed_checked_cnt = my_governor->get_nof_comps();
2464 break;
2465 default:
2466 break;
2467 }
2468 }
2469 for (size_t i = 0;i < u.named_templates->get_nof_nts(); i++) {
2470 bool nrc = u.named_templates->get_nt_byIndex(i)->get_template()->
2471 chk_restriction(definition_name, TR_OMIT);
2472 needs_runtime_check = needs_runtime_check || nrc;
2473 if (needed_checked_cnt)
2474 checked_map.add(
2475 u.named_templates->get_nt_byIndex(i)->get_name().get_name(), 0);
2476 }
2477 if (needed_checked_cnt) {
2478 bool nrc = base_template->chk_restriction_named_list(definition_name,
2479 checked_map, needed_checked_cnt);
2480 needs_runtime_check = needs_runtime_check || nrc;
2481 checked_map.clear();
2482 }
2483 } break;
2484 case INDEXED_TEMPLATE_LIST:
2485 for (size_t i = 0; i < u.indexed_templates->get_nof_its(); i++) {
2486 bool nrc = u.indexed_templates->get_it_byIndex(i)->get_template()->
2487 chk_restriction(definition_name, TR_OMIT);
2488 needs_runtime_check = needs_runtime_check || nrc;
2489 }
2490 needs_runtime_check = true; // only basic check, needs runtime check
2491 break;
2492 case OMIT_VALUE:
2493 if (template_restriction==TR_OMIT) break;
2494 // Else restriction is TR_VALUE, but template type is OMIT:
2495 // fall through to error.
2496 default:
2497 error("Restriction on %s does not allow usage of %s",
2498 definition_name, get_templatetype_str());
2499 break;
2500 }
2501 break;
2502 case TR_PRESENT:
2503 if (is_ifpresent)
2504 error("Restriction on %s does not allow usage of `ifpresent'",
2505 definition_name);
2506 switch(templatetype) {
2507 case TEMPLATE_REFD: {
2508 bool nrc = chk_restriction_refd(definition_name, template_restriction);
2509 needs_runtime_check = needs_runtime_check || nrc;
2510 } break;
2511 case VALUE_LIST:
2512 for (size_t i = 0; i < u.templates->get_nof_ts(); i++) {
2513 bool nrc = u.templates->get_t_byIndex(i)->
2514 chk_restriction(definition_name, template_restriction);
2515 needs_runtime_check = needs_runtime_check || nrc;
2516 }
2517 break;
2518 case COMPLEMENTED_LIST: {
2519 // some basic check, always needs runtime check
2520 needs_runtime_check = true;
2521 bool has_any_or_omit = false;
2522 for (size_t i = 0; i < u.templates->get_nof_ts(); i++) {
2523 templatetype_t item_templatetype =
2524 u.templates->get_t_byIndex(i)->templatetype;
2525 if (item_templatetype==OMIT_VALUE || item_templatetype==ANY_OR_OMIT) {
2526 has_any_or_omit = true;
2527 break;
2528 }
2529 }
2530 if (has_any_or_omit) break;
2531 }
2532 // FIXME really no break?
2533 case OMIT_VALUE:
2534 case ANY_OR_OMIT:
2535 error("Restriction on %s does not allow usage of %s",
2536 definition_name, get_templatetype_str());
2537 break;
2538 default:
2539 break; // all others are ok
2540 }
2541 break;
2542 default:
2543 FATAL_ERROR("Template::chk_restriction()");
2544 }
2545 return needs_runtime_check;
2546 }
2547
2548 void Template::generate_code_expr(expression_struct *expr,
2549 template_restriction_t template_restriction)
2550 {
2551 // Only templates without extra matching attributes can be directly
2552 // represented in a C++ expression.
2553 if (!length_restriction && !is_ifpresent
2554 && template_restriction == TR_NONE) {
2555 // The single expression must be tried first because this rule might
2556 // cover some referenced templates.
2557 if (has_single_expr()) {
2558 expr->expr = mputstr(expr->expr, get_single_expr(true).c_str());
2559 return;
2560 }
2561 switch (templatetype) {
2562 case SPECIFIC_VALUE:
2563 // A simple specific value: use explicit cast.
2564 expr->expr = mputprintf(expr->expr, "%s(",
2565 my_governor->get_genname_template(my_scope).c_str());
2566 u.specific_value->generate_code_expr(expr);
2567 expr->expr = mputc(expr->expr, ')');
2568 return;
2569 case TEMPLATE_REFD:
2570 // A simple unfoldable referenced template.
2571 if (!get_needs_conversion()) {
2572 u.ref.ref->generate_code(expr);
2573 } else {
2574 Type *my_gov = get_expr_governor(Type::EXPECTED_TEMPLATE)
2575 ->get_type_refd_last();
2576 Type *refd_gov = u.ref.ref->get_refd_assignment()->get_Type()
2577 ->get_field_type(u.ref.ref->get_subrefs(),
2578 Type::EXPECTED_TEMPLATE)->get_type_refd_last();
2579 if (!my_gov || !refd_gov || my_gov == refd_gov)
2580 FATAL_ERROR("Template::generate_code_expr()");
2581 expression_struct expr_tmp;
2582 Code::init_expr(&expr_tmp);
2583 const string& tmp_id1 = get_temporary_id();
2584 const char *tmp_id_str1 = tmp_id1.c_str();
2585 const string& tmp_id2 = get_temporary_id();
2586 const char *tmp_id_str2 = tmp_id2.c_str();
2587 expr->preamble = mputprintf(expr->preamble,
2588 "%s %s;\n", refd_gov->get_genname_template(my_scope).c_str(),
2589 tmp_id_str1);
2590 expr_tmp.expr = mputprintf(expr_tmp.expr, "%s = ", tmp_id_str1);
2591 u.ref.ref->generate_code(&expr_tmp);
2592 expr->preamble = Code::merge_free_expr(expr->preamble, &expr_tmp);
2593 expr->preamble = mputprintf(expr->preamble,
2594 "%s %s;\n"
2595 "if (!%s(%s, %s)) TTCN_error(\"Values or templates of types `%s' "
2596 "and `%s' are not compatible at run-time\");\n",
2597 my_gov->get_genname_template(my_scope).c_str(), tmp_id_str2,
2598 TypeConv::get_conv_func(refd_gov, my_gov, get_my_scope()
2599 ->get_scope_mod()).c_str(), tmp_id_str2, tmp_id_str1, my_gov
2600 ->get_typename().c_str(), refd_gov->get_typename().c_str());
2601 expr->expr = mputprintf(expr->expr, "%s", tmp_id_str2);
2602 }
2603 return;
2604 case TEMPLATE_INVOKE:
2605 generate_code_expr_invoke(expr);
2606 return;
2607 default:
2608 break;
2609 }
2610 }
2611 // if none of the above methods are applicable use the most generic and
2612 // least efficient solution
2613 // create a temporary object, initialize it and use it in the expression
2614 const string& tmp_id = get_temporary_id();
2615 const char *tmp_id_str = tmp_id.c_str();
2616
2617 expr->preamble = mputprintf(expr->preamble, "%s %s;\n",
2618 my_governor->get_genname_template(my_scope).c_str(), tmp_id_str);
2619 set_genname_recursive(tmp_id);
2620 expr->preamble = generate_code_init(expr->preamble, tmp_id_str);
2621 if (template_restriction != TR_NONE)
2622 expr->preamble = Template::generate_restriction_check_code(expr->preamble,
2623 tmp_id_str, template_restriction);
2624 expr->expr = mputstr(expr->expr, tmp_id_str);
2625 }
2626
2627 char *Template::generate_code_init(char *str, const char *name)
2628 {
2629 if (get_code_generated()) return str;
2630 set_code_generated();
2631 if (err_descr) {
2632 str = err_descr->generate_code_init_str(str, string(name)+"_err_descr");
2633 }
2634 switch (templatetype) {
2635 case OMIT_VALUE:
2636 case ANY_VALUE:
2637 case ANY_OR_OMIT:
2638 case BSTR_PATTERN:
2639 case HSTR_PATTERN:
2640 case OSTR_PATTERN:
2641 case CSTR_PATTERN:
2642 case USTR_PATTERN:
2643 str = mputprintf(str, "%s = %s;\n", name, get_single_expr(false).c_str());
2644 break;
2645 case SPECIFIC_VALUE:
2646 if (get_code_section() == CS_POST_INIT)
2647 str = u.specific_value->rearrange_init_code(str);
2648 str = u.specific_value->generate_code_init(str, name);
2649 break;
2650 case TEMPLATE_REFD:
2651 str = generate_code_init_refd(str, name);
2652 break;
2653 case TEMPLATE_INVOKE:
2654 if (get_code_section() == CS_POST_INIT)
2655 str = rearrange_init_code_invoke(str);
2656 str = generate_code_init_invoke(str, name);
2657 break;
2658 case TEMPLATE_LIST:
2659 case INDEXED_TEMPLATE_LIST:
2660 str = generate_code_init_seof(str, name);
2661 break;
2662 case NAMED_TEMPLATE_LIST:
2663 str = generate_code_init_se(str, name);
2664 break;
2665 case VALUE_LIST_ALL_FROM:
2666 str = generate_code_init_all_from_list(str, name);
2667 break;
2668 case ALL_FROM:
2669 str = generate_code_init_all_from(str, name);
2670 break;
2671 case VALUE_LIST:
2672 str = generate_code_init_list(str, name, false);
2673 break;
2674 case COMPLEMENTED_LIST:
2675 str = generate_code_init_list(str, name, true);
2676 break;
2677 case VALUE_RANGE:
2678 if (get_code_section() == CS_POST_INIT)
2679 str = u.value_range->rearrange_init_code(str);
2680 str = u.value_range->generate_code_init(str, name);
2681 break;
2682 case SUPERSET_MATCH:
2683 str = generate_code_init_set(str, name, true);
2684 break;
2685 case SUBSET_MATCH:
2686 str = generate_code_init_set(str, name, false);
2687 break;
2688 case PERMUTATION_MATCH:
2689 warning("Don't know how to init PERMUT");
2690 str = mputprintf(str, "/* FIXME: PERMUT goes here, name=%s*/\n", name);
2691 break;
2692 case TEMPLATE_ERROR:
2693 case TEMPLATE_NOTUSED:
2694 // "default"
2695 FATAL_ERROR("Template::generate_code_init()");
2696 }
2697 if (length_restriction) {
2698 if (get_code_section() == CS_POST_INIT)
2699 str = length_restriction->rearrange_init_code(str);
2700 str = length_restriction->generate_code_init(str, name);
2701 }
2702 if (is_ifpresent) str = mputprintf(str, "%s.set_ifpresent();\n", name);
2703 if (err_descr) {
2704 str = mputprintf(str, "%s.set_err_descr(&%s_err_descr);\n", name, name);
2705 }
2706 return str;
2707 }
2708
2709 char *Template::rearrange_init_code(char *str)
2710 {
2711 switch (templatetype) {
2712 case SPECIFIC_VALUE:
2713 str = u.specific_value->rearrange_init_code(str);
2714 break;
2715 case TEMPLATE_REFD:
2716 str = rearrange_init_code_refd(str);
2717 break;
2718 case TEMPLATE_INVOKE:
2719 str = rearrange_init_code_invoke(str);
2720 break;
2721 case TEMPLATE_LIST:
2722 case VALUE_LIST:
2723 case COMPLEMENTED_LIST:
2724 case SUPERSET_MATCH:
2725 case SUBSET_MATCH:
2726 case PERMUTATION_MATCH:
2727 for (size_t i = 0; i < u.templates->get_nof_ts(); i++)
2728 str = u.templates->get_t_byIndex(i)->rearrange_init_code(str);
2729 break;
2730 case NAMED_TEMPLATE_LIST:
2731 for (size_t i = 0; i < u.named_templates->get_nof_nts(); i++)
2732 str = u.named_templates->get_nt_byIndex(i)->get_template()
2733 ->rearrange_init_code(str);
2734 break;
2735 case INDEXED_TEMPLATE_LIST:
2736 for (size_t i = 0; i < u.indexed_templates->get_nof_its(); i++)
2737 str = u.indexed_templates->get_it_byIndex(i)->get_template()
2738 ->rearrange_init_code(str);
2739 break;
2740 case VALUE_RANGE:
2741 str = u.value_range->rearrange_init_code(str);
2742 break;
2743 default:
2744 break;
2745 }
2746 if (length_restriction) str = length_restriction->rearrange_init_code(str);
2747 return str;
2748 }
2749
2750 bool Template::use_single_expr_for_init()
2751 {
2752 Template *t_last = get_template_refd_last();
2753 // return false in case of unfoldable references
2754 if (t_last->templatetype == TEMPLATE_REFD) return false;
2755 // return false if t_last is in a different module
2756 if (t_last->my_scope->get_scope_mod_gen() != my_scope->get_scope_mod_gen())
2757 return false;
2758 // return false if t_last cannot be represented by a single expression
2759 if (!t_last->has_single_expr()) return false;
2760 // return true if t_last is a generic wildcard, string pattern, etc.
2761 if (t_last->templatetype != SPECIFIC_VALUE) return true;
2762 // examine the specific value
2763 Value *v_last = t_last->u.specific_value->get_value_refd_last();
2764 switch (v_last->get_valuetype()) {
2765 case Value::V_EXPR:
2766 // do not calculate expressions again
2767 return false;
2768 case Value::V_REFD: {
2769 // v_last is an unfoldable value reference
2770 // the scope of the definition that v_last refers to
2771 Scope *v_scope =
2772 v_last->get_reference()->get_refd_assignment()->get_my_scope();
2773 for (Scope *t_scope = my_scope; t_scope;
2774 t_scope = t_scope->get_parent_scope()) {
2775 // return true if the referred definition is in the same scope
2776 // as this or in one of the parent scopes of this
2777 if (t_scope == v_scope) return true;
2778 }
2779 // otherwise return false
2780 return false; }
2781 default:
2782 // return true only if v_last is defined in the same module as this
2783 return v_last->get_my_scope()->get_scope_mod_gen() ==
2784 my_scope->get_scope_mod_gen();
2785 }
2786 }
2787
2788 char *Template::generate_code_init_refd(char *str, const char *name)
2789 {
2790 if (use_single_expr_for_init() && has_single_expr()) {
2791 str = mputprintf(str, "%s = %s;\n", name,
2792 get_single_expr(false).c_str());
2793 } else {
2794 expression_struct expr;
2795 Code::init_expr(&expr);
2796 bool use_ref_for_codegen = true;
2797 if (get_code_section() == CS_POST_INIT) {
2798 // the referencing template is a part of a non-parameterized template
2799 Common::Assignment *ass = u.ref.ref->get_refd_assignment();
2800 if (ass->get_asstype() == Common::Assignment::A_TEMPLATE &&
2801 ass->get_my_scope()->get_scope_mod_gen() ==
2802 my_scope->get_scope_mod_gen()) {
2803 // the reference points to (a field of) a template
2804 // within the local module
2805 if (ass->get_FormalParList()) {
2806 // the referred template is parameterized
2807 // generate the initialization sequence first for all dependent
2808 // non-parameterized templates
2809 str = rearrange_init_code_refd(str);
2810 } else {
2811 // the referred template is non-parameterized
2812 // use a different algorithm for code generation
2813 str = generate_rearrange_init_code_refd(str, &expr);
2814 use_ref_for_codegen = false;
2815 }
2816 }
2817 }
2818 if (use_ref_for_codegen) u.ref.ref->generate_code_const_ref(&expr);
2819 if (expr.preamble || expr.postamble) {
2820 // the expressions within reference need temporary objects
2821 str = mputstr(str, "{\n");
2822 str = mputstr(str, expr.preamble);
2823 if (use_runtime_2 && get_needs_conversion()) {
2824 const string& tmp_id = get_temporary_id();
2825 const char *tmp_id_str = tmp_id.c_str();
2826 Type *my_gov = my_governor->get_type_refd_last();
2827 Type *refd_gov = u.ref.ref->get_refd_assignment()->get_Type()
2828 ->get_type_refd_last();
2829 if (!my_gov || !refd_gov)
2830 FATAL_ERROR("Template::generate_code_init_refd()");
2831 str = mputprintf(str,
2832 "%s %s;\n"
2833 "if (!%s(%s, %s)) TTCN_error(\"Values or templates of types `%s' "
2834 "and `%s' are not compatible at run-time\");\n",
2835 my_gov->get_genname_template(my_scope).c_str(), tmp_id_str,
2836 TypeConv::get_conv_func(refd_gov, my_gov, get_my_scope()
2837 ->get_scope_mod()).c_str(), tmp_id_str, expr.expr, my_gov
2838 ->get_typename().c_str(), refd_gov->get_typename().c_str());
2839 str = mputprintf(str, "%s = %s;\n", name, tmp_id_str);
2840 } else {
2841 str = mputprintf(str, "%s = %s;\n", name, expr.expr);
2842 }
2843 str = mputstr(str, expr.postamble);
2844 str = mputstr(str, "}\n");
2845 } else {
2846 // the reference does not need temporary objects
2847 if (use_runtime_2 && get_needs_conversion()) {
2848 const string& tmp_id = get_temporary_id();
2849 const char *tmp_id_str = tmp_id.c_str();
2850 Type *my_gov = my_governor->get_type_refd_last();
2851 Type *refd_gov = u.ref.ref->get_refd_assignment()->get_Type()
2852 ->get_type_refd_last();
2853 if (!my_gov || !refd_gov)
2854 FATAL_ERROR("Template::generate_code_init_refd()");
2855 str = mputprintf(str,
2856 "%s %s;\n"
2857 "if (!%s(%s, %s)) TTCN_error(\"Values or templates of types `%s' "
2858 "and `%s' are not compatible at run-time\");\n",
2859 my_gov->get_genname_template(my_scope).c_str(), tmp_id_str,
2860 TypeConv::get_conv_func(refd_gov, my_gov, get_my_scope()
2861 ->get_scope_mod()).c_str(), tmp_id_str, expr.expr, my_gov
2862 ->get_typename().c_str(), refd_gov->get_typename().c_str());
2863 str = mputprintf(str, "%s = %s;\n", name, tmp_id_str);
2864 } else {
2865 str = mputprintf(str, "%s = %s;\n", name, expr.expr);
2866 }
2867 }
2868 Code::free_expr(&expr);
2869 }
2870 return str;
2871 }
2872
2873 char *Template::generate_code_init_invoke(char *str, const char *name)
2874 {
2875 expression_struct expr;
2876 Code::init_expr(&expr);
2877 expr.expr = mputprintf(expr.expr, "%s = ", name);
2878 generate_code_expr_invoke(&expr);
2879 return Code::merge_free_expr(str, &expr);
2880 }
2881
2882 char *Template::generate_rearrange_init_code_refd(char *str,
2883 expression_struct *expr)
2884 {
2885 /** Initially we can assume that:
2886 * - this is a referenced template and a part of a non-parameterized
2887 * template
2888 * - u.ref.ref points to (a field of) a non-parameterized template within
2889 * the same module as \a this.
2890 * - this ensures that the do-while loop will run at least twice (i.e. the
2891 * first continue statement will be reached in the first iteration) */
2892 stack<FieldOrArrayRef> refstack;
2893 Template *t = this;
2894 // first try to find the smallest dependent template
2895 for ( ; ; ) {
2896 if (t->templatetype == TEMPLATE_REFD) {
2897 Common::Assignment *ass = t->u.ref.ref->get_refd_assignment();
2898 /** Don't follow the reference if:
2899 * - the referenced definition is not a template
2900 * - the referenced template is parameterized or
2901 * - the referenced template is in different module */
2902 if (ass->get_asstype() == Common::Assignment::A_TEMPLATE &&
2903 ass->get_FormalParList() == 0 &&
2904 ass->get_my_scope()->get_scope_mod_gen() ==
2905 my_scope->get_scope_mod_gen()) {
2906 // accumulate the sub-references of the referred reference
2907 FieldOrArrayRefs *subrefs = t->u.ref.ref->get_subrefs();
2908 if (subrefs) {
2909 for (size_t i = subrefs->get_nof_refs(); i > 0; i--)
2910 refstack.push(subrefs->get_ref(i - 1));
2911 }
2912 // jump to the referred top-level template
2913 t = ass->get_Template();
2914 // start the iteration from the beginning
2915 continue;
2916 // stop otherwise: the reference cannot be followed
2917 } else break;
2918 }
2919 // stop if there are no sub-references
2920 if (refstack.empty()) break;
2921 // take the topmost sub-reference
2922 FieldOrArrayRef *subref = refstack.top();
2923 if (subref->get_type() == FieldOrArrayRef::FIELD_REF) {
2924 if (t->templatetype != NAMED_TEMPLATE_LIST) break;
2925 // the field reference can be followed
2926 t = t->u.named_templates->get_nt_byName(*subref->get_id())
2927 ->get_template();
2928 } else {
2929 // trying to follow an array reference
2930 if (t->templatetype != TEMPLATE_LIST) break;
2931 Value *array_index = subref->get_val()->get_value_refd_last();
2932 if (array_index->get_valuetype() != Value::V_INT) break;
2933 // the index is available at compilation time
2934 Int index = array_index->get_val_Int()->get_val();
2935 // index transformation in case of arrays
2936 if (t->my_governor->get_typetype() == Type::T_ARRAY)
2937 index -= t->my_governor->get_dimension()->get_offset();
2938 t = t->get_listitem_byIndex(index);
2939 }
2940 // the topmost sub-reference was processed
2941 // it can be erased from the stack
2942 refstack.pop();
2943 }
2944 // the smallest dependent template is now in t
2945 // generate the initializer sequence for t
2946 str = t->generate_code_init(str, t->get_lhs_name().c_str());
2947 // the equivalent C++ code of the referenced template is composed of the
2948 // genname of t and the remained sub-references in refstack
2949 expr->expr = mputstr(expr->expr, t->get_genname_own(my_scope).c_str());
2950 while (!refstack.empty()) {
2951 FieldOrArrayRef *subref = refstack.pop();
2952 if (subref->get_type() == FieldOrArrayRef::FIELD_REF) {
2953 expr->expr = mputprintf(expr->expr, ".%s()",
2954 subref->get_id()->get_name().c_str());
2955 } else {
2956 expr->expr = mputc(expr->expr, '[');
2957 subref->get_val()->generate_code_expr(expr);
2958 expr->expr = mputc(expr->expr, ']');
2959 }
2960 }
2961 return str;
2962 }
2963
2964 bool Template::compile_time() const
2965 {
2966 switch (templatetype) {
2967 case ALL_FROM:
2968 case VALUE_LIST_ALL_FROM:
2969 return false;
2970 case TEMPLATE_ERROR: /**< erroneous template */
2971 case TEMPLATE_NOTUSED: /**< not used symbol (-) */
2972 case OMIT_VALUE: /**< omit */
2973 case ANY_VALUE: /**< any value (?) */
2974 case ANY_OR_OMIT: /**< any or omit (*) */
2975 case SPECIFIC_VALUE: /**< specific value */
2976 case TEMPLATE_REFD: /**< reference to another template */
2977 case VALUE_RANGE: /**< value range match */
2978 case BSTR_PATTERN: /**< bitstring pattern */
2979 case HSTR_PATTERN: /**< hexstring pattern */
2980 case OSTR_PATTERN: /**< octetstring pattern */
2981 case CSTR_PATTERN: /**< character string pattern */
2982 case USTR_PATTERN: /**< universal charstring pattern */
2983 case TEMPLATE_INVOKE:
2984 // Simple templates can be computed at compile time
2985 return true;
2986
2987 // "Complex" templates need to look at all elements
2988 case TEMPLATE_LIST:
2989 case VALUE_LIST:
2990 case COMPLEMENTED_LIST:
2991 case SUPERSET_MATCH:
2992 case SUBSET_MATCH:
2993 case PERMUTATION_MATCH:
2994 for (size_t i = 0; i < u.templates->get_nof_ts(); i++)
2995 if (u.templates->get_t_byIndex(i)->compile_time()) continue;
2996 else return false;
2997 return true;
2998 case NAMED_TEMPLATE_LIST:
2999 for (size_t i = 0; i < u.named_templates->get_nof_nts(); i++)
3000 if (!u.named_templates->get_nt_byIndex(i)->get_template()->compile_time())
3001 return false;
3002 return true;
3003 case INDEXED_TEMPLATE_LIST:
3004 for (size_t i = 0; i <u.indexed_templates->get_nof_its(); i++)
3005 if (!u.indexed_templates->get_it_byIndex(i)->get_template()->compile_time())
3006 return false;
3007 return true;
3008 }
3009
3010 return true; // not reached
3011 }
3012
3013 char *Template::generate_code_init_seof(char *str, const char *name)
3014 {
3015 switch (templatetype) {
3016 case TEMPLATE_LIST: {
3017 size_t nof_ts = u.templates->get_nof_ts();
3018 if (nof_ts == 0) {
3019 str = mputprintf(str, "%s = NULL_VALUE;\n", name);
3020 break;
3021 }
3022
3023 // nof_ts > 0
3024 Type *t_last = my_governor->get_type_refd_last();
3025 Int index_offset;
3026 if (t_last->get_typetype() == Type::T_ARRAY) {
3027 // take the start index from the array dimension
3028 index_offset = t_last->get_dimension()->get_offset();
3029 } else {
3030 // indexing always starts from zero
3031 index_offset = 0;
3032 }
3033
3034 const string& oftype_name =
3035 t_last->get_ofType()->get_genname_template(my_scope);
3036 const char *oftype_name_str = oftype_name.c_str();
3037
3038 ReferenceChain refch (this, "While searching template");
3039 if (!flattened || my_scope->get_statementblock_scope()) { // this "if" may be redundant if all non-var templates are flattened
3040 str = mputstr(str, "// this is a var template\n");
3041
3042 if (compile_time()) goto compile_time;
3043 // run-time, variable sized init
3044
3045 // This is a record-of var template, like this:
3046 // var template rec_of_int vt_r := {
3047 // 1, 2, 3, permutation(10, all from x, 20), 4, 5 }
3048 // ^^^^^^^--- these ------------------------^^^^^
3049 // are known at compile time, but the length of the "all from"
3050 // is only known at run time.
3051 // Collect the indices where there is an "all from".
3052 dynamic_array<int> variables;
3053 size_t fixed_part = 0;
3054 if (has_permutation) {
3055 for (size_t i = 0; i < nof_ts; i++) {
3056 Template *t = u.templates->get_t_byIndex(i);
3057 if (t->templatetype == PERMUTATION_MATCH) {
3058 size_t num_p = t->u.templates->get_nof_ts();
3059 // t->u.templates is 2: "hello" and all from ...
3060 for (size_t j = 0; j < num_p; ++j) {
3061 Template *subt = t->u.templates->get_t_byIndex(j);
3062 if (subt->templatetype == ALL_FROM) {
3063 variables.add(j);
3064 }
3065 else fixed_part++;
3066 }
3067 }
3068 else fixed_part++;
3069 }
3070
3071 char* str_preamble = 0;
3072 char* str_set_size = mputprintf(0, "%s.set_size(%lu", name,
3073 (unsigned long)fixed_part);
3074
3075 // variable part
3076 for (size_t i = 0; i < nof_ts; i++) {
3077 Template *t = u.templates->get_t_byIndex(i);
3078 for (size_t k = 0, v = variables.size(); k < v; ++k) {
3079 if (t->templatetype != PERMUTATION_MATCH) continue; // ?? really nothing to do ??
3080 Template *subt = t->u.templates->get_t_byIndex(variables[k]);
3081 if (subt->templatetype == ALL_FROM) {
3082 Value *refv = subt->u.all_from->u.specific_value;
3083 // don't call get_Value(), it rips out the value from the template
3084
3085 if (refv->get_valuetype()!=Value::V_REFD) FATAL_ERROR("%s", __FUNCTION__);
3086 Common::Reference *ref = refv->get_reference();
3087 FieldOrArrayRefs *subrefs = ref->get_subrefs();
3088 Common::Assignment *ass = ref->get_refd_assignment();
3089
3090 str_set_size = mputstrn(str_set_size, " + ", 3);
3091
3092 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3093 if (ref_pard) {
3094 // in case of parametrised references:
3095 // - temporary parameters need to be declared (stored in str_preamble)
3096 // - the same temporary needs to be used at each call (generate_code_cached call)
3097 expression_struct expr;
3098 Code::init_expr(&expr);
3099
3100 ref_pard->generate_code_cached(&expr);
3101 str_set_size = mputprintf(str_set_size, "%s", expr.expr);
3102 if (expr.preamble)
3103 str_preamble = mputstr(str_preamble, expr.preamble);
3104
3105 Code::free_expr(&expr);
3106 }
3107 else {
3108 str_set_size = mputstr (str_set_size, ass->get_id().get_name().c_str());
3109 if (subrefs) {
3110 expression_struct expr;
3111 Code::init_expr(&expr);
3112
3113 subrefs->generate_code(&expr, ass);
3114 str_set_size = mputprintf(str_set_size, "%s", expr.expr);
3115
3116 Code::free_expr(&expr);
3117 }
3118 }
3119 str_set_size = mputstr(str_set_size, ".n_elem()");
3120 }
3121 }
3122 }
3123
3124 str = mputstr(str, str_preamble);
3125 str = mputstr(str, str_set_size);
3126
3127 Free(str_preamble);
3128 Free(str_set_size);
3129
3130 str = mputstrn(str, ");\n", 3); // finally done set_size
3131
3132 size_t index = 0;
3133 string skipper, hopper;
3134 for (size_t i = 0; i < nof_ts; i++) {
3135 Template *t = u.templates->get_t_byIndex(i);
3136 switch (t->templatetype) {
3137 case ALL_FROM: {
3138 break; }
3139 case PERMUTATION_MATCH: {
3140 size_t nof_perm_ts = t->u.templates->get_nof_ts();
3141 for (size_t j = 0; j < nof_perm_ts; j++) {
3142 Int ix(index_offset + index + j);
3143 Template *permut_elem = t->u.templates->get_t_byIndex(j);
3144 if (permut_elem->templatetype == ALL_FROM) {
3145 expression_struct expr;
3146 Code::init_expr(&expr);
3147 switch (permut_elem->u.all_from->templatetype) {
3148 case SPECIFIC_VALUE: {
3149 Value *spec = permut_elem->u.all_from->u.specific_value;
3150 switch (spec->get_valuetype()) {
3151 case Common::Value::V_REFD: {
3152 Common::Reference *ref = spec->get_reference();
3153
3154 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3155 if (ref_pard)
3156 ref_pard->generate_code_cached(&expr);
3157 else
3158 ref->generate_code(&expr);
3159
3160 break; }
3161 default:
3162 FATAL_ERROR("vtype %d", spec->get_valuetype());
3163 break;
3164 }
3165 break; }
3166
3167
3168 default:
3169 FATAL_ERROR("ttype %d", permut_elem->u.all_from->templatetype);
3170 break;
3171 }
3172 str = mputprintf(str,
3173 "for (int i_i = 0, i_lim = %s.n_elem(); i_i < i_lim; ++i_i) {\n",
3174 expr.expr);
3175
3176 str = permut_elem->generate_code_init_seof_element(str, name,
3177 (Int2string(ix) + skipper + " + i_i").c_str(),
3178 oftype_name_str);
3179
3180 str = mputstrn(str, "}\n", 2);
3181 skipper += "-1+";
3182 skipper += expr.expr;
3183 skipper += ".n_elem() /* 3005 */ ";
3184 Code::free_expr(&expr);
3185 }
3186 else {
3187 str = permut_elem->generate_code_init_seof_element(str, name,
3188 (Int2string(ix) + skipper).c_str(), oftype_name_str);
3189 }
3190 }
3191 // do not consider index_offset in case of permutation indicators
3192 str = mputprintf(str, "%s.add_permutation(%lu%s, %lu%s);\n", name,
3193 (unsigned long)index, hopper.c_str(),
3194 (unsigned long)(index + nof_perm_ts - 1), skipper.c_str());
3195 hopper = skipper;
3196 t->set_code_generated();
3197 index += nof_perm_ts;
3198 break; }
3199
3200 default:
3201 str = t->generate_code_init_seof_element(str, name,
3202 (Int2string(index_offset + index) + skipper).c_str(), oftype_name_str);
3203 // no break
3204 case TEMPLATE_NOTUSED:
3205 index++;
3206 break;
3207 }
3208 }
3209
3210 break;
3211
3212 }
3213
3214
3215 if (!has_permutation && has_allfrom()) {
3216 for (size_t i = 0; i < nof_ts; i++) {
3217 Template *t = u.templates->get_t_byIndex(i);
3218 if (t->templatetype == ALL_FROM) {
3219 variables.add(i);
3220 }
3221 else {
3222 fixed_part++;
3223 }
3224 }
3225 char* str_preamble = 0;
3226 char* str_set_size = mputprintf(0, "%s.set_size(%lu", name,
3227 (unsigned long)fixed_part);
3228
3229 // variable part
3230 for (size_t i = 0; i < nof_ts; i++) {
3231 Template *t = u.templates->get_t_byIndex(i);
3232 printf("generate_code_init_seof ALL_FROM: temptype %u\n", t->templatetype);
3233 for (size_t k = 0, v = variables.size(); k < v; ++k) {
3234 if (t->templatetype == ALL_FROM) {
3235 Value *refv = t->u.all_from->u.specific_value;
3236 // don't call get_Value(), it rips out the value from the template
3237 if (refv->get_valuetype()!=Value::V_REFD) FATAL_ERROR("%s", __FUNCTION__);
3238 Common::Reference *ref = refv->get_reference();
3239 FieldOrArrayRefs *subrefs = ref->get_subrefs();
3240 Common::Assignment *ass = ref->get_refd_assignment();
3241 str_set_size = mputstrn(str_set_size, " + ", 3);
3242 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3243 if (ref_pard) {
3244 // in case of parametrised references:
3245 // - temporary parameters need to be declared (stored in str_preamble)
3246 // - the same temporary needs to be used at each call (generate_code_cached call)
3247 expression_struct expr;
3248 Code::init_expr(&expr);
3249
3250 ref_pard->generate_code_cached(&expr);
3251 str_set_size = mputprintf(str_set_size, "%s", expr.expr);
3252 if (expr.preamble)
3253 str_preamble = mputstr(str_preamble, expr.preamble);
3254 Code::free_expr(&expr);
3255 }
3256 else {
3257 str_set_size = mputstr (str_set_size, ass->get_id().get_name().c_str());
3258 if (subrefs) {
3259 expression_struct expr;
3260 Code::init_expr(&expr);
3261 subrefs->generate_code(&expr, ass);
3262 str_set_size = mputprintf(str_set_size, "%s", expr.expr);
3263 Code::free_expr(&expr);
3264 }
3265 }
3266 str_set_size = mputstr(str_set_size, ".n_elem()");
3267 }
3268 }
3269 }
3270 str = mputstr(str, str_preamble);
3271 str = mputstr(str, str_set_size);
3272 Free(str_preamble);
3273 Free(str_set_size);
3274 str = mputstrn(str, ");\n", 3); // finally done set_size
3275
3276 size_t index = 0;
3277 string skipper;
3278 for (size_t i = 0; i < nof_ts; i++) {
3279 Template *t = u.templates->get_t_byIndex(i);
3280 Int ix(index_offset + i);
3281 switch (t->templatetype) {
3282 case ALL_FROM: {
3283 expression_struct expr;
3284 Code::init_expr(&expr);
3285 switch (t->u.all_from->templatetype) {
3286 case SPECIFIC_VALUE: {
3287 Value *spec = t->u.all_from->u.specific_value;
3288 switch (spec->get_valuetype()) {
3289 case Common::Value::V_REFD: {
3290 Common::Reference *ref = spec->get_reference();
3291 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3292 if (ref_pard)
3293 ref_pard->generate_code_cached(&expr);
3294 else
3295 ref->generate_code(&expr);
3296 break; }
3297 default:
3298 FATAL_ERROR("vtype %d", spec->get_valuetype());
3299 break;
3300 }
3301 break; }
3302 default: {
3303 FATAL_ERROR("ttype %d", t->u.all_from->templatetype);
3304 break; }
3305 }
3306 str = mputprintf(str,
3307 "for (int i_i = 0, i_lim = %s.n_elem(); i_i < i_lim; ++i_i) {\n",
3308 expr.expr);
3309 str = t->generate_code_init_seof_element(str, name,
3310 (Int2string(ix) + skipper + " + i_i").c_str(),
3311 oftype_name_str);
3312 str = mputstrn(str, "}\n", 2);
3313 skipper += "-1+";
3314 skipper += expr.expr;
3315 skipper += ".n_elem() ";
3316 Code::free_expr(&expr);
3317 t->set_code_generated();
3318 ++index;
3319 break; }
3320 default: {
3321 printf("generate_code_init_seof allfrom default: %s\n",
3322 (Int2string(index_offset + index) + skipper).c_str());
3323 str = t->generate_code_init_seof_element(str, name,
3324 (Int2string(index_offset + index) + skipper).c_str(), oftype_name_str);
3325 // no break
3326 case TEMPLATE_NOTUSED:
3327 ++index;
3328 break; }
3329 }
3330 }
3331 }
3332
3333 // else carry on
3334 }
3335compile_time:
3336 // setting the size first
3337 if (!has_allfrom())
3338 str = mputprintf(str, "%s.set_size(%lu);\n", name, (unsigned long) get_nof_listitems());
3339 // determining the index offset based on the governor
3340
3341 size_t index = 0;
3342 for (size_t i = 0; i < nof_ts; i++) {
3343 Template *t = u.templates->get_t_byIndex(i);
3344 switch (t->templatetype) {
3345 case PERMUTATION_MATCH: {
3346 size_t nof_perm_ts = t->u.templates->get_nof_ts();
3347 for (size_t j = 0; j < nof_perm_ts; j++) {
3348 Int ix(index_offset + index + j);
3349 str = t->u.templates->get_t_byIndex(j)
3350 ->generate_code_init_seof_element(str, name,
3351 Int2string(ix).c_str(), oftype_name_str);
3352 }
3353 // do not consider index_offset in case of permutation indicators
3354 str = mputprintf(str, "%s.add_permutation(%lu, %lu);\n", name,
3355 (unsigned long)index, (unsigned long) (index + nof_perm_ts - 1));
3356 t->set_code_generated();
3357 index += nof_perm_ts;
3358 break; }
3359
3360 default:
3361 str = t->generate_code_init_seof_element(str, name,
3362 Int2string(index_offset + index).c_str(), oftype_name_str);
3363 // no break
3364 case ALL_FROM:
3365 case TEMPLATE_NOTUSED:
3366 index++;
3367 }
3368 }
3369 break; }
3370 case INDEXED_TEMPLATE_LIST: {
3371 size_t nof_its = u.indexed_templates->get_nof_its();
3372 if (nof_its > 0) {
3373 Type *t_last = my_governor->get_type_refd_last();
3374 const string& oftype_name =
3375 t_last->get_ofType()->get_genname_template(my_scope);
3376 const char *oftype_name_str = oftype_name.c_str();
3377 // There's no need to generate a set_size call here. To do that, we
3378 // should know the size of the base template, which is not available
3379 // from here.
3380 for (size_t i = 0; i < nof_its; i++) {
3381 IndexedTemplate *it = u.indexed_templates->get_it_byIndex(i);
3382 const string& tmp_id_1 = get_temporary_id();
3383 str = mputstr(str, "{\n");
3384 Value *index = (it->get_index()).get_val();
3385 if (index->get_valuetype() != Value::V_INT) {
3386 const string& tmp_id_2 = get_temporary_id();
3387 str = mputprintf(str, "int %s;\n", tmp_id_2.c_str());
3388 str = index->generate_code_init(str, tmp_id_2.c_str());
3389 str = mputprintf(str, "%s& %s = %s[%s];\n", oftype_name_str,
3390 tmp_id_1.c_str(), name, tmp_id_2.c_str());
3391 } else {
3392 str = mputprintf(str, "%s& %s = %s[%s];\n", oftype_name_str,
3393 tmp_id_1.c_str(), name,
3394 Int2string(index->get_val_Int()->get_val()).c_str());
3395 }
3396 str = it->get_template()->generate_code_init(str, tmp_id_1.c_str());
3397 str = mputstr(str, "}\n");
3398 }
3399 } else {
3400 // It seems to be impossible in this case.
3401 str = mputprintf(str, "%s = NULL_VALUE;\n", name);
3402 }
3403 break; }
3404 default:
3405 FATAL_ERROR("Template::generate_code_init_seof()");
3406 return NULL;
3407 }
3408 return str;
3409 }
3410
3411 char *Template::generate_code_init_seof_element(char *str, const char *name,
3412 const char* index, const char *element_type_genname)
3413 {
3414 if (needs_temp_ref()) {
3415 const string& tmp_id = get_temporary_id();
3416 str = mputprintf(str, "{\n"
3417 "%s& %s = %s[%s];\n",
3418 element_type_genname, tmp_id.c_str(), name, index);
3419 str = generate_code_init(str, tmp_id.c_str());
3420 str = mputstr(str, "}\n");
3421 } else {
3422 char *embedded_name = mprintf("%s[%s]", name, index);
3423 str = generate_code_init(str, embedded_name);
3424 Free(embedded_name);
3425 }
3426 return str;
3427 }
3428
3429 char *Template::generate_code_init_all_from(char *str, const char *name)
3430 {
3431 // we are ALL_FROM, hence u.all_from
3432 switch (u.all_from->templatetype) {
3433 case SPECIFIC_VALUE: {
3434 Value *spec = u.all_from->u.specific_value;
3435 switch (spec->get_valuetype()) {
3436 case Common::Value::V_REFD: {
3437 Common::Reference *ref = spec->get_reference();
3438 expression_struct expr;
3439 Code::init_expr(&expr);
3440 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3441 if (ref_pard)
3442 ref_pard->generate_code_cached(&expr);
3443 else
3444 ref->generate_code(&expr);
3445 str = mputprintf(str, "%s = %s[i_i];\n", name, expr.expr);
3446 // The caller will have to provide the for cycle with this variable
3447 Code::free_expr(&expr);
3448 break; }
3449 default:
3450 break;
3451 }
3452 break; }
3453 default:
3454 break;
3455 }
3456 return str;
3457 }
3458
3459 char *Template::generate_code_init_all_from_list(char *str, const char *name)
3460 {
3461 // FIXME: this is the third instance
3462 expression_struct expr;
3463 Code::init_expr(&expr);
3464 switch (u.all_from->templatetype) {
3465 case SPECIFIC_VALUE: {
3466 Value *spec = u.all_from->u.specific_value;
3467 switch (spec->get_valuetype()) {
3468 case Common::Value::V_REFD: {
3469 Common::Reference *ref = spec->get_reference();
3470 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3471 if (ref_pard)
3472 ref_pard->generate_code_cached(&expr);
3473 else
3474 ref->generate_code(&expr);
3475 break; }
3476 default:
3477 FATAL_ERROR("vtype %d", spec->get_valuetype());
3478 break;
3479 }
3480 break; }
3481
3482 default:
3483 FATAL_ERROR("ttype %d", u.all_from->templatetype);
3484 break;
3485 }
3486
3487 if (expr.preamble)
3488 str = mputstr(str, expr.preamble);
3489
3490 str = mputprintf(str,
3491 "%s.set_type(VALUE_LIST, %s.n_elem());\n"
3492 "for (int i_i = 0, i_lim = %s.n_elem(); i_i < i_lim; ++i_i) {\n",
3493 name,
3494 expr.expr,
3495 expr.expr);
3496 string embedded_name(name);
3497 embedded_name += ".list_item(i_i)";
3498 str = generate_code_init_all_from(str, embedded_name.c_str());
3499 str = mputstrn(str, "}\n", 2);
3500
3501 Code::free_expr(&expr);
3502 return str;
3503 }
3504
3505 char *Template::generate_code_init_se(char *str, const char *name)
3506 { // named template list
3507 size_t nof_nts = u.named_templates->get_nof_nts();
3508 Type *type = my_governor->get_type_refd_last();
3509 if (type->get_nof_comps() > 0) {
3510 for (size_t i = 0; i < nof_nts; i++) {
3511 NamedTemplate *nt = u.named_templates->get_nt_byIndex(i);
3512 const Identifier& fieldname = nt->get_name();
3513 const char *fieldname_str = 0;
3514 string at("AT_");
3515 if (type->get_typetype()==Type::T_ANYTYPE) {
3516 at += fieldname.get_name();
3517 fieldname_str = at.c_str();
3518 }
3519 else {
3520 fieldname_str = fieldname.get_name().c_str();
3521 }
3522 Template *t = nt->get_template();
3523 if (t->needs_temp_ref()) {
3524 Type *field_type;
3525 if (type->get_typetype() == Type::T_SIGNATURE) {
3526 field_type = type->get_signature_parameters()
3527 ->get_param_byName(fieldname)->get_type();
3528 } else {
3529 field_type = type->get_comp_byName(fieldname)->get_type();
3530 }
3531 const string& tmp_id = get_temporary_id();
3532 const char *tmp_id_str = tmp_id.c_str();
3533 str = mputprintf(str, "{\n"
3534 "%s& %s = %s.%s();\n",
3535 field_type->get_genname_template(my_scope).c_str(), tmp_id_str,
3536 name, fieldname_str);
3537 str = t->generate_code_init(str, tmp_id_str);
3538 str = mputstr(str, "}\n");
3539 } else {
3540 char *embedded_name = mprintf("%s.%s()", name, fieldname_str);
3541 str = t->generate_code_init(str, embedded_name);
3542 Free(embedded_name);
3543 }
3544 }
3545 } else {
3546 str = mputprintf(str, "%s = NULL_VALUE;\n", name);
3547 }
3548 return str;
3549 }
3550
3551 char *Template::generate_code_init_list(char *str, const char *name,
3552 bool is_complemented) // VALUE_LIST or COMPLEMENTED_LIST
3553 {
3554 size_t nof_ts = u.templates->get_nof_ts();
3555 const string& type_name = my_governor->get_genname_template(my_scope);
3556 const char *type_name_str = type_name.c_str();
3557
3558 dynamic_array<int> variables;
3559 size_t fixed_part = 0;
3560 for (size_t i = 0; i < nof_ts; ++i) {
3561 Template *t = u.templates->get_t_byIndex(i);
3562 if (t->templatetype == ALL_FROM) {
3563 variables.add(i);
3564 }
3565 else ++fixed_part;
3566 }
3567
3568 if (variables.size() > 0) {
3569 char* str_preamble = 0;
3570 char* str_set_type = mputprintf(0, "%s.set_type(%s, %lu", name,
3571 (is_complemented ? "COMPLEMENTED_LIST" : "VALUE_LIST"),
3572 (unsigned long)fixed_part);
3573 // The code to compute the number of elements at run time (the variable part).
3574 // This is the sum of sizes of "all from"s.
3575 for (size_t v = 0, vs = variables.size(); v < vs; ++v) {
3576 Template *vt = u.templates->get_t_byIndex(variables[v]);
3577 if (vt->templatetype != ALL_FROM) FATAL_ERROR("must be ALL_FROM");
3578 if (vt->u.all_from->templatetype != SPECIFIC_VALUE) FATAL_ERROR("not value");
3579 Value *val = vt->u.all_from->u.specific_value;
3580 if (val->get_valuetype() != Value::V_REFD) FATAL_ERROR("ref expected from val");
3581 Common::Reference *ref = val->get_reference();
3582 FieldOrArrayRefs *subrefs = ref->get_subrefs();
3583 Common::Assignment *ass = ref->get_refd_assignment();
3584 if (!ass) FATAL_ERROR("Could not grab ass!");
3585
3586 str_set_type = mputstrn(str_set_type, " + ", 3);
3587
3588 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3589 if (ref_pard) {
3590 // in case of parametrised references:
3591 // - temporary parameters need to be declared (stored in str_preamble)
3592 // - the same temporary needs to be used at each call (generate_code_cached call)
3593 expression_struct expr;
3594 Code::init_expr(&expr);
3595
3596 ref_pard->generate_code_cached(&expr);
3597 str_set_type = mputprintf(str_set_type, "%s", expr.expr);
3598 if (expr.preamble)
3599 str_preamble = mputstr(str_preamble, expr.preamble);
3600
3601 Code::free_expr(&expr);
3602 }
3603 else {
3604 str_set_type = mputstr (str_set_type, ass->get_id().get_name().c_str());
3605 if (subrefs) {
3606 expression_struct expr;
3607 Code::init_expr(&expr);
3608
3609 subrefs->generate_code(&expr, ass);
3610 str_set_type = mputprintf(str, "%s", expr.expr);
3611
3612 Code::free_expr(&expr);
3613 }
3614 }
3615 str_set_type = mputstr(str_set_type, ".n_elem()");
3616 }
3617
3618 str = mputstr(str, str_preamble);
3619 str = mputstr(str, str_set_type);
3620
3621 Free(str_preamble);
3622 Free(str_set_type);
3623
3624 str = mputstrn(str, ");\n", 3);
3625
3626 string shifty; // contains the expression used to calculate
3627 // the size increase due to the runtime expansion of "all from".
3628 // In nof_ts, each "all from" appears as 1, but actually contributes
3629 // more. So the increase (by which all elements after the "all from"
3630 // are shifted) is:  target_of_all_from.n_elem()-1
3631 // This needs to be accumulated for each "all from".
3632 for (size_t vi = 0; vi < nof_ts; ++vi) {
3633 Template *t = u.templates->get_t_byIndex(vi);
3634 switch (t->templatetype) {
3635 case VALUE_LIST_ALL_FROM:
3636 FATAL_ERROR("VALUE_LIST_ALL_FROM not handled");
3637 case ALL_FROM: {
3638 expression_struct expr;
3639 Code::init_expr(&expr);
3640 // variable one
3641 switch (t->u.all_from->templatetype) {
3642 case SPECIFIC_VALUE: {
3643 Value *sv = t->u.all_from->u.specific_value;
3644 switch (sv->get_valuetype()) {
3645 case Value::V_REFD: {
3646 Common::Reference *ref = sv->get_reference();
3647 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3648 if (ref_pard)
3649 ref_pard->generate_code_cached(&expr);
3650 else
3651 ref->generate_code(&expr);
3652 break; }
3653
3654 default:
3655 FATAL_ERROR("VT NOT HANDLED");
3656 } // switch valuetype
3657 break; }
3658
3659 default:
3660 FATAL_ERROR("ttype not handled");
3661 break;
3662 }
3663
3664 // All local variables should contain a single underscore,
3665 // to avoid potential clashes with field names.
3666 str = mputprintf(str, "for (int i_i= 0, i_lim = %s.n_elem(); i_i < i_lim; ++i_i) {\n",
3667 expr.expr);
3668
3669 if (t->needs_temp_ref()) {
3670 // Not possible, because t->templatetype is ALL_FROM
3671 FATAL_ERROR("temp ref not handled");
3672 }
3673 char *embedded_name = mprintf("%s.list_item(%lu + i_i%s)", name,
3674 (unsigned long) vi, shifty.c_str());
3675 str = t->generate_code_init(str, embedded_name);
3676 Free(embedded_name);
3677
3678 str = mputstrn(str, "}\n", 2);
3679
3680 shifty += "-1+";
3681 shifty += expr.expr;
3682 shifty += ".n_elem() /* 3303 */ ";
3683
3684 Code::free_expr(&expr);
3685 break; }
3686
3687 default: // "fixed one"
3688 if (t->needs_temp_ref()) { // FIXME: this is copypasta from below / but may need to be changed
3689 const string& tmp_id = get_temporary_id();
3690 const char *tmp_id_str = tmp_id.c_str();
3691 str = mputprintf(str, "{\n"
3692 "%s& %s = %s.list_item(%lu);\n",
3693 type_name_str, tmp_id_str, name, (unsigned long) vi);
3694 str = t->generate_code_init(str, tmp_id_str);
3695 str = mputstr(str, "}\n");
3696 } else {
3697 char *embedded_name = mprintf("%s.list_item(%lu%s)", name,
3698 (unsigned long) vi, shifty.c_str());
3699 str = t->generate_code_init(str, embedded_name);
3700 Free(embedded_name);
3701 }
3702 break;
3703 } // switch t->templatetype
3704 }
3705 }
3706 else {
3707 str = mputprintf(str, "%s.set_type(%s, %lu);\n", name,
3708 is_complemented ? "COMPLEMENTED_LIST" : "VALUE_LIST",
3709 (unsigned long) nof_ts);
3710 for (size_t i = 0; i < nof_ts; i++) {
3711 Template *t = u.templates->get_t_byIndex(i);
3712 if (t->needs_temp_ref()) {
3713 const string& tmp_id = get_temporary_id();
3714 const char *tmp_id_str = tmp_id.c_str();
3715 str = mputprintf(str, "{\n"
3716 "%s& %s = %s.list_item(%lu);\n",
3717 type_name_str, tmp_id_str, name, (unsigned long) i);
3718 str = t->generate_code_init(str, tmp_id_str);
3719 str = mputstr(str, "}\n");
3720 } else {
3721 char *embedded_name = mprintf("%s.list_item(%lu)", name,
3722 (unsigned long) i);
3723 str = t->generate_code_init(str, embedded_name);
3724 Free(embedded_name);
3725 }
3726 }
3727 }
3728 return str;
3729 }
3730
3731 char *Template::generate_code_init_set(char *str, const char *name,
3732 bool is_superset) // SUPERSET_MATCH and SUBSET_MATCH
3733 {
3734 size_t nof_ts = u.templates->get_nof_ts();
3735 const string& oftype_name =
3736 my_governor->get_ofType()->get_genname_template(my_scope);
3737 const char *oftype_name_str = oftype_name.c_str();
3738
3739 dynamic_array<int> variables;
3740 size_t fixed_part = 0;
3741 for (size_t i = 0; i < nof_ts; ++i) {
3742 Template *t = u.templates->get_t_byIndex(i);
3743 if (t->templatetype == ALL_FROM) {
3744 variables.add(i);
3745 }
3746 else ++fixed_part;
3747 }
3748
3749 //warning("There are %lu set elements", nof_ts);
3750 if (variables.size() > 0) {
3751 char* str_preamble = 0;
3752 char* str_set_type = mputprintf(0, "%s.set_type(%s, %lu", name,
3753 is_superset ? "SUPERSET_MATCH" : "SUBSET_MATCH", (unsigned long) fixed_part);
3754
3755 for (size_t v = 0, vs = variables.size(); v < vs; ++v) {
3756 Template *vt = u.templates->get_t_byIndex(variables[v]);
3757 if (vt->templatetype != ALL_FROM) FATAL_ERROR("must be ALL_FROM");
3758 if (vt->u.all_from->templatetype != SPECIFIC_VALUE) FATAL_ERROR("not value");
3759 Value *val = vt->u.all_from->u.specific_value;
3760 if (val->get_valuetype() != Value::V_REFD) FATAL_ERROR("ref expected from val");
3761 Common::Reference *ref = val->get_reference();
3762 FieldOrArrayRefs *subrefs = ref->get_subrefs();
3763 Common::Assignment *ass = ref->get_refd_assignment();
3764 if (!ass) FATAL_ERROR("Could not grab ass!");
3765
3766 str_set_type = mputstrn(str_set_type, " + ", 3);
3767
3768 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3769 if (ref_pard) {
3770 // in case of parametrised references:
3771 // - temporary parameters need to be declared (stored in str_preamble)
3772 // - the same temporary needs to be used at each call (generate_code_cached call)
3773 expression_struct expr;
3774 Code::init_expr(&expr);
3775
3776 ref_pard->generate_code_cached(&expr);
3777 str_set_type = mputprintf(str_set_type, "%s", expr.expr);
3778 if (expr.preamble)
3779 str_preamble = mputstr(str_preamble, expr.preamble);
3780
3781 Code::free_expr(&expr);
3782 }
3783 else {
3784 str_set_type = mputstr (str_set_type, ass->get_id().get_name().c_str());
3785 if (subrefs) {
3786 expression_struct expr;
3787 Code::init_expr(&expr);
3788
3789 subrefs->generate_code(&expr, ass);
3790 str_set_type = mputprintf(str_set_type, "%s", expr.expr);
3791
3792 Code::free_expr(&expr);
3793 }
3794 }
3795 str_set_type = mputstr(str_set_type, ".n_elem()");
3796 }
3797
3798 str = mputstr(str, str_preamble);
3799 str = mputstr(str, str_set_type);
3800
3801 Free(str_preamble);
3802 Free(str_set_type);
3803
3804 str = mputstrn(str, ");\n", 3);
3805
3806 string shifty;
3807 for (size_t i = 0; i < nof_ts; i++) {
3808 Template *t = u.templates->get_t_byIndex(i);
3809 switch (t->templatetype) {
3810 case ALL_FROM: {
3811 expression_struct expr; // FIXME copypasta from init_list above !
3812 Code::init_expr(&expr);
3813 // variable one
3814 switch (t->u.all_from->templatetype) {
3815 case SPECIFIC_VALUE: {
3816 Value *sv = t->u.all_from->u.specific_value;
3817 switch (sv->get_valuetype()) {
3818 case Value::V_REFD: {
3819 Common::Reference *ref = sv->get_reference();
3820 Ref_pard* ref_pard = dynamic_cast<Ref_pard*>(ref);
3821 if (ref_pard)
3822 ref_pard->generate_code_cached(&expr);
3823 else
3824 ref->generate_code(&expr);
3825 break; }
3826
3827 default:
3828 FATAL_ERROR("VT NOT HANDLED");
3829 } // switch valuetype
3830 break; }
3831
3832 default:
3833 FATAL_ERROR("ttype not handled");
3834 break;
3835 }
3836
3837 str = mputprintf(str,
3838 "for (int i_i = 0, i_lim = %s.n_elem(); i_i < i_lim; ++i_i) {\n",
3839 expr.expr);
3840
3841 // Name for the LHS
3842 char *embedded_name = mprintf("%s.set_item(%lu%s + i_i)", name,
3843 (unsigned long) i, shifty.c_str());
3844 str = t->generate_code_init_all_from(str, embedded_name);
3845 Free(embedded_name);
3846
3847 str = mputstrn(str, "}\n", 2);
3848
3849 shifty += "-1+";
3850 shifty += expr.expr;
3851 shifty += ".n_elem() /* 3442 */";
3852 Code::free_expr(&expr);
3853 break; }
3854 case VALUE_LIST_ALL_FROM:
3855 FATAL_ERROR("Not possible");
3856 break; // not reached
3857
3858 default:
3859 if (t->needs_temp_ref()) {
3860 const string& tmp_id = get_temporary_id();
3861 const char *tmp_id_str = tmp_id.c_str();
3862 str = mputprintf(str, "{\n"
3863 "%s& %s = %s.set_item(%lu%s);\n",
3864 oftype_name_str, tmp_id_str, name, (unsigned long) i, shifty.c_str());
3865 str = t->generate_code_init(str, tmp_id_str);
3866 str = mputstr(str, "}\n");
3867 } else {
3868 char *embedded_name = mprintf("%s.set_item(%lu%s)", name,
3869 (unsigned long) i, shifty.c_str());
3870 str = t->generate_code_init(str, embedded_name);
3871 Free(embedded_name);
3872 }
3873 break;
3874 } // switch t->templatetype
3875 }
3876
3877 }
3878 else {
3879 str = mputprintf(str, "%s.set_type(%s, %lu);\n", name,
3880 is_superset ? "SUPERSET_MATCH" : "SUBSET_MATCH", (unsigned long) nof_ts);
3881 for (size_t i = 0; i < nof_ts; i++) {
3882 Template *t = u.templates->get_t_byIndex(i);
3883 if (t->needs_temp_ref()) {
3884 const string& tmp_id = get_temporary_id();
3885 const char *tmp_id_str = tmp_id.c_str();
3886 str = mputprintf(str, "{\n"
3887 "%s& %s = %s.set_item(%lu);\n",
3888 oftype_name_str, tmp_id_str, name, (unsigned long) i);
3889 str = t->generate_code_init(str, tmp_id_str);
3890 str = mputstr(str, "}\n");
3891 } else {
3892 char *embedded_name = mprintf("%s.set_item(%lu)", name,
3893 (unsigned long) i);
3894 str = t->generate_code_init(str, embedded_name);
3895 Free(embedded_name);
3896 }
3897 }
3898 }
3899 return str;
3900 }
3901
3902 void Template::generate_code_expr_invoke(expression_struct *expr)
3903 {
3904 Value *last_v = u.invoke.v->get_value_refd_last();
3905 if (last_v->get_valuetype() == Value::V_FUNCTION) {
3906 Common::Assignment *function = last_v->get_refd_fat();
3907 expr->expr = mputprintf(expr->expr, "%s(",
3908 function->get_genname_from_scope(my_scope).c_str());
3909 u.invoke.ap_list->generate_code_alias(expr,
3910 function->get_FormalParList(), function->get_RunsOnType(), false);
3911 } else {
3912 u.invoke.v->generate_code_expr_mandatory(expr);
3913 expr->expr = mputstr(expr->expr, ".invoke(");
3914 Type* gov_last = u.invoke.v->get_expr_governor_last();
3915 u.invoke.ap_list->generate_code_alias(expr, 0,
3916 gov_last->get_fat_runs_on_type(), gov_last->get_fat_runs_on_self());
3917 }
3918 expr->expr = mputc(expr->expr, ')');
3919 }
3920
3921 char *Template::rearrange_init_code_refd(char *str)
3922 {
3923 if (templatetype != TEMPLATE_REFD)
3924 FATAL_ERROR("Template::rearrange_init_code_refd()");
3925 ActualParList *parlist = u.ref.ref->get_parlist();
3926 // generate code for the templates that are used in the actual parameter
3927 // list of the reference
3928 Common::Assignment *ass = u.ref.ref->get_refd_assignment();
3929 bool rearrange = (ass->get_my_scope()->get_scope_mod_gen() ==
3930 my_scope->get_scope_mod_gen());
3931 if (parlist) str = parlist->rearrange_init_code(str, rearrange);
3932 // do nothing if the reference does not point to a template definition
3933 if (ass->get_asstype() != Common::Assignment::A_TEMPLATE) return str;
3934 // do nothing if the referenced template is in another module
3935 if (ass->get_my_scope()->get_scope_mod_gen() !=
3936 my_scope->get_scope_mod_gen()) return str;
3937 Template *t = ass->get_Template();
3938 if (parlist) {
3939 // the reference points to a parameterized template
3940 // we must perform the rearrangement for all non-parameterized templates
3941 // that are referred by the parameterized template regardless the
3942 // sub-references of u.ref.ref
3943 str = t->rearrange_init_code(str);
3944 } else {
3945 // the reference points to a non-parameterized template
3946 FieldOrArrayRefs *subrefs = u.ref.ref->get_subrefs();
3947 if (subrefs) {
3948 // we should follow the sub-references as much as we can
3949 // and perform the rearrangement for the referred field only
3950 for (size_t i = 0; i < subrefs->get_nof_refs(); i++) {
3951 FieldOrArrayRef *ref = subrefs->get_ref(i);
3952 if (ref->get_type() == FieldOrArrayRef::FIELD_REF) {
3953 // stop if the body does not have fields
3954 if (t->templatetype != NAMED_TEMPLATE_LIST) break;
3955 // get the sub-template of the referred field
3956 t = t->u.named_templates->get_nt_byName(*ref->get_id())
3957 ->get_template();
3958 } else {
3959 // stop if the body is not a list
3960 if (t->templatetype != TEMPLATE_LIST) break;
3961 Value *array_index = ref->get_val()->get_value_refd_last();
3962 // stop if the index cannot be evaluated at compile time
3963 if (array_index->get_valuetype() != Value::V_INT) break;
3964 Int index = array_index->get_val_Int()->get_val();
3965 // index transformation in case of arrays
3966 if (t->my_governor->get_typetype() == Type::T_ARRAY)
3967 index -= t->my_governor->get_dimension()->get_offset();
3968 // get the template with the given index
3969 t = t->get_listitem_byIndex(index);
3970 }
3971 }
3972 }
3973 // otherwise if the reference points to a top-level template
3974 // we should initialize its entire body
3975 str = t->generate_code_init(str, t->get_lhs_name().c_str());
3976 }
3977 return str;
3978 }
3979
3980 char *Template::rearrange_init_code_invoke(char *str)
3981 {
3982 str = u.invoke.v->rearrange_init_code(str);
3983 bool type_is_local = u.invoke.v->get_expr_governor_last()->get_my_scope()
3984 ->get_scope_mod_gen() == my_scope->get_scope_mod_gen();
3985 str = u.invoke.ap_list->rearrange_init_code(str, type_is_local);
3986 return str;
3987 }
3988
3989 bool Template::needs_temp_ref()
3990 {
3991 if (length_restriction || is_ifpresent) return true;
3992 switch (templatetype) {
3993 case OMIT_VALUE:
3994 case ANY_VALUE:
3995 case ANY_OR_OMIT:
3996 case SPECIFIC_VALUE:
3997 case TEMPLATE_REFD:
3998 case TEMPLATE_INVOKE:
3999 case BSTR_PATTERN:
4000 case HSTR_PATTERN:
4001 case OSTR_PATTERN:
4002 case CSTR_PATTERN:
4003 case USTR_PATTERN:
4004 return false;
4005 case TEMPLATE_LIST:
4006 // temporary reference is needed if the template has at least one
4007 // element (excluding not used symbols)
4008 for (size_t i = 0; i < u.templates->get_nof_ts(); i++) {
4009 if (u.templates->get_t_byIndex(i)->templatetype != TEMPLATE_NOTUSED)
4010 return true;
4011 }
4012 return false;
4013 case INDEXED_TEMPLATE_LIST:
4014 for (size_t i = 0; i < u.indexed_templates->get_nof_its(); i++) {
4015 if (u.indexed_templates->get_it_byIndex(i)->get_template()
4016 ->templatetype != TEMPLATE_NOTUSED)
4017 return true;
4018 }
4019 return false;
4020 case NAMED_TEMPLATE_LIST:
4021 return u.named_templates->get_nof_nts() > 1;
4022 case ALL_FROM:
4023 case VALUE_LIST_ALL_FROM:
4024 return false;
4025 case VALUE_LIST:
4026 case COMPLEMENTED_LIST:
4027 case VALUE_RANGE:
4028 case SUPERSET_MATCH:
4029 case SUBSET_MATCH:
4030 return true;
4031 case TEMPLATE_ERROR:
4032 case TEMPLATE_NOTUSED:
4033 FATAL_ERROR("Template::needs_temp_ref()");
4034 case PERMUTATION_MATCH:
4035 // FIXME
4036 return false;
4037 }
4038 return false;
4039 }
4040
4041 bool Template::has_single_expr()
4042 {
4043 if (length_restriction || is_ifpresent || get_needs_conversion())
4044 return false;
4045 switch (templatetype) {
4046 case OMIT_VALUE:
4047 case ANY_VALUE:
4048 case ANY_OR_OMIT:
4049 case BSTR_PATTERN:
4050 case HSTR_PATTERN:
4051 case OSTR_PATTERN:
4052 case CSTR_PATTERN:
4053 case USTR_PATTERN:
4054 return true;
4055 case SPECIFIC_VALUE:
4056 return u.specific_value->has_single_expr();
4057 case TEMPLATE_REFD: {
4058 Template *t_last = get_template_refd_last();
4059 if (t_last != this && t_last->has_single_expr()) {
4060 for (Scope *t_scope = my_scope; t_scope;
4061 t_scope = t_scope->get_parent_scope()) {
4062 // return true if t_last is visible from my scope
4063 if (t_scope == t_last->my_scope) return true;
4064 }
4065 }
4066 // otherwise consider the reference itself
4067 return u.ref.ref->has_single_expr(); }
4068 case TEMPLATE_INVOKE:
4069 if (!u.invoke.v->has_single_expr()) return false;
4070 for (size_t i = 0; i < u.invoke.ap_list->get_nof_pars(); i++)
4071 if (!u.invoke.ap_list->get_par(i)->has_single_expr()) return false;
4072 return true;
4073 case TEMPLATE_LIST:
4074 return u.templates->get_nof_ts() == 0;
4075 case NAMED_TEMPLATE_LIST: {
4076 if (!my_governor) FATAL_ERROR("Template::has_single_expr()");
4077 Type *type = my_governor->get_type_refd_last();
4078 return type->get_nof_comps() == 0; }
4079 case INDEXED_TEMPLATE_LIST:
4080 return u.indexed_templates->get_nof_its() == 0;
4081 case VALUE_LIST:
4082 case COMPLEMENTED_LIST:
4083 case VALUE_RANGE:
4084 case SUPERSET_MATCH:
4085 case SUBSET_MATCH:
4086 case PERMUTATION_MATCH:
4087 return false;
4088 case ALL_FROM:
4089 case VALUE_LIST_ALL_FROM:
4090 return false;
4091 default:
4092 FATAL_ERROR("Template::has_single_expr()");
4093 return false;
4094 }
4095 }
4096
4097 string Template::get_single_expr(bool cast_needed)
4098 {
4099 if (cast_needed && (length_restriction || is_ifpresent))
4100 FATAL_ERROR("Template::get_single_expr()");
4101 string ret_val;
4102 switch (templatetype) {
4103 case OMIT_VALUE:
4104 ret_val = "OMIT_VALUE";
4105 break;
4106 case ANY_VALUE:
4107 ret_val = "ANY_VALUE";
4108 break;
4109 case ANY_OR_OMIT:
4110 ret_val = "ANY_OR_OMIT";
4111 break;
4112 case SPECIFIC_VALUE:
4113 ret_val = u.specific_value->get_single_expr();
4114 break;
4115 case TEMPLATE_REFD: {
4116 // convert the reference to a single expression
4117 expression_struct expr;
4118 Code::init_expr(&expr);
4119 u.ref.ref->generate_code(&expr);
4120 if (expr.preamble || expr.postamble)
4121 FATAL_ERROR("Template::get_single_expr()");
4122 ret_val = expr.expr;
4123 Code::free_expr(&expr);
4124 return ret_val;
4125 }
4126 case TEMPLATE_INVOKE: {
4127 expression_struct expr;
4128 Code::init_expr(&expr);
4129 generate_code_expr_invoke(&expr);
4130 if (expr.preamble || expr.postamble)
4131 FATAL_ERROR("Template::get_single_expr()");
4132 ret_val = expr.expr;
4133 Code::free_expr(&expr);
4134 return ret_val; }
4135 case TEMPLATE_LIST:
4136 if (u.templates->get_nof_ts() != 0)
4137 FATAL_ERROR("Template::get_single_expr()");
4138 ret_val = "NULL_VALUE";
4139 break;
4140 case NAMED_TEMPLATE_LIST:
4141 if (u.named_templates->get_nof_nts() != 0)
4142 FATAL_ERROR("Template::get_single_expr()");
4143 ret_val = "NULL_VALUE";
4144 break;
4145 case INDEXED_TEMPLATE_LIST:
4146 if (u.indexed_templates->get_nof_its() != 0)
4147 FATAL_ERROR("Template::get_single_expr()");
4148 ret_val = "NULL_VALUE";
4149 break;
4150 case BSTR_PATTERN:
4151 return get_my_scope()->get_scope_mod_gen()
4152 ->add_bitstring_pattern(*u.pattern);
4153 case HSTR_PATTERN:
4154 return get_my_scope()->get_scope_mod_gen()
4155 ->add_hexstring_pattern(*u.pattern);
4156 case OSTR_PATTERN:
4157 return get_my_scope()->get_scope_mod_gen()
4158 ->add_octetstring_pattern(*u.pattern);
4159 case CSTR_PATTERN:
4160 case USTR_PATTERN:
4161 return u.pstring
4162 ->create_charstring_literals(get_my_scope()->get_scope_mod_gen());
4163 default:
4164 FATAL_ERROR("Template::get_single_expr()");
4165 }
4166 if (cast_needed) ret_val = my_governor->get_genname_template(my_scope) +
4167 "(" + ret_val + ")";
4168 return ret_val;
4169 }
4170
4171 void Template::dump(unsigned level) const
4172 {
4173 DEBUG(level, "%s", get_templatetype_str());
4174 switch (templatetype) {
4175 case TEMPLATE_ERROR:
4176 case OMIT_VALUE:
4177 case ANY_VALUE:
4178 case ANY_OR_OMIT:
4179 break;
4180 case SPECIFIC_VALUE:
4181 u.specific_value->dump(level+1);
4182 break;
4183 case TEMPLATE_REFD:
4184 u.ref.ref->dump(level+1);
4185 break;
4186 case TEMPLATE_INVOKE:
4187 u.invoke.v->dump(level+1);
4188 if (u.invoke.ap_list) u.invoke.ap_list->dump(level + 1);
4189 else if (u.invoke.t_list) u.invoke.t_list->dump(level + 1);
4190 break;
4191 case TEMPLATE_LIST:
4192 case VALUE_LIST:
4193 case COMPLEMENTED_LIST:
4194 case SUPERSET_MATCH:
4195 case SUBSET_MATCH:
4196 case PERMUTATION_MATCH:
4197 for (size_t i = 0; i < u.templates->get_nof_ts(); i++)
4198 u.templates->get_t_byIndex(i)->dump(level+1);
4199 break;
4200 case NAMED_TEMPLATE_LIST:
4201 for (size_t i = 0; i < u.named_templates->get_nof_nts(); i++)
4202 u.named_templates->get_nt_byIndex(i)->dump(level+1);
4203 break;
4204 case INDEXED_TEMPLATE_LIST:
4205 for (size_t i = 0; i < u.indexed_templates->get_nof_its(); i++)
4206 u.indexed_templates->get_it_byIndex(i)->dump(level+1);
4207 break;
4208 case VALUE_RANGE:
4209 u.value_range->dump(level);
4210 break;
4211 case BSTR_PATTERN:
4212 case HSTR_PATTERN:
4213 case OSTR_PATTERN:
4214 DEBUG(level+1, "%s", u.pattern->c_str());
4215 break;
4216 case CSTR_PATTERN:
4217 case USTR_PATTERN:
4218 u.pstring->dump(level+1);
4219 break;
4220 case ALL_FROM:
4221 case VALUE_LIST_ALL_FROM:
4222 u.all_from->dump(level+1);
4223 break;
4224 default:
4225 break;
4226 }
4227 if (length_restriction) length_restriction->dump(level + 1);
4228 }
4229
4230 bool Template::has_allfrom() const
4231 { // the code generation of all from is not fully implemented. This helps avoid of using it.
4232 if (templatetype != TEMPLATE_LIST) FATAL_ERROR("has_allfrom(): Templatetype shall be TEMPLATE_LIST");
4233 size_t nof_ts = u.templates->get_nof_ts();
4234 for (size_t i = 0; i < nof_ts; i++) {
4235 if (u.templates->get_t_byIndex(i)->templatetype == ALL_FROM) {
4236 return true;
4237 }
4238 }
4239 return false;
4240 }
4241
4242 // =================================
4243 // ===== TemplateInstance
4244 // =================================
4245
4246 TemplateInstance::TemplateInstance(const TemplateInstance& p)
4247 : Node(p), Location(p)
4248 {
4249 type = p.type ? p.type->clone() : 0;
4250 derived_reference = p.derived_reference ? p.derived_reference->clone() : 0;
4251 template_body = p.template_body->clone();
4252 }
4253
4254 TemplateInstance::TemplateInstance(Type *p_type, Ref_base *p_ref,
4255 Template *p_body) : Node(), Location(), type(p_type),
4256 derived_reference(p_ref), template_body(p_body)
4257 {
4258 if (!p_body) FATAL_ERROR("TemplateInstance::TemplateInstance()");
4259 if (type) type->set_ownertype(Type::OT_TEMPLATE_INST, this);
4260 }
4261
4262 TemplateInstance::~TemplateInstance()
4263 {
4264 delete type;
4265 delete derived_reference;
4266 delete template_body;
4267 }
4268
4269 void TemplateInstance::release()
4270 {
4271 type = 0;
4272 derived_reference = 0;
4273 template_body = 0;
4274 }
4275
4276 TemplateInstance *TemplateInstance::clone() const
4277 {
4278 return new TemplateInstance(*this);
4279 }
4280
4281 void TemplateInstance::set_fullname(const string& p_fullname)
4282 {
4283 Node::set_fullname(p_fullname);
4284 if (type) type->set_fullname(p_fullname + ".<type>");
4285 if (derived_reference)
4286 derived_reference->set_fullname(p_fullname + ".<derived_reference>");
4287 template_body->set_fullname(p_fullname);
4288 }
4289
4290 void TemplateInstance::set_my_scope(Scope *p_scope)
4291 {
4292 if (type) type->set_my_scope(p_scope);
4293 if (derived_reference) derived_reference->set_my_scope(p_scope);
4294 template_body->set_my_scope(p_scope);
4295 }
4296
4297 Type::typetype_t TemplateInstance::get_expr_returntype
4298 (Type::expected_value_t exp_val)
4299 {
4300 Type *t = get_expr_governor(exp_val);
4301 if (t) return t->get_type_refd_last()->get_typetype_ttcn3();
4302 else return template_body->get_expr_returntype(exp_val);
4303 }
4304
4305 Type *TemplateInstance::get_expr_governor(Type::expected_value_t exp_val)
4306 {
4307 if (type) return type;
4308 if (derived_reference) {
4309 Type *ret_val = chk_DerivedRef(0);
4310 if (ret_val) return ret_val;
4311 }
4312 return template_body->get_expr_governor(exp_val);
4313 }
4314
4315 void TemplateInstance::chk(Type *governor)
4316 {
4317 if (!governor) FATAL_ERROR("TemplateInstance::chk()");
4318 governor = chk_Type(governor);
4319 governor = chk_DerivedRef(governor);
4320 template_body->set_my_governor(governor);
4321 governor->chk_this_template_ref(template_body);
4322 governor->chk_this_template_generic(template_body,
4323 (derived_reference != 0 ? INCOMPLETE_ALLOWED : INCOMPLETE_NOT_ALLOWED),
4324 OMIT_ALLOWED, ANY_OR_OMIT_ALLOWED, SUB_CHK, NOT_IMPLICIT_OMIT, 0);
4325 }
4326
4327 Type *TemplateInstance::chk_Type(Type *governor)
4328 {
4329 if (!type) return governor;
4330 {
4331 Error_Context cntxt(type, "In explicit type specification");
4332 type->chk();
4333 }
4334 TypeCompatInfo info(template_body->get_template_refd_last()
4335 ->get_my_scope()->get_scope_mod(), governor, type,
4336 true, false);
4337 TypeChain l_chain;
4338 TypeChain r_chain;
4339 if (!governor) return type;
4340 else if (governor->is_compatible(type, &info, &l_chain, &r_chain)) {
4341 return governor;
4342 } else {
4343 if (info.is_subtype_error()) {
4344 type->error("%s", info.get_subtype_error().c_str());
4345 } else
4346 if (!info.is_erroneous()) {
4347 type->error("Incompatible explicit type specification: `%s' was "
4348 "expected instead of `%s'",
4349 governor->get_typename().c_str(),
4350 type->get_typename().c_str());
4351 } else {
4352 type->error("%s", info.get_error_str_str().c_str());
4353 }
4354 return type;
4355 }
4356 }
4357
4358 Type *TemplateInstance::chk_DerivedRef(Type *governor)
4359 {
4360 if (!derived_reference) return governor;
4361 Error_Context cntxt(derived_reference, "In derived reference");
4362 Common::Assignment *ass = derived_reference->get_refd_assignment();
4363 // the base template reference must not have sub-references
4364 if (derived_reference->get_subrefs())
4365 FATAL_ERROR("TemplateInstance::chk_DerivedRef()");
4366 bool set_bt = false;
4367 if (!ass) goto error;
4368 switch (ass->get_asstype()) {
4369 case Common::Assignment::A_TEMPLATE:
4370 set_bt = true;
4371 // no break
4372 case Common::Assignment::A_MODULEPAR_TEMP:
4373 case Common::Assignment::A_VAR_TEMPLATE:
4374 case Common::Assignment::A_PAR_TEMPL_IN:
4375 case Common::Assignment::A_PAR_TEMPL_OUT:
4376 case Common::Assignment::A_PAR_TEMPL_INOUT:
4377 case Common::Assignment::A_FUNCTION_RTEMP:
4378 case Common::Assignment::A_EXT_FUNCTION_RTEMP: {
4379 if (!governor) governor = type;
4380 Type *base_template_type = ass->get_Type();
4381 if (governor) {
4382 TypeCompatInfo info(template_body->get_template_refd_last()
4383 ->get_my_scope()->get_scope_mod(), governor, type,
4384 true, false);
4385 TypeChain l_chain;
4386 TypeChain r_chain;
4387 if (!governor->is_compatible(base_template_type, &info, &l_chain,
4388 &r_chain)) {
4389 if (info.is_subtype_error()) {
4390 derived_reference->error("%s", info.get_subtype_error().c_str());
4391 } else
4392 if (!info.is_erroneous()) {
4393 derived_reference->error("Base template `%s' has incompatible "
4394 "type: `%s' was expected instead of `%s'",
4395 ass->get_fullname().c_str(),
4396 governor->get_typename().c_str(),
4397 base_template_type->get_typename().c_str());
4398 } else {
4399 derived_reference->error("%s", info.get_error_str_str().c_str());
4400 }
4401 // if explicit type specification is omitted
4402 // check the template body against the type of the base template
4403 if (!type) governor = base_template_type;
4404 set_bt = false;
4405 } else {
4406 if (info.needs_conversion())
4407 template_body->set_needs_conversion();
4408 }
4409 } else governor = base_template_type;
4410 break; }
4411 default:
4412 derived_reference->error("Reference to a template was expected instead "
4413 "of %s", ass->get_description().c_str());
4414 goto error;
4415 }
4416 if (set_bt) template_body->set_base_template(ass->get_Template());
4417 return governor;
4418 error:
4419 // drop the erroneous derived_reference to avoid further errors
4420 delete derived_reference;
4421 derived_reference = 0;
4422 return governor;
4423 }
4424
4425 void TemplateInstance::chk_recursions(ReferenceChain& refch)
4426 {
4427 template_body->chk_recursions(refch);
4428 }
4429
4430 bool TemplateInstance::is_string_type(Type::expected_value_t exp_val)
4431 {
4432 switch (get_expr_returntype(exp_val)) {
4433 case Type::T_CSTR:
4434 case Type::T_USTR:
4435 case Type::T_BSTR:
4436 case Type::T_HSTR:
4437 case Type::T_OSTR:
4438 return true;
4439 default:
4440 return false;
4441 }
4442 }
4443
4444 bool TemplateInstance::chk_restriction(const char* definition_name,
4445 template_restriction_t template_restriction)
4446 {
4447 bool needs_runtime_check = false;
4448 if (derived_reference) // if modified
4449 {
4450 Common::Assignment *ass = derived_reference->get_refd_assignment();
4451 switch (ass->get_asstype()) {
4452 case Common::Assignment::A_TEMPLATE:
4453 // already added to template_body as base template by chk_DerivedRef()
4454 needs_runtime_check = template_body->chk_restriction(
4455 definition_name, template_restriction);
4456 break;
4457 case Common::Assignment::A_MODULEPAR_TEMP:
4458 case Common::Assignment::A_VAR_TEMPLATE:
4459 case Common::Assignment::A_EXT_FUNCTION_RTEMP:
4460 case Common::Assignment::A_FUNCTION_RTEMP:
4461 case Common::Assignment::A_PAR_TEMPL_IN:
4462 case Common::Assignment::A_PAR_TEMPL_OUT:
4463 case Common::Assignment::A_PAR_TEMPL_INOUT: {
4464 // create a temporary Template to be added as the base template and
4465 // check, remove and delete after checked
4466 if (template_body->get_base_template())
4467 FATAL_ERROR("TemplateInstance::chk_restriction()");
4468 template_body->set_base_template(new Template(derived_reference));
4469 needs_runtime_check = template_body->chk_restriction(
4470 definition_name, template_restriction);
4471 delete template_body->get_base_template();
4472 template_body->set_base_template(0);
4473 } break;
4474 default:
4475 break;
4476 }
4477 } else { // simple
4478 needs_runtime_check = template_body->chk_restriction(definition_name,
4479 template_restriction);
4480 }
4481 return needs_runtime_check;
4482 }
4483
4484 bool TemplateInstance::has_single_expr()
4485 {
4486 if (derived_reference) return false;
4487 else if (type) return false;
4488 else return template_body->has_single_expr();
4489 }
4490
4491 void TemplateInstance::set_code_section(
4492 GovernedSimple::code_section_t p_code_section)
4493 {
4494 if (derived_reference) derived_reference->set_code_section(p_code_section);
4495 template_body->set_code_section(p_code_section);
4496 }
4497
4498 bool TemplateInstance::needs_temp_ref()
4499 {
4500 if (template_body->get_templatetype() != Template::SPECIFIC_VALUE)
4501 return false;
4502 Value *val = template_body->get_specific_value();
4503 if (val->get_valuetype() == Value::V_REFD) {
4504 FieldOrArrayRefs *refs = val->get_reference()->get_subrefs();
4505 if (!refs) return false;
4506 // We need at least a 2-long reference chain. E.g. "a[0].b". 3.0.4
4507 // can't handle a normal reference following an indexed reference. The
4508 // indexed reference must be on the first place. Code like: "a.b[0].c"
4509 // compiles fine.
4510 if (refs->get_nof_refs() < 2
4511 || refs->get_ref(0)->get_type() != FieldOrArrayRef::ARRAY_REF)
4512 return false;
4513 } else {
4514 return false;
4515 }
4516 return true;
4517 }
4518
4519 void TemplateInstance::generate_code(expression_struct *expr,
4520 template_restriction_t template_restriction)
4521 {
4522 if (derived_reference) {
4523 // preserve the target expression
4524 char *expr_backup = expr->expr;
4525 // reset the space for the target expression
4526 expr->expr = NULL;
4527 derived_reference->generate_code(expr);
4528 // now the C++ equivalent of the base template reference is in expr->expr
4529 const string& tmp_id = template_body->get_temporary_id();
4530 const char *tmp_id_str = tmp_id.c_str();
4531 // create a temporary variable and copy the contents of base template
4532 // into it
4533 expr->preamble = mputprintf(expr->preamble, "%s %s(%s);\n",
4534 template_body->get_my_governor()->get_genname_template(
4535 template_body->get_my_scope()).c_str(), tmp_id_str, expr->expr);
4536 // perform the modifications on the temporary variable
4537 expr->preamble = template_body->generate_code_init(expr->preamble,
4538 tmp_id_str);
4539 // runtime template restriction check
4540 if (template_restriction!=TR_NONE)
4541 expr->preamble = Template::generate_restriction_check_code(
4542 expr->preamble, tmp_id_str, template_restriction);
4543 // the base template reference is no longer needed
4544 Free(expr->expr);
4545 // restore the target expression append the name of the temporary
4546 // variable to it
4547 expr->expr = mputstr(expr_backup, tmp_id_str);
4548 } else template_body->generate_code_expr(expr, template_restriction);
4549 }
4550
4551 char *TemplateInstance::rearrange_init_code(char *str)
4552 {
4553 if (derived_reference) {
4554 ActualParList *parlist = derived_reference->get_parlist();
4555 Common::Assignment *ass = derived_reference->get_refd_assignment();
4556 if (!ass) FATAL_ERROR("TemplateInstance::rearrange_init_code()");
4557 bool is_local = (ass->get_my_scope()->get_scope_mod_gen() ==
4558 derived_reference->get_my_scope()->get_scope_mod_gen());
4559 if (parlist) str = parlist->rearrange_init_code(str, is_local);
4560 if (is_local && ass->get_asstype() == Common::Assignment::A_TEMPLATE) {
4561 // the base template reference refers to a template within the local
4562 // module
4563 Template *t = ass->get_Template();
4564 if (parlist) {
4565 // the referred template is parameterized
4566 // the embedded referenced templates shall be visited
4567 str = t->rearrange_init_code(str);
4568 } else {
4569 // the referred template is not parameterized
4570 // its entire body has to be initialized now
4571 str = t->generate_code_init(str, t->get_lhs_name().c_str());
4572 }
4573 }
4574 }
4575 str = template_body->rearrange_init_code(str);
4576 return str;
4577 }
4578
4579 void TemplateInstance::append_stringRepr(string& str) const
4580 {
4581 if (type) {
4582 str += type->get_typename();
4583 str += " : ";
4584 }
4585 if (derived_reference) {
4586 str += "modifies ";
4587 str += derived_reference->get_dispname();
4588 str += " := ";
4589 }
4590 str += template_body->get_stringRepr();
4591 }
4592
4593 void TemplateInstance::dump(unsigned level) const
4594 {
4595 if (type) {
4596 DEBUG(level, "type:");
4597 type->dump(level + 1);
4598 }
4599 if (derived_reference) {
4600 DEBUG(level, "modifies:");
4601 derived_reference->dump(level + 1);
4602 }
4603 template_body->dump(level);
4604 }
4605
4606 Value* TemplateInstance::get_specific_value() const
4607 {
4608 if (type) return NULL;
4609 if (derived_reference) return NULL;
4610 if (template_body->is_length_restricted() || template_body->get_ifpresent())
4611 return NULL;
4612 if (template_body->get_templatetype()!=Template::SPECIFIC_VALUE) return NULL;
4613 return template_body->get_specific_value();
4614 }
4615
4616 Def_Template* TemplateInstance::get_Referenced_Base_Template()
4617 { // it may return 0
4618 if (!get_Template()) return NULL;
4619 Ttcn::Template::templatetype_t tpt = get_Template()->get_templatetype();
4620 if (Ttcn::Template::TEMPLATE_REFD != tpt) return NULL;
4621 Ttcn::Ref_base* refbase = get_Template()->get_reference();
4622 Ttcn::Reference * ref = dynamic_cast<Ttcn::Reference*>(refbase);
4623 if (!ref) return NULL;
4624 Common::Assignment* ass = ref->get_refd_assignment();
4625 Ttcn::Definition* def = dynamic_cast<Ttcn::Definition*>(ass);
4626 if (!def) return NULL;
4627 Ttcn::Def_Template* deftemp = dynamic_cast<Ttcn::Def_Template*>(def);
4628 if (!deftemp) return NULL;
4629
4630 return deftemp;
4631 }
4632
4633}
4634
This page took 0.189132 seconds and 5 git commands to generate.