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"
12 #include "../TypeCompat.hh"
13 #include "../SigParam.hh"
14 #include "../CompField.hh"
15 #include "../Valuestuff.hh"
16 #include "ArrayDimensions.hh"
17 #include "PatternString.hh"
19 #include "../../common/dbgnew.hh"
20 #include "Attributes.hh"
24 // =================================
26 // =================================
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
)
33 switch (templatetype
) {
35 case TEMPLATE_NOTUSED
:
41 u
.specific_value
= p
.u
.specific_value
->clone();
44 u
.ref
.ref
= p
.u
.ref
.ref
->clone();
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;
54 case VALUE_LIST_ALL_FROM
:
55 u
.all_from
= p
.u
.all_from
->clone();
59 case COMPLEMENTED_LIST
:
62 case PERMUTATION_MATCH
:
63 u
.templates
= p
.u
.templates
->clone(); // FATAL_ERROR
65 case NAMED_TEMPLATE_LIST
:
66 u
.named_templates
= p
.u
.named_templates
->clone(); // FATAL_ERROR
68 case INDEXED_TEMPLATE_LIST
:
69 u
.indexed_templates
= p
.u
.indexed_templates
->clone(); // FATAL_ERROR
72 u
.value_range
= p
.u
.value_range
->clone();
77 u
.pattern
= new string(*p
.u
.pattern
);
81 u
.pstring
= p
.u
.pstring
->clone();
84 // FATAL_ERROR("Template::Template()");
87 p
.length_restriction
? p
.length_restriction
->clone() : 0; // FATAL_ERR
90 void Template::clean_up()
92 switch(templatetype
) {
94 case TEMPLATE_NOTUSED
:
100 delete u
.specific_value
;
105 case TEMPLATE_INVOKE
:
107 delete u
.invoke
.t_list
;
108 delete u
.invoke
.ap_list
;
112 case COMPLEMENTED_LIST
:
115 case PERMUTATION_MATCH
:
119 case VALUE_LIST_ALL_FROM
:
122 case NAMED_TEMPLATE_LIST
:
123 delete u
.named_templates
;
125 case INDEXED_TEMPLATE_LIST
:
126 delete u
.indexed_templates
;
129 delete u
.value_range
;
141 // FATAL_ERROR("Template::clean_up()");
145 string
Template::create_stringRepr()
148 switch (templatetype
) {
150 ret_val
+= "<erroneous template>";
152 case TEMPLATE_NOTUSED
:
165 ret_val
+= u
.specific_value
->get_stringRepr();
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();
173 case TEMPLATE_INVOKE
: {
174 ret_val
+= u
.invoke
.v
->get_stringRepr();
175 ret_val
+= ".invoke(";
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();
184 if (u
.templates
->get_nof_ts() > 0) {
186 u
.templates
->append_stringRepr(ret_val
);
188 } else ret_val
+= "{ }";
190 case NAMED_TEMPLATE_LIST
:
192 for (size_t i
= 0; i
< u
.named_templates
->get_nof_nts(); i
++) {
193 if (i
> 0) ret_val
+= ", ";
195 NamedTemplate
*nt
= u
.named_templates
->get_nt_byIndex(i
);
196 ret_val
+= nt
->get_name().get_dispname();
198 ret_val
+= nt
->get_template()->get_stringRepr();
202 case INDEXED_TEMPLATE_LIST
:
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
);
210 ret_val
+= it
->get_template()->get_stringRepr();
216 u
.templates
->append_stringRepr(ret_val
);
219 case COMPLEMENTED_LIST
:
220 ret_val
+= "complement(";
221 u
.templates
->append_stringRepr(ret_val
);
225 u
.value_range
->append_stringRepr(ret_val
);
228 ret_val
+= "superset(";
229 u
.templates
->append_stringRepr(ret_val
);
233 ret_val
+= "subset(";
234 u
.templates
->append_stringRepr(ret_val
);
237 case PERMUTATION_MATCH
:
238 ret_val
+= "permutation(";
239 u
.templates
->append_stringRepr(ret_val
);
244 ret_val
+= *u
.pattern
;
249 ret_val
+= *u
.pattern
;
254 ret_val
+= *u
.pattern
;
259 ret_val
+= "pattern \"";
260 ret_val
+= u
.pstring
->get_full_str();
264 ret_val
+= "<unknown template>";
267 if (length_restriction
) length_restriction
->append_stringRepr(ret_val
);
268 if (is_ifpresent
) ret_val
+= " ifpresent";
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)
280 case TEMPLATE_NOTUSED
:
286 FATAL_ERROR("Template::Template()");
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)
296 if (!v
) FATAL_ERROR("Template::Template()");
297 u
.specific_value
= v
;
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)
306 if(!p_ref
) FATAL_ERROR("Template::Template()");
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)
321 case COMPLEMENTED_LIST
:
324 case PERMUTATION_MATCH
:
327 FATAL_ERROR("Template::Template()");
329 if (!ts
) FATAL_ERROR("Template::Template()");
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;
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)
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)
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)
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
;
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)
372 if (!nts
) FATAL_ERROR("Template::Template()");
373 u
.named_templates
= nts
;
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),
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
==
389 has_permutation
= true;
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)
401 if (!vr
) FATAL_ERROR("Template::Template()");
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)
417 FATAL_ERROR("Template::Template()");
419 if (!p_patt
) FATAL_ERROR("Template::Template()");
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)
429 if (!p_ps
) FATAL_ERROR("Template::Template()");
433 Template::~Template()
436 delete length_restriction
;
439 Template
*Template::clone() const
441 return new Template(*this);
444 void Template::set_fullname(const string
& p_fullname
)
446 GovernedSimple::set_fullname(p_fullname
);
447 switch (templatetype
) {
449 case TEMPLATE_NOTUSED
:
458 u
.specific_value
->set_fullname(p_fullname
);
461 u
.ref
.ref
->set_fullname(p_fullname
);
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
);
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
) + "]");
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
) + "]");
480 case NAMED_TEMPLATE_LIST
:
481 u
.named_templates
->set_fullname(p_fullname
);
484 case VALUE_LIST_ALL_FROM
:
485 u
.all_from
->set_fullname(p_fullname
);
488 case COMPLEMENTED_LIST
:
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
) + ")");
498 u
.value_range
->set_fullname(p_fullname
);
502 u
.pstring
->set_fullname(p_fullname
);
505 // FATAL_ERROR("Template::set_fullname()");
507 if (length_restriction
)
508 length_restriction
->set_fullname(p_fullname
+ ".<length_restriction>");
511 void Template::set_my_scope(Scope
*p_scope
)
513 GovernedSimple::set_my_scope(p_scope
);
514 switch (templatetype
) {
516 case TEMPLATE_NOTUSED
:
525 u
.specific_value
->set_my_scope(p_scope
);
528 u
.ref
.ref
->set_my_scope(p_scope
);
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
);
536 case VALUE_LIST_ALL_FROM
:
537 u
.all_from
->set_my_scope(p_scope
);
541 case COMPLEMENTED_LIST
:
544 case PERMUTATION_MATCH
:
545 u
.templates
->set_my_scope(p_scope
);
547 case NAMED_TEMPLATE_LIST
:
548 u
.named_templates
->set_my_scope(p_scope
);
550 case INDEXED_TEMPLATE_LIST
:
551 u
.indexed_templates
->set_my_scope(p_scope
);
554 u
.value_range
->set_my_scope(p_scope
);
558 u
.pstring
->set_my_scope(p_scope
);
561 // FATAL_ERROR("Template::set_my_scope()");
563 if (length_restriction
) length_restriction
->set_my_scope(p_scope
);
566 void Template::set_genname_recursive(const string
& p_genname
)
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();
574 if (type
->get_typetype() == Type::T_ARRAY
)
575 offset
= type
->get_dimension()->get_offset();
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
);
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
);
606 void Template::set_genname_prefix(const char *p_genname_prefix
)
608 GovernedSimple::set_genname_prefix(p_genname_prefix
);
609 switch (templatetype
) {
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
);
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
);
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
);
629 void Template::set_code_section(code_section_t p_code_section
)
631 GovernedSimple::set_code_section(p_code_section
);
632 switch (templatetype
) {
634 u
.specific_value
->set_code_section(p_code_section
);
637 u
.ref
.ref
->set_code_section(p_code_section
);
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
);
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
);
648 case COMPLEMENTED_LIST
:
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
);
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
);
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
);
666 u
.value_range
->set_code_section(p_code_section
);
670 u
.pstring
->set_code_section(p_code_section
);
675 if (length_restriction
)
676 length_restriction
->set_code_section(p_code_section
);
679 void Template::set_templatetype(templatetype_t p_templatetype
)
681 if (p_templatetype
== templatetype
) return;
682 if (p_templatetype
== TEMPLATE_ERROR
) {
684 templatetype
= TEMPLATE_ERROR
;
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();
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
);
703 FATAL_ERROR("Template::set_templatetype()");
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();
720 case Type::T_SIGNATURE
:
724 if (nof_temps
== 0 && nof_comps
== 0) break;
726 FATAL_ERROR("Template::set_templatetype()");
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
:
736 case Type::T_SIGNATURE
: // be precise
738 default: // not possible, fatal error ?
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
);
749 size_t upper_limit
; // min(nof_temps, nof_comps)
751 if (nof_temps
<= nof_comps
) {
752 upper_limit
= nof_temps
;
755 upper_limit
= nof_comps
;
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;
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
);
769 u
.named_templates
->set_my_scope(get_my_scope());
770 u
.named_templates
->set_fullname(get_fullname());
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()");
777 case CSTR_PATTERN
: // current type
778 if (p_templatetype
== USTR_PATTERN
)
779 templatetype
= USTR_PATTERN
;
781 FATAL_ERROR("Template::set_templatetype()");
784 FATAL_ERROR("Template::set_templatetype()");
786 templatetype
= p_templatetype
;
789 const char *Template::get_templatetype_str() const
791 switch(templatetype
) {
793 return "erroneous template";
794 case TEMPLATE_NOTUSED
:
795 return "not used symbol";
801 return "any or omit";
803 return "specific value";
805 return "referenced template";
806 case TEMPLATE_INVOKE
:
807 return "template returning invoke";
809 case VALUE_LIST_ALL_FROM
:
810 return "template with 'all from'";
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";
818 return "value range match";
820 return "value list match";
821 case COMPLEMENTED_LIST
:
822 return "complemented list match";
824 return "superset match";
826 return "subset match";
827 case PERMUTATION_MATCH
:
828 return "permutation match";
830 return "bitstring pattern";
832 return "hexstring pattern";
834 return "octetstring pattern";
836 return "character string pattern";
838 return "universal string pattern";
840 return "unknown template";
844 bool Template::is_undef_lowerid()
846 return templatetype
== SPECIFIC_VALUE
&&
847 u
.specific_value
->is_undef_lowerid();
850 void Template::set_lowerid_to_ref()
852 switch (templatetype
) {
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);
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
);
872 } else set_templatetype(TEMPLATE_ERROR
);
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();
881 u
.value_range
->set_lowerid_to_ref();
888 Type::typetype_t
Template::get_expr_returntype(Type::expected_value_t exp_val
)
890 switch (templatetype
) {
892 return Type::T_ERROR
;
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
; }
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
;
910 return Type::T_UNDEF
;
912 return u
.value_range
->get_expr_returntype(exp_val
);
915 return Type::T_SETOF
;
927 return Type::T_UNDEF
;
931 Type
*Template::get_expr_governor(Type::expected_value_t exp_val
)
933 if (my_governor
) return my_governor
;
934 switch (templatetype
) {
936 return u
.specific_value
->get_expr_governor(exp_val
);
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
);
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
);
952 case TEMPLATE_INVOKE
: {
953 Type
*t
= u
.invoke
.v
->get_expr_governor(Type::EXPECTED_DYNAMIC_VALUE
);
955 if(u
.invoke
.v
->get_valuetype() != Value::V_ERROR
)
956 u
.invoke
.v
->error("A value of type function expected");
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());
968 return t
->get_function_return_type();
969 case Type::T_ALTSTEP
:
972 u
.invoke
.v
->error("A value of type function expected instead of `%s'",
973 t
->get_typename().c_str());
978 return Type::get_pooltype(get_expr_returntype(exp_val
));
981 set_templatetype(TEMPLATE_ERROR
);
985 void Template::set_my_governor(Type
*p_gov
)
988 FATAL_ERROR("Template::set_my_governor(): NULL parameter");
992 Type
*Template::get_my_governor() const
997 void Template::set_length_restriction(LengthRestriction
*p_lr
)
999 if (length_restriction
) FATAL_ERROR("Template::set_length_restriction()");
1000 length_restriction
= p_lr
;
1003 Template::completeness_t
1004 Template::get_completeness_condition_seof(bool incomplete_allowed
)
1006 if (!incomplete_allowed
) return C_MUST_COMPLETE
;
1007 else if (!base_template
) return C_MAY_INCOMPLETE
;
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
;
1022 switch (my_governor
->get_type_refd_last()->get_typetype()) {
1025 // only the first elements can be incomplete
1028 // we are in error recovery
1029 return C_MAY_INCOMPLETE
;
1031 break; // should not get here
1033 // partial overwriting is not allowed for literal specific values,
1034 // matching ranges/lists/sets and patterns
1035 return C_MUST_COMPLETE
;
1040 Template::completeness_t
Template::get_completeness_condition_choice
1041 (bool incomplete_allowed
, const Identifier
& p_fieldname
)
1043 if (!incomplete_allowed
) return C_MUST_COMPLETE
;
1044 else if (!base_template
) return C_MAY_INCOMPLETE
;
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
;
1062 // partial overwriting is not allowed for literal specific values,
1063 // matching ranges/lists/sets and patterns
1064 return C_MUST_COMPLETE
;
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
);
1075 Value
*Template::get_specific_value() const
1077 if (templatetype
!= SPECIFIC_VALUE
)
1078 FATAL_ERROR("Template::get_specific_value()");
1079 return u
.specific_value
;
1082 Ref_base
*Template::get_reference() const
1084 if (templatetype
!= TEMPLATE_REFD
)
1085 FATAL_ERROR("Template::get_reference()");
1089 ValueRange
*Template::get_value_range() const
1091 if (templatetype
!= VALUE_RANGE
)
1092 FATAL_ERROR("Template::get_value_range()");
1093 return u
.value_range
;
1096 PatternString
* Template::get_cstr_pattern() const
1098 if (templatetype
!= CSTR_PATTERN
)
1099 FATAL_ERROR("Template::get_cstr_pattern()");
1103 PatternString
* Template::get_ustr_pattern() const
1105 if (templatetype
!= USTR_PATTERN
)
1106 FATAL_ERROR("Template::get_ustr_pattern()");
1110 size_t Template::get_nof_comps() const
1112 switch (templatetype
) {
1115 case COMPLEMENTED_LIST
:
1116 case SUPERSET_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();
1125 FATAL_ERROR("Template::get_of_comps()");
1130 Template
*Template::get_temp_byIndex(size_t n
) const
1132 switch (templatetype
) {
1135 case COMPLEMENTED_LIST
:
1136 case SUPERSET_MATCH
:
1138 case PERMUTATION_MATCH
:
1139 return u
.templates
->get_t_byIndex(n
);
1141 FATAL_ERROR("Template::get_temp_byIndex()");
1146 IndexedTemplate
*Template::get_indexedtemp_byIndex(size_t n
) const
1148 if (templatetype
!= INDEXED_TEMPLATE_LIST
)
1149 FATAL_ERROR("Template::get_indexedtemp_byIndex()");
1150 return u
.indexed_templates
->get_it_byIndex(n
);
1153 NamedTemplate
*Template::get_namedtemp_byIndex(size_t n
) const
1155 if (templatetype
!= NAMED_TEMPLATE_LIST
)
1156 FATAL_ERROR("Template::get_namedtemp_byIndex()");
1157 return u
.named_templates
->get_nt_byIndex(n
);
1160 Template
*Template::get_all_from() const
1162 if (templatetype
!= ALL_FROM
1163 &&templatetype
!= VALUE_LIST_ALL_FROM
)
1164 FATAL_ERROR("Template::get_all_from()");
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
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();
1183 } else return u
.templates
->get_nof_ts();
1186 Template
*Template::get_listitem_byIndex(size_t n
) const
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
;
1200 if (n
== index
) return t
;
1204 FATAL_ERROR("Template::get_listitem_byIndex(): index overflow");
1206 } else return u
.templates
->get_t_byIndex(n
);
1209 /** \todo revise and merge with get_template_refd() */
1210 Template
* Template::get_template_refd_last(ReferenceChain
*refch
)
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
;
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
;
1227 // otherwise evaluate the reference
1230 refch
->mark_state();
1231 destroy_refch
= false;
1233 refch
= new ReferenceChain(this, "While searching referenced template");
1234 destroy_refch
= true;
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
);
1244 ret_val
= u
.ref
.refd_last
;
1246 // a circular reference was found
1247 set_templatetype(TEMPLATE_ERROR
);
1250 if (destroy_refch
) delete refch
;
1251 else refch
->prev_state();
1255 Template
* Template::get_refd_sub_template(Ttcn::FieldOrArrayRefs
*subrefs
,
1257 ReferenceChain
*refch
)
1259 if (!subrefs
) return this;
1261 for (size_t i
=0; i
<subrefs
->get_nof_refs(); i
++) {
1263 t
=t
->get_template_refd_last(refch
);
1264 t
->set_lowerid_to_ref();
1265 switch(t
->templatetype
) {
1266 case TEMPLATE_ERROR
:
1269 case INDEXED_TEMPLATE_LIST
:
1272 case SPECIFIC_VALUE
:
1273 (void)t
->u
.specific_value
->get_refd_sub_value(
1274 subrefs
, i
, usedInIsbound
, refch
); // only to report errors
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
,
1283 else t
=t
->get_refd_array_template(ref
->get_val(), usedInIsbound
, refch
);
1288 Template
* Template::get_template_refd(ReferenceChain
*refch
)
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
);
1304 // Why do we not set u.ref.refd_last ?
1306 else if (subrefs
&& subrefs
->has_unfoldable_index()) {
1307 // some array indices could not be evaluated
1309 u
.ref
.refd_last
= this;
1310 } else if (u
.ref
.ref
->getUsedInIsbound()) {
1312 u
.ref
.refd_last
= this;
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");
1324 set_templatetype(TEMPLATE_ERROR
);
1328 // the reference is unfoldable
1334 Template
* Template::get_refd_field_template(const Identifier
& field_id
,
1335 const Location
& loc
, bool usedInIsbound
, ReferenceChain
*refch
)
1337 switch (templatetype
) {
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());
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()) {
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());
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);
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 ");
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());
1389 return nt
->get_template();
1394 typetype_str
="record";
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());
1403 } else if (templatetype
!= NAMED_TEMPLATE_LIST
) {
1404 // this is an invalid matching mechanism
1405 // the error should be already reported
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
);
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());
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());
1431 Template
* Template::get_refd_array_template(Value
*array_index
,
1433 ReferenceChain
*refch
)
1435 switch (templatetype
) {
1440 case COMPLEMENTED_LIST
:
1441 case SUPERSET_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());
1451 Value
*v_index
= array_index
->get_value_refd_last(refch
);
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;
1459 array_index
->error("An integer value was expected as index");
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()) {
1470 typetype_str
="record";
1473 if (index_available
) {
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());
1480 } else if (templatetype
!= TEMPLATE_LIST
) {
1481 // remain silent the error has been already reported
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
);
1494 // the index is not available or the error has been reported above
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()))
1509 // remain silent, the error has been already reported
1513 // the index is not available or the error has been reported above
1518 array_index
->error("Invalid array element reference: type `%s' cannot "
1519 "be indexed", t
->get_typename().c_str());
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
);
1529 if(ret_val
->get_templatetype() == TEMPLATE_NOTUSED
)
1530 error("Not used symbol is not allowed in this context");
1533 } else return ret_val
;
1536 bool Template::temps_contains_anyornone_symbol() const
1538 switch (templatetype
) {
1540 case SUPERSET_MATCH
:
1542 case PERMUTATION_MATCH
:
1545 FATAL_ERROR("Template::temps_contains_anyornone_symbol()");
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
) {
1553 case PERMUTATION_MATCH
:
1555 if (t
->temps_contains_anyornone_symbol()) return true;
1564 size_t Template::get_nof_comps_not_anyornone() const
1566 switch (templatetype
) {
1568 case SUPERSET_MATCH
:
1570 case PERMUTATION_MATCH
:
1573 FATAL_ERROR("Template::get_nof_comps_not_anyornone()");
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
) {
1583 case PERMUTATION_MATCH
:
1585 ret_val
+= t
->get_nof_comps_not_anyornone();
1588 // other types are counted as 1
1596 bool Template::pattern_contains_anyornone_symbol() const
1598 switch (templatetype
) {
1602 return u
.pattern
->find('*') < u
.pattern
->size();
1607 FATAL_ERROR("Template::pattern_contains_anyornone_symbol()");
1612 size_t Template::get_min_length_of_pattern() const
1615 switch (templatetype
) {
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
++;
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
]) {
1636 // count as 1 and skip over the next hex digit
1646 FATAL_ERROR("Template::get_min_length_of_pattern()");
1651 bool Template::is_Value() const
1653 if (length_restriction
|| is_ifpresent
) return false;
1654 switch (templatetype
) {
1655 case TEMPLATE_ERROR
:
1656 case TEMPLATE_NOTUSED
:
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;
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;
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;
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;
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
:
1704 Value
*Template::get_Value()
1707 switch(templatetype
) {
1708 case TEMPLATE_ERROR
:
1709 ret_val
= new Value(Value::V_ERROR
);
1711 case TEMPLATE_NOTUSED
:
1712 ret_val
= new Value(Value::V_NOTUSED
);
1715 ret_val
= new Value(Value::V_OMIT
);
1717 case SPECIFIC_VALUE
:
1718 ret_val
= u
.specific_value
;
1719 u
.specific_value
= 0;
1720 set_templatetype(TEMPLATE_ERROR
);
1722 case TEMPLATE_LIST
: {
1723 Values
*vs
= new Values
;
1724 size_t nof_ts
= u
.templates
->get_nof_ts();
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();
1731 ret_val
= new Value(Value::V_SEQOF
, vs
);
1732 if (gov
) ret_val
->set_my_governor(gov
->get_parent_type());
1734 case NAMED_TEMPLATE_LIST
: {
1735 NamedValues
*nvs
= new NamedValues
;
1736 size_t nof_nts
= u
.named_templates
->get_nof_nts();
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
);
1746 ret_val
= new Value(Value::V_SEQ
, nvs
);
1747 if (gov
) ret_val
->set_my_governor(gov
->get_parent_type());
1749 case INDEXED_TEMPLATE_LIST
: {
1750 Values
*ivs
= new Values(true);
1751 size_t nof_its
= u
.indexed_templates
->get_nof_its();
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
);
1761 ret_val
= new Value(Value::V_SEQOF
, ivs
);
1762 if (gov
) ret_val
->set_my_governor(gov
->get_parent_type());
1765 FATAL_ERROR("Template::get_Value()");
1769 ret_val
->set_location(*this);
1770 ret_val
->set_my_scope(get_my_scope());
1771 ret_val
->set_fullname(get_fullname());
1775 bool Template::is_Ref() const
1777 if (length_restriction
|| is_ifpresent
|| templatetype
!= SPECIFIC_VALUE
)
1779 Value
*v
= u
.specific_value
;
1780 switch (v
->get_valuetype()) {
1781 case Value::V_UNDEF_LOWERID
:
1784 if (dynamic_cast<Ref_base
*>(v
->get_reference())) return true;
1791 Ref_base
*Template::get_Ref()
1793 if (templatetype
!= SPECIFIC_VALUE
)
1794 FATAL_ERROR("Template::get_Ref()");
1795 return u
.specific_value
->steal_ttcn_ref_base();
1798 void Template::chk_recursions(ReferenceChain
& refch
)
1800 if (recurs_checked
) return;
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;
1812 t
->set_lowerid_to_ref();
1813 switch (t
->templatetype
) {
1814 case SPECIFIC_VALUE
:
1815 t
->u
.specific_value
->chk_recursions(refch
);
1819 case COMPLEMENTED_LIST
:
1820 case SUPERSET_MATCH
:
1822 case PERMUTATION_MATCH
:
1823 for (size_t i
= 0; i
< t
->u
.templates
->get_nof_ts(); i
++) {
1825 t
->u
.templates
->get_t_byIndex(i
)->chk_recursions(refch
);
1829 case NAMED_TEMPLATE_LIST
:
1830 for (size_t i
= 0; i
< t
->u
.named_templates
->get_nof_nts(); i
++) {
1832 t
->u
.named_templates
->get_nt_byIndex(i
)
1833 ->get_template()->chk_recursions(refch
);
1837 case INDEXED_TEMPLATE_LIST
:
1838 for (size_t i
= 0; i
< t
->u
.indexed_templates
->get_nof_its(); i
++) {
1840 t
->u
.indexed_templates
->get_it_byIndex(i
)
1841 ->get_template()->chk_recursions(refch
);
1847 t
->u
.pstring
->chk_recursions(refch
);
1853 recurs_checked
= true;
1856 void Template::chk_specific_value(bool allow_omit
)
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");
1862 chk_specific_value_generic();
1865 void Template::chk_specific_value_generic()
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
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
);
1885 case TEMPLATE_INVOKE
:
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();
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();
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();
1905 t
->error("A specific value was expected instead of %s",
1906 t
->get_templatetype_str());
1909 t
->specific_value_checked
= true;
1910 specific_value_checked
= true;
1913 void Template::chk_invoke()
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();
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
);
1927 if (u
.invoke
.v
->get_valuetype() != Value::V_ERROR
)
1928 u
.invoke
.v
->error("A value of type function was expected in the "
1930 set_templatetype(TEMPLATE_ERROR
);
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;
1941 u
.invoke
.ap_list
= 0;
1943 parlist
->set_fullname(get_fullname());
1944 parlist
->set_my_scope(get_my_scope());
1945 u
.invoke
.ap_list
= parlist
;
1949 Templates
*Template::harbinger(Template
*t
, bool from_permutation
, bool killer
)
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;
1964 Common::Assignment
*ass
= ref
->get_refd_assignment();
1965 if (ass
== NULL
) { // perhaps erroneous
1969 Common::Assignment::asstype_t asst
= ass
->get_asstype();
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
;
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();
1985 case TEMPLATE_REFD
: {
1986 delete new_templates
;
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
: {
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;
2009 if (from_permutation
) {
2010 break; // AnyElementOrNone allowed in "all from" now
2013 case PERMUTATION_MATCH
:
2014 t
->error("'all from' can not refer to permutation or AnyElementsOrNone");
2015 t
->set_templatetype(TEMPLATE_ERROR
);
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
);
2029 // Cannot flatten at compile time
2030 delete new_templates
;
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()) {
2043 case PERMUTATION_MATCH
:
2044 t
->error("'all from' can not refer to permutation or AnyElementsOrNone");
2045 t
->set_templatetype(TEMPLATE_ERROR
);
2050 delete new_templates
;
2057 tpl
->error("Matching mechanism can not be used as target of 'all from'");
2060 FATAL_ERROR("harbinger");
2064 if (killer
) delete t
;
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
);
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());
2085 } // switch(valuetype)
2086 if (killer
) delete t
;
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
2101 FATAL_ERROR("harbinger asst %d", asst
);
2107 FATAL_ERROR("unexpected all from inside all from");
2110 FATAL_ERROR("tt %d", t
->u
.all_from
->templatetype
);
2113 return new_templates
;
2116 bool Template::flatten(bool from_permutation
)
2118 switch (templatetype
) {
2121 case COMPLEMENTED_LIST
:
2122 case SUPERSET_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);
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
);
2145 new_templates
->add_t(t
); // transfer it unchanged
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);
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
2163 new_templates
->add_t(t
); // transfer it unchanged
2166 break; } // case ALL_FROM
2168 case PERMUTATION_MATCH
: {
2169 // permut inside a value list: { 1, permut(), 2 }
2170 flattened
&= t
->flatten(true);
2171 new_templates
->add_t(t
);
2175 // case COMPLEMENTED_LIST:
2176 // case SUPERSET_MATCH:
2177 // case SUBSET_MATCH:
2179 flattened
&=t
->flatten(false);
2180 new_templates
->add_t(t
);
2183 new_templates
->add_t(t
);
2186 t
= 0; // take it away from the original
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());
2196 case VALUE_LIST_ALL_FROM
: {
2197 Templates
*new_templates
= harbinger(this, from_permutation
, false);
2198 if (new_templates
) {
2200 // now we can change the type
2201 templatetype
= VALUE_LIST
;
2202 u
.templates
= new_templates
;
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
:
2213 case BSTR_PATTERN
: case HSTR_PATTERN
: case OSTR_PATTERN
:
2214 case CSTR_PATTERN
: case USTR_PATTERN
:
2218 set_fullname(get_fullname()); // recompute fullname (esp. for components)
2219 set_my_scope(get_my_scope());
2224 const char* Template::get_restriction_name(template_restriction_t tr
)
2236 FATAL_ERROR("Template::get_restriction_name()");
2241 template_restriction_t
Template::get_sub_restriction(
2242 template_restriction_t tr
, Ref_base
* ref
)
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();
2249 Type
* t
= ass
->get_Type();
2251 ReferenceChain
refch(ref
, "While checking template restriction");
2252 t
= t
->get_field_type(ref
->get_subrefs(), Type::EXPECTED_TEMPLATE
,
2254 if (t
) is_optional
= false;
2263 return is_optional
? TR_OMIT
: TR_VALUE
;
2265 return is_optional
? TR_NONE
: TR_PRESENT
;
2267 FATAL_ERROR("Template::get_sub_restriction()");
2272 bool Template::is_less_restrictive(template_restriction_t needed_tr
,
2273 template_restriction_t refd_tr
)
2275 switch (needed_tr
) {
2279 return refd_tr
!=TR_VALUE
;
2281 return refd_tr
!=TR_VALUE
&& refd_tr
!=TR_OMIT
;
2283 return refd_tr
!=TR_VALUE
&& refd_tr
!=TR_PRESENT
;
2285 FATAL_ERROR("Template::is_less_restrictive()");
2290 char* Template::generate_restriction_check_code(char* str
, const char* name
,
2291 template_restriction_t tr
)
2293 const char* tr_name
;
2298 tr_name
= "TR_OMIT";
2301 tr_name
= "TR_VALUE";
2304 tr_name
= "TR_PRESENT";
2307 FATAL_ERROR("Template::generate_restriction_check_code()");
2309 return mputprintf(str
, "%s.check_restriction(%s);\n", name
, tr_name
);
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
)
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);
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
;
2338 error("Restriction on %s does not allow usage of %s",
2339 definition_name
, get_templatetype_str());
2342 // others will be converted to specific value
2345 return needs_runtime_check
;
2348 bool Template::chk_restriction_refd(const char* definition_name
,
2349 template_restriction_t template_restriction
)
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();
2361 bool nrc
= t_last
->chk_restriction(definition_name
,
2362 template_restriction
);
2363 needs_runtime_check
= needs_runtime_check
|| nrc
;
2366 case Common::Assignment::A_MODULEPAR_TEMP
:
2367 is_var_template
= true;
2368 refd_tr
= TR_NONE
; // can't place restriction on this thing
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();
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;
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();
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();
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());
2410 return needs_runtime_check
;
2413 bool Template::chk_restriction(const char* definition_name
,
2414 template_restriction_t template_restriction
)
2416 bool needs_runtime_check
= false;
2417 switch (template_restriction
) {
2422 if (length_restriction
)
2423 error("Restriction on %s does not allow usage of length restriction",
2426 error("Restriction on %s does not allow usage of `ifpresent'",
2428 switch(templatetype
) {
2429 case TEMPLATE_ERROR
:
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
;
2437 else needs_runtime_check
= true;
2439 case SPECIFIC_VALUE
:
2440 case TEMPLATE_INVOKE
:
2442 case TEMPLATE_REFD
: {
2443 bool nrc
= chk_restriction_refd(definition_name
, template_restriction
);
2444 needs_runtime_check
= needs_runtime_check
|| nrc
;
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
;
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()) {
2462 case Type::T_SIGNATURE
:
2463 needed_checked_cnt
= my_governor
->get_nof_comps();
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
)
2475 u
.named_templates
->get_nt_byIndex(i
)->get_name().get_name(), 0);
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();
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
;
2490 needs_runtime_check
= true; // only basic check, needs runtime check
2493 if (template_restriction
==TR_OMIT
) break;
2494 // Else restriction is TR_VALUE, but template type is OMIT:
2495 // fall through to error.
2497 error("Restriction on %s does not allow usage of %s",
2498 definition_name
, get_templatetype_str());
2504 error("Restriction on %s does not allow usage of `ifpresent'",
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
;
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
;
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;
2530 if (has_any_or_omit
) break;
2532 // FIXME really no break?
2535 error("Restriction on %s does not allow usage of %s",
2536 definition_name
, get_templatetype_str());
2539 break; // all others are ok
2543 FATAL_ERROR("Template::chk_restriction()");
2545 return needs_runtime_check
;
2548 void Template::generate_code_expr(expression_struct
*expr
,
2549 template_restriction_t template_restriction
)
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());
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
, ')');
2570 // A simple unfoldable referenced template.
2571 if (!get_needs_conversion()) {
2572 u
.ref
.ref
->generate_code(expr
);
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(),
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
,
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
);
2604 case TEMPLATE_INVOKE
:
2605 generate_code_expr_invoke(expr
);
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();
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
);
2627 char *Template::generate_code_init(char *str
, const char *name
)
2629 if (get_code_generated()) return str
;
2630 set_code_generated();
2632 str
= err_descr
->generate_code_init_str(str
, string(name
)+"_err_descr");
2634 switch (templatetype
) {
2643 str
= mputprintf(str
, "%s = %s;\n", name
, get_single_expr(false).c_str());
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
);
2651 str
= generate_code_init_refd(str
, name
);
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
);
2659 case INDEXED_TEMPLATE_LIST
:
2660 str
= generate_code_init_seof(str
, name
);
2662 case NAMED_TEMPLATE_LIST
:
2663 str
= generate_code_init_se(str
, name
);
2665 case VALUE_LIST_ALL_FROM
:
2666 str
= generate_code_init_all_from_list(str
, name
);
2669 str
= generate_code_init_all_from(str
, name
);
2672 str
= generate_code_init_list(str
, name
, false);
2674 case COMPLEMENTED_LIST
:
2675 str
= generate_code_init_list(str
, name
, true);
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
);
2682 case SUPERSET_MATCH
:
2683 str
= generate_code_init_set(str
, name
, true);
2686 str
= generate_code_init_set(str
, name
, false);
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
);
2692 case TEMPLATE_ERROR
:
2693 case TEMPLATE_NOTUSED
:
2695 FATAL_ERROR("Template::generate_code_init()");
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
);
2702 if (is_ifpresent
) str
= mputprintf(str
, "%s.set_ifpresent();\n", name
);
2704 str
= mputprintf(str
, "%s.set_err_descr(&%s_err_descr);\n", name
, name
);
2709 char *Template::rearrange_init_code(char *str
)
2711 switch (templatetype
) {
2712 case SPECIFIC_VALUE
:
2713 str
= u
.specific_value
->rearrange_init_code(str
);
2716 str
= rearrange_init_code_refd(str
);
2718 case TEMPLATE_INVOKE
:
2719 str
= rearrange_init_code_invoke(str
);
2723 case COMPLEMENTED_LIST
:
2724 case SUPERSET_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
);
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
);
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
);
2741 str
= u
.value_range
->rearrange_init_code(str
);
2746 if (length_restriction
) str
= length_restriction
->rearrange_init_code(str
);
2750 bool Template::use_single_expr_for_init()
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())
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()) {
2766 // do not calculate expressions again
2768 case Value::V_REFD
: {
2769 // v_last is an unfoldable value reference
2770 // the scope of the definition that v_last refers to
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;
2779 // otherwise return false
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();
2788 char *Template::generate_code_init_refd(char *str
, const char *name
)
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());
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
);
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;
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
,
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
);
2841 str
= mputprintf(str
, "%s = %s;\n", name
, expr
.expr
);
2843 str
= mputstr(str
, expr
.postamble
);
2844 str
= mputstr(str
, "}\n");
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
,
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
);
2865 str
= mputprintf(str
, "%s = %s;\n", name
, expr
.expr
);
2868 Code::free_expr(&expr
);
2873 char *Template::generate_code_init_invoke(char *str
, const char *name
)
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
);
2882 char *Template::generate_rearrange_init_code_refd(char *str
,
2883 expression_struct
*expr
)
2885 /** Initially we can assume that:
2886 * - this is a referenced template and a part of a non-parameterized
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
;
2894 // first try to find the smallest dependent template
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();
2909 for (size_t i
= subrefs
->get_nof_refs(); i
> 0; i
--)
2910 refstack
.push(subrefs
->get_ref(i
- 1));
2912 // jump to the referred top-level template
2913 t
= ass
->get_Template();
2914 // start the iteration from the beginning
2916 // stop otherwise: the reference cannot be followed
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())
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
);
2940 // the topmost sub-reference was processed
2941 // it can be erased from the stack
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());
2956 expr
->expr
= mputc(expr
->expr
, '[');
2957 subref
->get_val()->generate_code_expr(expr
);
2958 expr
->expr
= mputc(expr
->expr
, ']');
2964 bool Template::compile_time() const
2966 switch (templatetype
) {
2968 case VALUE_LIST_ALL_FROM
:
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
2987 // "Complex" templates need to look at all elements
2990 case COMPLEMENTED_LIST
:
2991 case SUPERSET_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;
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())
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())
3010 return true; // not reached
3013 char *Template::generate_code_init_seof(char *str
, const char *name
)
3015 switch (templatetype
) {
3016 case TEMPLATE_LIST
: {
3017 size_t nof_ts
= u
.templates
->get_nof_ts();
3019 str
= mputprintf(str
, "%s = NULL_VALUE;\n", name
);
3024 Type
*t_last
= my_governor
->get_type_refd_last();
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();
3030 // indexing always starts from zero
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();
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");
3042 if (compile_time()) goto compile_time
;
3043 // run-time, variable sized init
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
) {
3071 char* str_preamble
= 0;
3072 char* str_set_size
= mputprintf(0, "%s.set_size(%lu", name
,
3073 (unsigned long)fixed_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
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();
3090 str_set_size
= mputstrn(str_set_size
, " + ", 3);
3092 Ref_pard
* ref_pard
= dynamic_cast<Ref_pard
*>(ref
);
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
);
3100 ref_pard
->generate_code_cached(&expr
);
3101 str_set_size
= mputprintf(str_set_size
, "%s", expr
.expr
);
3103 str_preamble
= mputstr(str_preamble
, expr
.preamble
);
3105 Code::free_expr(&expr
);
3108 str_set_size
= mputstr (str_set_size
, ass
->get_id().get_name().c_str());
3110 expression_struct expr
;
3111 Code::init_expr(&expr
);
3113 subrefs
->generate_code(&expr
, ass
);
3114 str_set_size
= mputprintf(str_set_size
, "%s", expr
.expr
);
3116 Code::free_expr(&expr
);
3119 str_set_size
= mputstr(str_set_size
, ".n_elem()");
3124 str
= mputstr(str
, str_preamble
);
3125 str
= mputstr(str
, str_set_size
);
3130 str
= mputstrn(str
, ");\n", 3); // finally done set_size
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
) {
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();
3154 Ref_pard
* ref_pard
= dynamic_cast<Ref_pard
*>(ref
);
3156 ref_pard
->generate_code_cached(&expr
);
3158 ref
->generate_code(&expr
);
3162 FATAL_ERROR("vtype %d", spec
->get_valuetype());
3169 FATAL_ERROR("ttype %d", permut_elem
->u
.all_from
->templatetype
);
3172 str
= mputprintf(str
,
3173 "for (int i_i = 0, i_lim = %s.n_elem(); i_i < i_lim; ++i_i) {\n",
3176 str
= permut_elem
->generate_code_init_seof_element(str
, name
,
3177 (Int2string(ix
) + skipper
+ " + i_i").c_str(),
3180 str
= mputstrn(str
, "}\n", 2);
3182 skipper
+= expr
.expr
;
3183 skipper
+= ".n_elem() /* 3005 */ ";
3184 Code::free_expr(&expr
);
3187 str
= permut_elem
->generate_code_init_seof_element(str
, name
,
3188 (Int2string(ix
) + skipper
).c_str(), oftype_name_str
);
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());
3196 t
->set_code_generated();
3197 index
+= nof_perm_ts
;
3201 str
= t
->generate_code_init_seof_element(str
, name
,
3202 (Int2string(index_offset
+ index
) + skipper
).c_str(), oftype_name_str
);
3204 case TEMPLATE_NOTUSED
:
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
) {
3225 char* str_preamble
= 0;
3226 char* str_set_size
= mputprintf(0, "%s.set_size(%lu", name
,
3227 (unsigned long)fixed_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
);
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
);
3250 ref_pard
->generate_code_cached(&expr
);
3251 str_set_size
= mputprintf(str_set_size
, "%s", expr
.expr
);
3253 str_preamble
= mputstr(str_preamble
, expr
.preamble
);
3254 Code::free_expr(&expr
);
3257 str_set_size
= mputstr (str_set_size
, ass
->get_id().get_name().c_str());
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
);
3266 str_set_size
= mputstr(str_set_size
, ".n_elem()");
3270 str
= mputstr(str
, str_preamble
);
3271 str
= mputstr(str
, str_set_size
);
3274 str
= mputstrn(str
, ");\n", 3); // finally done set_size
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
) {
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
);
3293 ref_pard
->generate_code_cached(&expr
);
3295 ref
->generate_code(&expr
);
3298 FATAL_ERROR("vtype %d", spec
->get_valuetype());
3303 FATAL_ERROR("ttype %d", t
->u
.all_from
->templatetype
);
3306 str
= mputprintf(str
,
3307 "for (int i_i = 0, i_lim = %s.n_elem(); i_i < i_lim; ++i_i) {\n",
3309 str
= t
->generate_code_init_seof_element(str
, name
,
3310 (Int2string(ix
) + skipper
+ " + i_i").c_str(),
3312 str
= mputstrn(str
, "}\n", 2);
3314 skipper
+= expr
.expr
;
3315 skipper
+= ".n_elem() ";
3316 Code::free_expr(&expr
);
3317 t
->set_code_generated();
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
);
3326 case TEMPLATE_NOTUSED
:
3336 // setting the size first
3338 str
= mputprintf(str
, "%s.set_size(%lu);\n", name
, (unsigned long) get_nof_listitems());
3339 // determining the index offset based on the governor
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
);
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
;
3361 str
= t
->generate_code_init_seof_element(str
, name
,
3362 Int2string(index_offset
+ index
).c_str(), oftype_name_str
);
3365 case TEMPLATE_NOTUSED
:
3370 case INDEXED_TEMPLATE_LIST
: {
3371 size_t nof_its
= u
.indexed_templates
->get_nof_its();
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
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());
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());
3396 str
= it
->get_template()->generate_code_init(str
, tmp_id_1
.c_str());
3397 str
= mputstr(str
, "}\n");
3400 // It seems to be impossible in this case.
3401 str
= mputprintf(str
, "%s = NULL_VALUE;\n", name
);
3405 FATAL_ERROR("Template::generate_code_init_seof()");
3411 char *Template::generate_code_init_seof_element(char *str
, const char *name
,
3412 const char* index
, const char *element_type_genname
)
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");
3422 char *embedded_name
= mprintf("%s[%s]", name
, index
);
3423 str
= generate_code_init(str
, embedded_name
);
3424 Free(embedded_name
);
3429 char *Template::generate_code_init_all_from(char *str
, const char *name
)
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
);
3442 ref_pard
->generate_code_cached(&expr
);
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
);
3459 char *Template::generate_code_init_all_from_list(char *str
, const char *name
)
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
);
3472 ref_pard
->generate_code_cached(&expr
);
3474 ref
->generate_code(&expr
);
3477 FATAL_ERROR("vtype %d", spec
->get_valuetype());
3483 FATAL_ERROR("ttype %d", u
.all_from
->templatetype
);
3488 str
= mputstr(str
, expr
.preamble
);
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",
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);
3501 Code::free_expr(&expr
);
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;
3515 if (type
->get_typetype()==Type::T_ANYTYPE
) {
3516 at
+= fieldname
.get_name();
3517 fieldname_str
= at
.c_str();
3520 fieldname_str
= fieldname
.get_name().c_str();
3522 Template
*t
= nt
->get_template();
3523 if (t
->needs_temp_ref()) {
3525 if (type
->get_typetype() == Type::T_SIGNATURE
) {
3526 field_type
= type
->get_signature_parameters()
3527 ->get_param_byName(fieldname
)->get_type();
3529 field_type
= type
->get_comp_byName(fieldname
)->get_type();
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");
3540 char *embedded_name
= mprintf("%s.%s()", name
, fieldname_str
);
3541 str
= t
->generate_code_init(str
, embedded_name
);
3542 Free(embedded_name
);
3546 str
= mputprintf(str
, "%s = NULL_VALUE;\n", name
);
3551 char *Template::generate_code_init_list(char *str
, const char *name
,
3552 bool is_complemented
) // VALUE_LIST or COMPLEMENTED_LIST
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();
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
) {
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!");
3586 str_set_type
= mputstrn(str_set_type
, " + ", 3);
3588 Ref_pard
* ref_pard
= dynamic_cast<Ref_pard
*>(ref
);
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
);
3596 ref_pard
->generate_code_cached(&expr
);
3597 str_set_type
= mputprintf(str_set_type
, "%s", expr
.expr
);
3599 str_preamble
= mputstr(str_preamble
, expr
.preamble
);
3601 Code::free_expr(&expr
);
3604 str_set_type
= mputstr (str_set_type
, ass
->get_id().get_name().c_str());
3606 expression_struct expr
;
3607 Code::init_expr(&expr
);
3609 subrefs
->generate_code(&expr
, ass
);
3610 str_set_type
= mputprintf(str
, "%s", expr
.expr
);
3612 Code::free_expr(&expr
);
3615 str_set_type
= mputstr(str_set_type
, ".n_elem()");
3618 str
= mputstr(str
, str_preamble
);
3619 str
= mputstr(str
, str_set_type
);
3624 str
= mputstrn(str
, ");\n", 3);
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");
3638 expression_struct expr
;
3639 Code::init_expr(&expr
);
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
);
3649 ref_pard
->generate_code_cached(&expr
);
3651 ref
->generate_code(&expr
);
3655 FATAL_ERROR("VT NOT HANDLED");
3656 } // switch valuetype
3660 FATAL_ERROR("ttype not handled");
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",
3669 if (t
->needs_temp_ref()) {
3670 // Not possible, because t->templatetype is ALL_FROM
3671 FATAL_ERROR("temp ref not handled");
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
);
3678 str
= mputstrn(str
, "}\n", 2);
3681 shifty
+= expr
.expr
;
3682 shifty
+= ".n_elem() /* 3303 */ ";
3684 Code::free_expr(&expr
);
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");
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
);
3703 } // switch t->templatetype
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");
3721 char *embedded_name
= mprintf("%s.list_item(%lu)", name
,
3723 str
= t
->generate_code_init(str
, embedded_name
);
3724 Free(embedded_name
);
3731 char *Template::generate_code_init_set(char *str
, const char *name
,
3732 bool is_superset
) // SUPERSET_MATCH and SUBSET_MATCH
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();
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
) {
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
);
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!");
3766 str_set_type
= mputstrn(str_set_type
, " + ", 3);
3768 Ref_pard
* ref_pard
= dynamic_cast<Ref_pard
*>(ref
);
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
);
3776 ref_pard
->generate_code_cached(&expr
);
3777 str_set_type
= mputprintf(str_set_type
, "%s", expr
.expr
);
3779 str_preamble
= mputstr(str_preamble
, expr
.preamble
);
3781 Code::free_expr(&expr
);
3784 str_set_type
= mputstr (str_set_type
, ass
->get_id().get_name().c_str());
3786 expression_struct expr
;
3787 Code::init_expr(&expr
);
3789 subrefs
->generate_code(&expr
, ass
);
3790 str_set_type
= mputprintf(str_set_type
, "%s", expr
.expr
);
3792 Code::free_expr(&expr
);
3795 str_set_type
= mputstr(str_set_type
, ".n_elem()");
3798 str
= mputstr(str
, str_preamble
);
3799 str
= mputstr(str
, str_set_type
);
3804 str
= mputstrn(str
, ");\n", 3);
3807 for (size_t i
= 0; i
< nof_ts
; i
++) {
3808 Template
*t
= u
.templates
->get_t_byIndex(i
);
3809 switch (t
->templatetype
) {
3811 expression_struct expr
; // FIXME copypasta from init_list above !
3812 Code::init_expr(&expr
);
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
);
3822 ref_pard
->generate_code_cached(&expr
);
3824 ref
->generate_code(&expr
);
3828 FATAL_ERROR("VT NOT HANDLED");
3829 } // switch valuetype
3833 FATAL_ERROR("ttype not handled");
3837 str
= mputprintf(str
,
3838 "for (int i_i = 0, i_lim = %s.n_elem(); i_i < i_lim; ++i_i) {\n",
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
);
3847 str
= mputstrn(str
, "}\n", 2);
3850 shifty
+= expr
.expr
;
3851 shifty
+= ".n_elem() /* 3442 */";
3852 Code::free_expr(&expr
);
3854 case VALUE_LIST_ALL_FROM
:
3855 FATAL_ERROR("Not possible");
3856 break; // not reached
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");
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
);
3874 } // switch t->templatetype
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");
3892 char *embedded_name
= mprintf("%s.set_item(%lu)", name
,
3894 str
= t
->generate_code_init(str
, embedded_name
);
3895 Free(embedded_name
);
3902 void Template::generate_code_expr_invoke(expression_struct
*expr
)
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);
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());
3918 expr
->expr
= mputc(expr
->expr
, ')');
3921 char *Template::rearrange_init_code_refd(char *str
)
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();
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
);
3945 // the reference points to a non-parameterized template
3946 FieldOrArrayRefs
*subrefs
= u
.ref
.ref
->get_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())
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
);
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());
3980 char *Template::rearrange_init_code_invoke(char *str
)
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
);
3989 bool Template::needs_temp_ref()
3991 if (length_restriction
|| is_ifpresent
) return true;
3992 switch (templatetype
) {
3996 case SPECIFIC_VALUE
:
3998 case TEMPLATE_INVOKE
:
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
)
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
)
4020 case NAMED_TEMPLATE_LIST
:
4021 return u
.named_templates
->get_nof_nts() > 1;
4023 case VALUE_LIST_ALL_FROM
:
4026 case COMPLEMENTED_LIST
:
4028 case SUPERSET_MATCH
:
4031 case TEMPLATE_ERROR
:
4032 case TEMPLATE_NOTUSED
:
4033 FATAL_ERROR("Template::needs_temp_ref()");
4034 case PERMUTATION_MATCH
:
4041 bool Template::has_single_expr()
4043 if (length_restriction
|| is_ifpresent
|| get_needs_conversion())
4045 switch (templatetype
) {
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;
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;
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;
4082 case COMPLEMENTED_LIST
:
4084 case SUPERSET_MATCH
:
4086 case PERMUTATION_MATCH
:
4089 case VALUE_LIST_ALL_FROM
:
4092 FATAL_ERROR("Template::has_single_expr()");
4097 string
Template::get_single_expr(bool cast_needed
)
4099 if (cast_needed
&& (length_restriction
|| is_ifpresent
))
4100 FATAL_ERROR("Template::get_single_expr()");
4102 switch (templatetype
) {
4104 ret_val
= "OMIT_VALUE";
4107 ret_val
= "ANY_VALUE";
4110 ret_val
= "ANY_OR_OMIT";
4112 case SPECIFIC_VALUE
:
4113 ret_val
= u
.specific_value
->get_single_expr();
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
);
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
);
4136 if (u
.templates
->get_nof_ts() != 0)
4137 FATAL_ERROR("Template::get_single_expr()");
4138 ret_val
= "NULL_VALUE";
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";
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";
4151 return get_my_scope()->get_scope_mod_gen()
4152 ->add_bitstring_pattern(*u
.pattern
);
4154 return get_my_scope()->get_scope_mod_gen()
4155 ->add_hexstring_pattern(*u
.pattern
);
4157 return get_my_scope()->get_scope_mod_gen()
4158 ->add_octetstring_pattern(*u
.pattern
);
4162 ->create_charstring_literals(get_my_scope()->get_scope_mod_gen());
4164 FATAL_ERROR("Template::get_single_expr()");
4166 if (cast_needed
) ret_val
= my_governor
->get_genname_template(my_scope
) +
4167 "(" + ret_val
+ ")";
4171 void Template::dump(unsigned level
) const
4173 DEBUG(level
, "%s", get_templatetype_str());
4174 switch (templatetype
) {
4175 case TEMPLATE_ERROR
:
4180 case SPECIFIC_VALUE
:
4181 u
.specific_value
->dump(level
+1);
4184 u
.ref
.ref
->dump(level
+1);
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);
4193 case COMPLEMENTED_LIST
:
4194 case SUPERSET_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);
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);
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);
4209 u
.value_range
->dump(level
);
4214 DEBUG(level
+1, "%s", u
.pattern
->c_str());
4218 u
.pstring
->dump(level
+1);
4221 case VALUE_LIST_ALL_FROM
:
4222 u
.all_from
->dump(level
+1);
4227 if (length_restriction
) length_restriction
->dump(level
+ 1);
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
) {
4242 // =================================
4243 // ===== TemplateInstance
4244 // =================================
4246 TemplateInstance::TemplateInstance(const TemplateInstance
& p
)
4247 : Node(p
), Location(p
)
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();
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
)
4258 if (!p_body
) FATAL_ERROR("TemplateInstance::TemplateInstance()");
4259 if (type
) type
->set_ownertype(Type::OT_TEMPLATE_INST
, this);
4262 TemplateInstance::~TemplateInstance()
4265 delete derived_reference
;
4266 delete template_body
;
4269 void TemplateInstance::release()
4272 derived_reference
= 0;
4276 TemplateInstance
*TemplateInstance::clone() const
4278 return new TemplateInstance(*this);
4281 void TemplateInstance::set_fullname(const string
& p_fullname
)
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
);
4290 void TemplateInstance::set_my_scope(Scope
*p_scope
)
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
);
4297 Type::typetype_t
TemplateInstance::get_expr_returntype
4298 (Type::expected_value_t exp_val
)
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
);
4305 Type
*TemplateInstance::get_expr_governor(Type::expected_value_t exp_val
)
4307 if (type
) return type
;
4308 if (derived_reference
) {
4309 Type
*ret_val
= chk_DerivedRef(0);
4310 if (ret_val
) return ret_val
;
4312 return template_body
->get_expr_governor(exp_val
);
4315 void TemplateInstance::chk(Type
*governor
)
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);
4327 Type
*TemplateInstance::chk_Type(Type
*governor
)
4329 if (!type
) return governor
;
4331 Error_Context
cntxt(type
, "In explicit type specification");
4334 TypeCompatInfo
info(template_body
->get_template_refd_last()
4335 ->get_my_scope()->get_scope_mod(), governor
, type
,
4339 if (!governor
) return type
;
4340 else if (governor
->is_compatible(type
, &info
, &l_chain
, &r_chain
)) {
4343 if (info
.is_subtype_error()) {
4344 type
->error("%s", info
.get_subtype_error().c_str());
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());
4352 type
->error("%s", info
.get_error_str_str().c_str());
4358 Type
*TemplateInstance::chk_DerivedRef(Type
*governor
)
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
:
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();
4382 TypeCompatInfo
info(template_body
->get_template_refd_last()
4383 ->get_my_scope()->get_scope_mod(), governor
, type
,
4387 if (!governor
->is_compatible(base_template_type
, &info
, &l_chain
,
4389 if (info
.is_subtype_error()) {
4390 derived_reference
->error("%s", info
.get_subtype_error().c_str());
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());
4399 derived_reference
->error("%s", info
.get_error_str_str().c_str());
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
;
4406 if (info
.needs_conversion())
4407 template_body
->set_needs_conversion();
4409 } else governor
= base_template_type
;
4412 derived_reference
->error("Reference to a template was expected instead "
4413 "of %s", ass
->get_description().c_str());
4416 if (set_bt
) template_body
->set_base_template(ass
->get_Template());
4419 // drop the erroneous derived_reference to avoid further errors
4420 delete derived_reference
;
4421 derived_reference
= 0;
4425 void TemplateInstance::chk_recursions(ReferenceChain
& refch
)
4427 template_body
->chk_recursions(refch
);
4430 bool TemplateInstance::is_string_type(Type::expected_value_t exp_val
)
4432 switch (get_expr_returntype(exp_val
)) {
4444 bool TemplateInstance::chk_restriction(const char* definition_name
,
4445 template_restriction_t template_restriction
)
4447 bool needs_runtime_check
= false;
4448 if (derived_reference
) // if modified
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
);
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);
4478 needs_runtime_check
= template_body
->chk_restriction(definition_name
,
4479 template_restriction
);
4481 return needs_runtime_check
;
4484 bool TemplateInstance::has_single_expr()
4486 if (derived_reference
) return false;
4487 else if (type
) return false;
4488 else return template_body
->has_single_expr();
4491 void TemplateInstance::set_code_section(
4492 GovernedSimple::code_section_t p_code_section
)
4494 if (derived_reference
) derived_reference
->set_code_section(p_code_section
);
4495 template_body
->set_code_section(p_code_section
);
4498 bool TemplateInstance::needs_temp_ref()
4500 if (template_body
->get_templatetype() != Template::SPECIFIC_VALUE
)
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"
4510 if (refs
->get_nof_refs() < 2
4511 || refs
->get_ref(0)->get_type() != FieldOrArrayRef::ARRAY_REF
)
4519 void TemplateInstance::generate_code(expression_struct
*expr
,
4520 template_restriction_t template_restriction
)
4522 if (derived_reference
) {
4523 // preserve the target expression
4524 char *expr_backup
= expr
->expr
;
4525 // reset the space for the target expression
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
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
,
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
4545 // restore the target expression append the name of the temporary
4547 expr
->expr
= mputstr(expr_backup
, tmp_id_str
);
4548 } else template_body
->generate_code_expr(expr
, template_restriction
);
4551 char *TemplateInstance::rearrange_init_code(char *str
)
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
4563 Template
*t
= ass
->get_Template();
4565 // the referred template is parameterized
4566 // the embedded referenced templates shall be visited
4567 str
= t
->rearrange_init_code(str
);
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());
4575 str
= template_body
->rearrange_init_code(str
);
4579 void TemplateInstance::append_stringRepr(string
& str
) const
4582 str
+= type
->get_typename();
4585 if (derived_reference
) {
4587 str
+= derived_reference
->get_dispname();
4590 str
+= template_body
->get_stringRepr();
4593 void TemplateInstance::dump(unsigned level
) const
4596 DEBUG(level
, "type:");
4597 type
->dump(level
+ 1);
4599 if (derived_reference
) {
4600 DEBUG(level
, "modifies:");
4601 derived_reference
->dump(level
+ 1);
4603 template_body
->dump(level
);
4606 Value
* TemplateInstance::get_specific_value() const
4608 if (type
) return NULL
;
4609 if (derived_reference
) return NULL
;
4610 if (template_body
->is_length_restricted() || template_body
->get_ifpresent())
4612 if (template_body
->get_templatetype()!=Template::SPECIFIC_VALUE
) return NULL
;
4613 return template_body
->get_specific_value();
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
;