1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
18 * Zalanyi, Balazs Andor
21 ******************************************************************************/
22 #include "../../common/dbgnew.hh"
23 #include "AST_ttcn3.hh"
24 #include "../Identifier.hh"
25 #include "../CompilerError.hh"
26 #include "../Setting.hh"
28 #include "../CompField.hh"
29 #include "../CompType.hh"
30 #include "../TypeCompat.hh"
31 #include "../Valuestuff.hh"
32 #include "../Value.hh"
33 #include "Ttcnstuff.hh"
34 #include "TtcnTemplate.hh"
35 #include "Templatestuff.hh"
36 #include "ArrayDimensions.hh"
39 #include "Statement.hh"
41 #include "Attributes.hh"
42 #include "PatternString.hh"
43 #include "../../common/version_internal.h"
44 #include "../CodeGenHelper.hh"
45 #include "../../common/JSON_Tokenizer.hh"
46 #include "../DebuggerStuff.hh"
49 // implemented in coding_attrib_p.y
50 extern Ttcn::ExtensionAttributes
* parse_extattributes(
51 Ttcn::WithAttribPath
*w_attrib_path
);
53 // implemented in compiler.y
54 extern Ttcn::ErroneousAttributeSpec
* ttcn3_parse_erroneous_attr_spec_string(
55 const char* p_str
, const Common::Location
& str_loc
);
58 extern void init_coding_attrib_lex(const Ttcn::AttributeSpec
& attrib
);
59 extern int coding_attrib_parse();
60 extern void cleanup_coding_attrib_lex();
61 extern Ttcn::ExtensionAttributes
*extatrs
;
63 /** Create a field name in the anytype
65 * The output of this function will be used to create an identifier
66 * to be used as the field name in the anytype.
67 * The type_name may be a built-in type (e.g. "integer") or a user-defined
70 * If the name has multiple components (a fullname?), it keeps just the last
71 * component without any dots. *
72 * Also, the space in "universal charstring" needs to be replaced
73 * with an underscore to make it an identifier.
75 * Note: Prefixing with "AT_" is not done here, but in defUnionClass().
77 * @param type_name string
78 * @return string to be used as the identifier.
80 string
anytype_field(const string
& type_name
)
82 string
retval(type_name
);
84 // keep just the last part of the name
85 // TODO check if there's a way to get just the last component (note that fetching the string is done outside of this function)
86 size_t dot
= retval
.rfind('.');
87 if (dot
>= retval
.size()) dot
= 0;
89 retval
.replace(0, dot
, "");
94 extern Common::Modules
*modules
; // in main.cc
97 static const string
_T_("_T_");
102 using namespace Common
;
104 // =================================
105 // ===== FieldOrArrayRef
106 // =================================
108 FieldOrArrayRef::FieldOrArrayRef(const FieldOrArrayRef
& p
)
109 : Node(p
), Location(p
), ref_type(p
.ref_type
)
111 switch (p
.ref_type
) {
113 u
.id
= p
.u
.id
->clone();
116 u
.arp
= p
.u
.arp
->clone();
119 FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
123 FieldOrArrayRef::FieldOrArrayRef(Identifier
*p_id
)
124 : Node(), Location(), ref_type(FIELD_REF
)
126 if (!p_id
) FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
130 FieldOrArrayRef::FieldOrArrayRef(Value
*p_arp
)
131 : Node(), Location(), ref_type(ARRAY_REF
)
133 if (!p_arp
) FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
137 FieldOrArrayRef::~FieldOrArrayRef()
147 FATAL_ERROR("FieldOrArrayRef::~FieldOrArrayRef()");
151 FieldOrArrayRef
*FieldOrArrayRef::clone() const
153 return new FieldOrArrayRef(*this);
156 void FieldOrArrayRef::set_fullname(const string
& p_fullname
)
158 Node::set_fullname(p_fullname
);
159 if (ref_type
== ARRAY_REF
)
160 u
.arp
->set_fullname(p_fullname
+ ".<array_index>");
163 void FieldOrArrayRef::set_my_scope(Scope
*p_scope
)
165 if (ref_type
== ARRAY_REF
) u
.arp
->set_my_scope(p_scope
);
168 const Identifier
* FieldOrArrayRef::get_id() const
170 if (ref_type
!= FIELD_REF
) FATAL_ERROR("FieldOrArrayRef::get_id()");
174 Value
*FieldOrArrayRef::get_val() const
176 if (ref_type
!= ARRAY_REF
) FATAL_ERROR("FieldOrArrayRef::get_val()");
180 void FieldOrArrayRef::append_stringRepr(string
& str
) const
185 str
+= u
.id
->get_dispname();
189 str
+= u
.arp
->get_stringRepr();
193 str
+= "<unknown sub-reference>";
197 void FieldOrArrayRef::set_field_name_to_lowercase()
199 if (ref_type
!= FIELD_REF
) FATAL_ERROR("FieldOrArrayRef::set_field_name_to_lowercase()");
200 string new_name
= u
.id
->get_name();
201 if (isupper(new_name
[0])) {
202 new_name
[0] = tolower(new_name
[0]);
203 if (new_name
[new_name
.size() - 1] == '_') {
204 // an underscore is inserted at the end of the field name if it's
205 // a basic type's name (since it would conflict with the class generated
207 // remove the underscore, it won't conflict with anything if its name
208 // starts with a lowercase letter
209 new_name
.replace(new_name
.size() - 1, 1, "");
212 u
.id
= new Identifier(Identifier::ID_NAME
, new_name
);
216 // =================================
217 // ===== FieldOrArrayRefs
218 // =================================
220 FieldOrArrayRefs::FieldOrArrayRefs(const FieldOrArrayRefs
& p
)
221 : Node(p
), refs_str_element(false)
223 for (size_t i
= 0; i
< p
.refs
.size(); i
++) refs
.add(p
.refs
[i
]->clone());
226 FieldOrArrayRefs::~FieldOrArrayRefs()
228 for (size_t i
= 0; i
< refs
.size(); i
++) delete refs
[i
];
232 FieldOrArrayRefs
*FieldOrArrayRefs::clone() const
234 return new FieldOrArrayRefs(*this);
237 void FieldOrArrayRefs::set_fullname(const string
& p_fullname
)
239 Node::set_fullname(p_fullname
);
240 for (size_t i
= 0; i
< refs
.size(); i
++)
241 refs
[i
]->set_fullname(p_fullname
+
242 ".<sub_reference" + Int2string(i
+ 1) + ">");
245 void FieldOrArrayRefs::set_my_scope(Scope
*p_scope
)
247 for (size_t i
= 0; i
< refs
.size(); i
++) refs
[i
]->set_my_scope(p_scope
);
250 bool FieldOrArrayRefs::has_unfoldable_index() const
252 for (size_t i
= 0; i
< refs
.size(); i
++) {
253 FieldOrArrayRef
*ref
= refs
[i
];
254 if (ref
->get_type() == FieldOrArrayRef::ARRAY_REF
) {
255 Value
*v
= ref
->get_val();
256 v
->set_lowerid_to_ref();
257 if (v
->is_unfoldable()) return true;
263 void FieldOrArrayRefs::remove_refs(size_t n
)
265 for (size_t i
= 0; i
< n
; i
++) delete refs
[i
];
266 refs
.replace(0, n
, NULL
);
267 set_fullname(get_fullname());
270 /* remove_last_field is used when unfolding references for
271 ischosen and ispresent function operands.
272 In this case it is NOT sure the last field exists.
273 Calling remove_last_field previously
274 will avoid getting the "variable...Has no member called..." error message.
275 The last field component will be checked as a separate step.
276 Warning: the removed Identifier has to be deleted later */
278 Identifier
* FieldOrArrayRefs::remove_last_field()
280 if (refs
.size() == 0) return 0;
281 size_t last_elem_ind
= refs
.size() - 1;
282 FieldOrArrayRef
* last_elem
= refs
[last_elem_ind
];
283 if (last_elem
->get_type() == FieldOrArrayRef::FIELD_REF
) {
284 Identifier
*ret_val
= last_elem
->get_id()->clone();
286 refs
.replace(last_elem_ind
, 1, NULL
);
291 void FieldOrArrayRefs::generate_code(expression_struct
*expr
,
292 Common::Assignment
*ass
, size_t nof_subrefs
/* = UINT_MAX*/)
295 bool is_template
= false;
296 switch (ass
->get_asstype()) {
297 case Common::Assignment::A_CONST
: // a Def_Const
298 case Common::Assignment::A_EXT_CONST
: // a Def_ExtConst
299 case Common::Assignment::A_MODULEPAR
: // a Def_Modulepar
300 case Common::Assignment::A_VAR
: // a Def_Var
301 case Common::Assignment::A_FUNCTION_RVAL
: // a Def_Function
302 case Common::Assignment::A_EXT_FUNCTION_RVAL
: // a Def_ExtFunction
303 case Common::Assignment::A_PAR_VAL_IN
: // a FormalPar
304 case Common::Assignment::A_PAR_VAL_OUT
: // a FormalPar
305 case Common::Assignment::A_PAR_VAL_INOUT
: // a FormalPar
306 // The type is important since the referred entities are value objects.
307 type
= ass
->get_Type();
309 case Common::Assignment::A_MODULEPAR_TEMP
: // a Def_Modulepar_Template
310 case Common::Assignment::A_TEMPLATE
: // a Def_Template
311 case Common::Assignment::A_VAR_TEMPLATE
: // a Def_Var_Template
312 case Common::Assignment::A_PAR_TEMPL_IN
: // a FormalPar
313 case Common::Assignment::A_PAR_TEMPL_OUT
: // a FormalPar
314 case Common::Assignment::A_PAR_TEMPL_INOUT
: // a FormalPar
315 // The type is semi-important because fields of anytype templates
317 type
= ass
->get_Type();
320 case Common::Assignment::A_TIMER
: // a Def_Timer
321 case Common::Assignment::A_PORT
: // a Def_Port
322 case Common::Assignment::A_FUNCTION_RTEMP
: // a Def_Function
323 case Common::Assignment::A_EXT_FUNCTION_RTEMP
: // a Def_ExtFunction
324 case Common::Assignment::A_PAR_TIMER
: // a FormalPar
325 case Common::Assignment::A_PAR_PORT
: // a FormalPar
326 // The type is not relevant (i.e. the optional fields do not require
327 // special handling).
331 // Reference to other definitions cannot occur during code generation.
332 FATAL_ERROR("FieldOrArrayRefs::generate_code()");
335 size_t n_refs
= (nof_subrefs
!= UINT_MAX
) ? nof_subrefs
: refs
.size();
336 for (size_t i
= 0; i
< n_refs
; i
++) {
337 if (type
) type
= type
->get_type_refd_last();
338 // type changes inside the loop; need to recompute "last" every time.
339 FieldOrArrayRef
*ref
= refs
[i
];
340 if (ref
->get_type() == FieldOrArrayRef::FIELD_REF
) {
341 // Write a call to the field accessor method.
342 // Fields of the anytype get a special prefix; see also:
343 // Template::generate_code_init_se, TypeConv::gen_conv_func_choice_anytype,
344 // defUnionClass and defUnionTemplate.
345 const Identifier
& id
= *ref
->get_id();
346 expr
->expr
= mputprintf(expr
->expr
, ".%s%s()",
347 ((type
!=0 && type
->get_typetype()==Type::T_ANYTYPE
) ? "AT_" : ""),
348 id
.get_name().c_str());
350 CompField
*cf
= type
->get_comp_byName(id
);
351 // If the field is optional, the return type of the accessor is an
352 // OPTIONAL<T>. Write a call to OPTIONAL<T>::operator(),
353 // which "reaches into" the OPTIONAL to get the contained type T.
354 // Don't do this at the end of the reference chain.
355 // Accessor methods for a foo_template return a bar_template
356 // and OPTIONAL<> is not involved, hence no "()".
357 if (!is_template
&& i
< n_refs
- 1 && cf
->get_is_optional())
358 expr
->expr
= mputstr(expr
->expr
, "()");
359 // Follow the field type.
360 type
= cf
->get_type();
363 // Generate code for array reference.
364 expr
->expr
= mputc(expr
->expr
, '[');
365 ref
->get_val()->generate_code_expr(expr
);
366 expr
->expr
= mputc(expr
->expr
, ']');
368 // Follow the embedded type.
369 switch (type
->get_typetype()) {
373 type
= type
->get_ofType();
376 // The index points to a string element.
377 // There are no further sub-references.
381 } // if (ref->get_type)
385 void FieldOrArrayRefs::append_stringRepr(string
& str
) const
387 for (size_t i
= 0; i
< refs
.size(); i
++) refs
[i
]->append_stringRepr(str
);
390 // =================================
392 // =================================
394 Ref_base::Ref_base(const Ref_base
& p
)
395 : Ref_simple(p
), subrefs(p
.subrefs
)
397 modid
= p
.modid
? p
.modid
->clone() : 0;
398 id
= p
.id
? p
.id
->clone() : 0;
399 params_checked
= p
.is_erroneous
;
402 Ref_base::Ref_base(Identifier
*p_modid
, Identifier
*p_id
)
403 : Ref_simple(), modid(p_modid
), id(p_id
), params_checked(false)
404 , usedInIsbound(false)
407 FATAL_ERROR("NULL parameter: Ttcn::Ref_base::Ref_base()");
410 Ref_base::~Ref_base()
416 void Ref_base::set_fullname(const string
& p_fullname
)
418 Ref_simple::set_fullname(p_fullname
);
419 subrefs
.set_fullname(p_fullname
);
422 void Ref_base::set_my_scope(Scope
*p_scope
)
424 Ref_simple::set_my_scope(p_scope
);
425 subrefs
.set_my_scope(p_scope
);
428 /* returns the referenced variable's base type or value */
429 Setting
* Ref_base::get_refd_setting()
431 Common::Assignment
*ass
= get_refd_assignment();
432 if (ass
) return ass
->get_Setting();
436 FieldOrArrayRefs
*Ref_base::get_subrefs()
438 if (!id
) get_modid();
439 if (subrefs
.get_nof_refs() == 0) return 0;
440 else return &subrefs
;
443 bool Ref_base::has_single_expr()
445 Common::Assignment
*ass
= get_refd_assignment();
446 if (!ass
) FATAL_ERROR("Ref_base::has_single_expr()");
447 for (size_t i
= 0; i
< subrefs
.get_nof_refs(); i
++) {
448 FieldOrArrayRef
*ref
= subrefs
.get_ref(i
);
449 if (ref
->get_type() == FieldOrArrayRef::ARRAY_REF
&&
450 !ref
->get_val()->has_single_expr()) return false;
455 void Ref_base::set_code_section(
456 GovernedSimple::code_section_t p_code_section
)
458 for (size_t i
= 0; i
< subrefs
.get_nof_refs(); i
++) {
459 FieldOrArrayRef
*ref
= subrefs
.get_ref(i
);
460 if (ref
->get_type() == FieldOrArrayRef::ARRAY_REF
)
461 ref
->get_val()->set_code_section(p_code_section
);
465 void Ref_base::generate_code_const_ref(expression_struct_t */
*expr*/
)
467 FATAL_ERROR("Ref_base::generate_code_const_ref()");
470 // =================================
472 // =================================
474 Reference::Reference(Identifier
*p_id
)
475 : Ref_base(), parlist(0)
477 subrefs
.add(new FieldOrArrayRef(p_id
));
480 Reference::~Reference()
488 * Common::PortTypeBody::PortTypeBody
490 * Common::TypeMappingTarget::TypeMappingTarget
491 * Common::PatternString::ps_elem_t::chk_ref */
492 Reference
*Reference::clone() const
494 return new Reference(*this);
497 string
Reference::get_dispname()
502 ret_val
+= modid
->get_dispname();
505 ret_val
+= id
->get_dispname();
506 subrefs
.append_stringRepr(ret_val
);
508 subrefs
.append_stringRepr(ret_val
);
509 // cut the leading dot
510 if (!ret_val
.empty() && ret_val
[0] == '.')
511 ret_val
.replace(0, 1, "");
516 Common::Assignment
* Reference::get_refd_assignment(bool check_parlist
)
518 Common::Assignment
*ass
= Ref_base::get_refd_assignment(check_parlist
);
519 // In fact calls Ref_simple::get_refd_assignment
520 if (ass
&& check_parlist
&& !params_checked
) {
521 params_checked
= true;
522 FormalParList
*fplist
= ass
->get_FormalParList();
524 if (fplist
->has_only_default_values()
525 && Common::Assignment::A_TEMPLATE
== ass
->get_asstype()) {
526 Ttcn::ParsedActualParameters params
;
527 Error_Context
cntxt(¶ms
, "In actual parameter list of %s",
528 ass
->get_description().c_str());
529 parlist
= new ActualParList();
530 is_erroneous
= fplist
->fold_named_and_chk(¶ms
, parlist
);
531 parlist
->set_fullname(get_fullname());
532 parlist
->set_my_scope(my_scope
);
534 error("Reference to parameterized definition `%s' without "
535 "actual parameter list", ass
->get_id().get_dispname().c_str());
542 const Identifier
* Reference::get_modid()
544 if (!id
) detect_modid();
548 const Identifier
* Reference::get_id()
550 if (!id
) detect_modid();
554 Type
*Reference::chk_variable_ref()
556 Common::Assignment
*t_ass
= get_refd_assignment();
557 if (!t_ass
) return 0;
558 switch (t_ass
->get_asstype()) {
559 case Common::Assignment::A_PAR_VAL_IN
:
560 t_ass
->use_as_lvalue(*this);
562 case Common::Assignment::A_VAR
:
563 case Common::Assignment::A_PAR_VAL_OUT
:
564 case Common::Assignment::A_PAR_VAL_INOUT
:
567 error("Reference to a variable or value parameter was "
568 "expected instead of %s", t_ass
->get_description().c_str());
571 FieldOrArrayRefs
*t_subrefs
= get_subrefs();
572 Type
*ret_val
= t_ass
->get_Type()->get_field_type(t_subrefs
,
573 Type::EXPECTED_DYNAMIC_VALUE
);
574 if (ret_val
&& t_subrefs
&& t_subrefs
->refers_to_string_element()) {
575 error("Reference to a string element of type `%s' cannot be used in "
576 "this context", ret_val
->get_typename().c_str());
581 Type
*Reference::chk_comptype_ref()
583 Common::Assignment
*ass
= get_refd_assignment();
585 if (ass
->get_asstype() == Common::Assignment::A_TYPE
) {
586 Type
*t
= ass
->get_Type()->get_type_refd_last();
587 switch (t
->get_typetype()) {
591 case Type::T_COMPONENT
:
594 error("Reference `%s' does not refer to a component type",
595 get_dispname().c_str());
598 error("Reference `%s' does not refer to a type",
599 get_dispname().c_str());
605 bool Reference::has_single_expr()
607 if (!Ref_base::has_single_expr()) {
610 if (parlist
!= NULL
) {
611 for (size_t i
= 0; i
< parlist
->get_nof_pars(); i
++) {
612 if (!parlist
->get_par(i
)->has_single_expr()) {
620 void Reference::ref_usage_found()
622 Common::Assignment
*ass
= get_refd_assignment();
623 if (!ass
) FATAL_ERROR("Reference::ref_usage_found()");
624 switch (ass
->get_asstype()) {
625 case Common::Assignment::A_PAR_VAL_OUT
:
626 case Common::Assignment::A_PAR_TEMPL_OUT
:
627 case Common::Assignment::A_PAR_VAL
:
628 case Common::Assignment::A_PAR_VAL_IN
:
629 case Common::Assignment::A_PAR_VAL_INOUT
:
630 case Common::Assignment::A_PAR_TEMPL_IN
:
631 case Common::Assignment::A_PAR_TEMPL_INOUT
:
632 case Common::Assignment::A_PAR_PORT
:
633 case Common::Assignment::A_PAR_TIMER
: {
634 FormalPar
*fpar
= dynamic_cast<FormalPar
*>(ass
);
636 FATAL_ERROR("Reference::ref_usage_found()");
638 fpar
->set_usage_found();
640 case Common::Assignment::A_EXT_CONST
: {
641 Def_ExtConst
* def
= dynamic_cast<Def_ExtConst
*>(ass
);
643 FATAL_ERROR("Reference::ref_usage_found()");
645 def
->set_usage_found();
652 void Reference::generate_code(expression_struct_t
*expr
)
655 Common::Assignment
*ass
= get_refd_assignment();
656 if (!ass
) FATAL_ERROR("Reference::generate_code()");
658 // reference without parameters to a template that has only default formal parameters.
659 // if @lazy: nothing to do, it's a C++ function call just like in case of Ref_pard::generate_code()
660 expr
->expr
= mputprintf(expr
->expr
, "%s(",
661 ass
->get_genname_from_scope(my_scope
).c_str());
662 parlist
->generate_code_alias(expr
, ass
->get_FormalParList(),
663 ass
->get_RunsOnType(), false);
664 expr
->expr
= mputc(expr
->expr
, ')');
666 expr
->expr
= mputstr(expr
->expr
,
667 LazyParamData::in_lazy() ?
668 LazyParamData::add_ref_genname(ass
, my_scope
).c_str() :
669 ass
->get_genname_from_scope(my_scope
).c_str());
671 if (subrefs
.get_nof_refs() > 0) subrefs
.generate_code(expr
, ass
);
674 void Reference::generate_code_const_ref(expression_struct_t
*expr
)
676 FieldOrArrayRefs
*t_subrefs
= get_subrefs();
677 if (!t_subrefs
|| t_subrefs
->get_nof_refs() == 0) {
683 Common::Assignment
*ass
= get_refd_assignment();
684 if (!ass
) FATAL_ERROR("Reference::generate_code_const_ref()");
687 switch (ass
->get_asstype()) {
688 case Common::Assignment::A_MODULEPAR
:
689 case Common::Assignment::A_VAR
:
690 case Common::Assignment::A_FUNCTION_RVAL
:
691 case Common::Assignment::A_EXT_FUNCTION_RVAL
:
692 case Common::Assignment::A_PAR_VAL_IN
:
693 case Common::Assignment::A_PAR_VAL_OUT
:
694 case Common::Assignment::A_PAR_VAL_INOUT
: {
697 case Common::Assignment::A_MODULEPAR_TEMP
:
698 case Common::Assignment::A_TEMPLATE
:
699 case Common::Assignment::A_VAR_TEMPLATE
:
700 case Common::Assignment::A_PAR_TEMPL_IN
:
701 case Common::Assignment::A_PAR_TEMPL_OUT
:
702 case Common::Assignment::A_PAR_TEMPL_INOUT
: {
705 case Common::Assignment::A_CONST
:
706 case Common::Assignment::A_EXT_CONST
:
712 Type
*refd_gov
= ass
->get_Type();
714 expr
->expr
= mputprintf(expr
->expr
, "const_cast< const %s&>(",
715 refd_gov
->get_genname_template(get_my_scope()).c_str() );
717 expr
->expr
= mputprintf(expr
->expr
, "const_cast< const %s&>(",
718 refd_gov
->get_genname_value(get_my_scope()).c_str());
721 // reference without parameters to a template that has only default formal parameters.
722 // if @lazy: nothing to do, it's a C++ function call just like in case of Ref_pard::generate_code()
723 expr
->expr
= mputprintf(expr
->expr
, "%s(",
724 ass
->get_genname_from_scope(my_scope
).c_str());
725 parlist
->generate_code_alias(expr
, ass
->get_FormalParList(),
726 ass
->get_RunsOnType(), false);
727 expr
->expr
= mputc(expr
->expr
, ')');
729 expr
->expr
= mputstr(expr
->expr
,
730 LazyParamData::in_lazy() ?
731 LazyParamData::add_ref_genname(ass
, my_scope
).c_str() :
732 ass
->get_genname_from_scope(my_scope
).c_str());
734 expr
->expr
= mputstr(expr
->expr
, ")");
736 if (t_subrefs
&& t_subrefs
->get_nof_refs() > 0)
737 t_subrefs
->generate_code(expr
, ass
);
740 void Reference::generate_code_portref(expression_struct_t
*expr
,
744 Common::Assignment
*ass
= get_refd_assignment();
745 if (!ass
) FATAL_ERROR("Reference::generate_code_portref()");
746 expr
->expr
= mputstr(expr
->expr
,
747 ass
->get_genname_from_scope(p_scope
).c_str());
748 if (subrefs
.get_nof_refs() > 0) subrefs
.generate_code(expr
, ass
);
752 void Reference::generate_code_ispresentbound(expression_struct_t
*expr
,
753 bool is_template
, const bool isbound
)
756 Common::Assignment
*ass
= get_refd_assignment();
757 const string
& ass_id
= ass
->get_genname_from_scope(my_scope
);
758 const char *ass_id_str
= ass_id
.c_str();
760 if (subrefs
.get_nof_refs() > 0) {
761 const string
& tmp_generalid
= my_scope
->get_scope_mod_gen()
762 ->get_temporary_id();
763 const char *tmp_generalid_str
= tmp_generalid
.c_str();
765 expression_struct isbound_expr
;
766 Code::init_expr(&isbound_expr
);
767 isbound_expr
.preamble
= mputprintf(isbound_expr
.preamble
,
768 "boolean %s = %s.is_bound();\n", tmp_generalid_str
,
770 ass
->get_Type()->generate_code_ispresentbound(&isbound_expr
, &subrefs
, my_scope
->get_scope_mod_gen(),
771 tmp_generalid
, ass_id
, is_template
, isbound
);
773 expr
->preamble
= mputstr(expr
->preamble
, isbound_expr
.preamble
);
774 expr
->preamble
= mputstr(expr
->preamble
, isbound_expr
.expr
);
775 Code::free_expr(&isbound_expr
);
777 expr
->expr
= mputprintf(expr
->expr
, "%s", tmp_generalid_str
);
779 expr
->expr
= mputprintf(expr
->expr
, "%s.%s(%s)", ass_id_str
,
780 isbound
? "is_bound":"is_present",
781 (!isbound
&& is_template
&& omit_in_value_list
) ? "TRUE" : "");
785 void Reference::detect_modid()
787 // do nothing if detection is already performed
789 // the first element of subrefs must be an <id>
790 const Identifier
*first_id
= subrefs
.get_ref(0)->get_id(), *second_id
= 0;
791 if (subrefs
.get_nof_refs() > 1) {
792 FieldOrArrayRef
*second_ref
= subrefs
.get_ref(1);
793 if (second_ref
->get_type() == FieldOrArrayRef::FIELD_REF
) {
794 // the reference begins with <id>.<id> (most complicated case)
795 // there are 3 possible situations:
796 // 1. first_id points to a local definition (this has the priority)
797 // modid: 0, id: first_id
798 // 2. first_id points to an imported module (trivial case)
799 // modid: first_id, id: second_id
800 // 3. none of the above (first_id might be an imported symbol)
801 // modid: 0, id: first_id
802 // Note: Rule 1 has the priority because it can be overridden using
803 // the notation <id>.objid { ... }.<id> (modid and id are set in the
804 // constructor), but there is no work-around in the reverse way.
805 if (!my_scope
->has_ass_withId(*first_id
)
806 && my_scope
->is_valid_moduleid(*first_id
)) {
807 // rule 1 is not fulfilled, but rule 2 is fulfilled
808 second_id
= second_ref
->get_id();
810 } // else: the reference begins with <id>[<arrayref>] -> there is no modid
811 } // else: the reference consists of a single <id> -> there is no modid
813 modid
= first_id
->clone();
814 id
= second_id
->clone();
815 subrefs
.remove_refs(2);
818 id
= first_id
->clone();
819 subrefs
.remove_refs(1);
823 // =================================
825 // =================================
827 Ref_pard::Ref_pard(const Ref_pard
& p
)
828 : Ref_base(p
), parlist(p
.parlist
), expr_cache(0)
830 params
= p
.params
? p
.params
->clone() : 0;
833 Ref_pard::Ref_pard(Identifier
*p_modid
, Identifier
*p_id
,
834 ParsedActualParameters
*p_params
)
835 : Ref_base(p_modid
, p_id
), parlist(), params(p_params
), expr_cache(0)
838 FATAL_ERROR("Ttcn::Ref_pard::Ref_pard(): NULL parameter");
841 Ref_pard::~Ref_pard()
847 Ref_pard
*Ref_pard::clone() const
849 return new Ref_pard(*this);
852 void Ref_pard::set_fullname(const string
& p_fullname
)
854 Ref_base::set_fullname(p_fullname
);
855 parlist
.set_fullname(p_fullname
);
856 if (params
) params
->set_fullname(p_fullname
);
859 void Ref_pard::set_my_scope(Scope
*p_scope
)
861 Ref_base::set_my_scope(p_scope
);
862 parlist
.set_my_scope(p_scope
);
863 if (params
) params
->set_my_scope(p_scope
);
866 string
Ref_pard::get_dispname()
868 if (is_erroneous
) return string("erroneous");
871 ret_val
+= modid
->get_dispname();
874 ret_val
+= id
->get_dispname();
876 if (params_checked
) {
877 // used after semantic analysis
878 for (size_t i
= 0; i
< parlist
.get_nof_pars(); i
++) {
879 if (i
> 0) ret_val
+= ", ";
880 parlist
.get_par(i
)->append_stringRepr(ret_val
);
883 // used before semantic analysis
884 for (size_t i
= 0; i
< params
->get_nof_tis(); i
++) {
885 if (i
> 0) ret_val
+= ", ";
886 params
->get_ti_byIndex(i
)->append_stringRepr(ret_val
);
890 subrefs
.append_stringRepr(ret_val
);
894 Common::Assignment
* Ref_pard::get_refd_assignment(bool check_parlist
)
896 Common::Assignment
*ass
= Ref_base::get_refd_assignment(check_parlist
);
897 if (ass
&& check_parlist
&& !params_checked
) {
898 params_checked
= true;
899 FormalParList
*fplist
= ass
->get_FormalParList();
901 Error_Context
cntxt(params
, "In actual parameter list of %s",
902 ass
->get_description().c_str());
903 is_erroneous
= fplist
->fold_named_and_chk(params
, &parlist
);
904 parlist
.set_fullname(get_fullname());
905 parlist
.set_my_scope(my_scope
);
906 // the parsed parameter list is no longer needed
910 params
->error("The referenced %s cannot have actual parameters",
911 ass
->get_description().c_str());
917 const Identifier
* Ref_pard::get_modid()
922 const Identifier
* Ref_pard::get_id()
927 ActualParList
*Ref_pard::get_parlist()
929 if (!params_checked
) FATAL_ERROR("Ref_pard::get_parlist()");
933 bool Ref_pard::chk_activate_argument()
935 Common::Assignment
*t_ass
= get_refd_assignment();
936 if (!t_ass
) return false;
937 if (t_ass
->get_asstype() != Common::Assignment::A_ALTSTEP
) {
938 error("Reference to an altstep was expected in the argument instead of "
939 "%s", t_ass
->get_description().c_str());
942 my_scope
->chk_runs_on_clause(t_ass
, *this, "activate");
943 // the altstep reference cannot have sub-references
944 if (get_subrefs()) FATAL_ERROR("Ref_pard::chk_activate_argument()");
945 FormalParList
*fp_list
= t_ass
->get_FormalParList();
946 // the altstep must have formal parameter list
947 if (!fp_list
) FATAL_ERROR("Ref_pard::chk_activate_argument()");
948 return fp_list
->chk_activate_argument(&parlist
,
949 t_ass
->get_description().c_str());
952 bool Ref_pard::has_single_expr()
954 if (!Ref_base::has_single_expr()) return false;
955 for (size_t i
= 0; i
< parlist
.get_nof_pars(); i
++)
956 if (!parlist
.get_par(i
)->has_single_expr()) return false;
957 // if any formal parameter has lazy evaluation
958 Common::Assignment
*ass
= get_refd_assignment();
960 const FormalParList
*fplist
= ass
->get_FormalParList();
962 size_t num_formal
= fplist
->get_nof_fps();
963 for (size_t i
=0; i
<num_formal
; ++i
) {
964 const FormalPar
*fp
= fplist
->get_fp_byIndex(i
);
965 if (fp
->get_lazy_eval()) return false;
972 void Ref_pard::set_code_section(
973 GovernedSimple::code_section_t p_code_section
)
975 Ref_base::set_code_section(p_code_section
);
976 for (size_t i
= 0; i
< parlist
.get_nof_pars(); i
++)
977 parlist
.get_par(i
)->set_code_section(p_code_section
);
980 void Ref_pard::generate_code(expression_struct_t
*expr
)
982 Common::Assignment
*ass
= get_refd_assignment();
983 // C++ function reference with actual parameter list
984 expr
->expr
= mputprintf(expr
->expr
, "%s(",
985 ass
->get_genname_from_scope(my_scope
).c_str());
986 parlist
.generate_code_alias(expr
, ass
->get_FormalParList(),
987 ass
->get_RunsOnType(),false);
988 expr
->expr
= mputc(expr
->expr
, ')');
990 if (subrefs
.get_nof_refs() > 0) subrefs
.generate_code(expr
, ass
);
993 void Ref_pard::generate_code_cached(expression_struct_t
*expr
)
996 expr
->expr
= mputstr(expr
->expr
, expr_cache
);
1000 expr_cache
= mputstr(expr_cache
, expr
->expr
);
1004 void Ref_pard::generate_code_const_ref(expression_struct_t
*expr
)
1006 FieldOrArrayRefs
*t_subrefs
= get_subrefs();
1007 if (!t_subrefs
|| t_subrefs
->get_nof_refs() == 0) {
1008 generate_code(expr
);
1012 Common::Assignment
*ass
= get_refd_assignment();
1013 if (!ass
) FATAL_ERROR("Ref_pard::generate_code_const_ref()");
1016 switch (ass
->get_asstype()) {
1017 case Common::Assignment::A_TEMPLATE
:
1018 if (NULL
== ass
->get_FormalParList()) {
1019 // not a parameterized template
1023 // else fall through
1024 case Common::Assignment::A_CONST
:
1025 case Common::Assignment::A_EXT_CONST
:
1026 case Common::Assignment::A_ALTSTEP
:
1027 case Common::Assignment::A_TESTCASE
:
1028 case Common::Assignment::A_FUNCTION
:
1029 case Common::Assignment::A_EXT_FUNCTION
:
1030 case Common::Assignment::A_FUNCTION_RVAL
:
1031 case Common::Assignment::A_EXT_FUNCTION_RVAL
:
1032 case Common::Assignment::A_FUNCTION_RTEMP
:
1033 case Common::Assignment::A_EXT_FUNCTION_RTEMP
:
1034 generate_code(expr
);
1036 case Common::Assignment::A_MODULEPAR
:
1037 case Common::Assignment::A_VAR
:
1038 case Common::Assignment::A_PAR_VAL_IN
:
1039 case Common::Assignment::A_PAR_VAL_OUT
:
1040 case Common::Assignment::A_PAR_VAL_INOUT
: {
1041 is_template
= false;
1043 case Common::Assignment::A_MODULEPAR_TEMP
:
1044 case Common::Assignment::A_VAR_TEMPLATE
:
1045 case Common::Assignment::A_PAR_TEMPL_IN
:
1046 case Common::Assignment::A_PAR_TEMPL_OUT
:
1047 case Common::Assignment::A_PAR_TEMPL_INOUT
: {
1051 is_template
= false;
1055 Type
*refd_gov
= ass
->get_Type();
1057 generate_code(expr
);
1062 expr
->expr
= mputprintf(expr
->expr
, "const_cast< const %s&>(",
1063 refd_gov
->get_genname_template(get_my_scope()).c_str() );
1065 expr
->expr
= mputprintf(expr
->expr
, "const_cast< const %s%s&>(",
1066 refd_gov
->get_genname_value(get_my_scope()).c_str(),
1067 is_template
? "_template":"");
1070 expr
->expr
= mputprintf(expr
->expr
, "%s(",
1071 ass
->get_genname_from_scope(my_scope
).c_str());
1072 parlist
.generate_code_alias(expr
, ass
->get_FormalParList(),
1073 ass
->get_RunsOnType(), false);
1074 expr
->expr
= mputstr(expr
->expr
, "))");
1076 t_subrefs
->generate_code(expr
, ass
);
1079 // =================================
1080 // ===== NameBridgingScope
1081 // =================================
1082 string
NameBridgingScope::get_scopeMacro_name() const
1084 if (scopeMacro_name
.empty()) FATAL_ERROR("NameBridgingScope::get_scopeMacro_name()");
1085 return scopeMacro_name
;
1088 NameBridgingScope
*NameBridgingScope::clone() const
1090 FATAL_ERROR("NameBridgingScope::clone");
1093 Common::Assignment
* NameBridgingScope::get_ass_bySRef(Ref_simple
*p_ref
)
1095 return get_parent_scope()->get_ass_bySRef(p_ref
);
1098 // =================================
1099 // ===== RunsOnScope
1100 // =================================
1102 RunsOnScope::RunsOnScope(Type
*p_comptype
)
1103 : Scope(), component_type(p_comptype
)
1105 if (!p_comptype
|| p_comptype
->get_typetype() != Type::T_COMPONENT
)
1106 FATAL_ERROR("RunsOnScope::RunsOnScope()");
1107 component_type
->set_ownertype(Type::OT_RUNSON_SCOPE
, this);
1108 component_defs
= p_comptype
->get_CompBody();
1109 set_scope_name("runs on `" + p_comptype
->get_fullname() + "'");
1112 RunsOnScope
*RunsOnScope::clone() const
1114 FATAL_ERROR("RunsOnScope::clone()");
1117 void RunsOnScope::chk_uniq()
1119 // do not perform this check if the component type is defined in the same
1120 // module as the 'runs on' clause
1121 if (parent_scope
->get_scope_mod() == component_defs
->get_scope_mod())
1123 size_t nof_defs
= component_defs
->get_nof_asss();
1124 for (size_t i
= 0; i
< nof_defs
; i
++) {
1125 Common::Assignment
*comp_def
= component_defs
->get_ass_byIndex(i
);
1126 const Identifier
& id
= comp_def
->get_id();
1127 if (parent_scope
->has_ass_withId(id
)) {
1128 comp_def
->warning("Imported component element definition `%s' hides a "
1129 "definition at module scope", comp_def
->get_fullname().c_str());
1130 Reference
ref(0, id
.clone());
1131 Common::Assignment
*hidden_ass
= parent_scope
->get_ass_bySRef(&ref
);
1132 hidden_ass
->warning("Hidden definition `%s' is here",
1133 hidden_ass
->get_fullname().c_str());
1139 RunsOnScope
*RunsOnScope::get_scope_runs_on()
1144 Common::Assignment
*RunsOnScope::get_ass_bySRef(Ref_simple
*p_ref
)
1146 if (!p_ref
) FATAL_ERROR("Ttcn::RunsOnScope::get_ass_bySRef()");
1147 if (p_ref
->get_modid()) return parent_scope
->get_ass_bySRef(p_ref
);
1149 const Identifier
& id
= *p_ref
->get_id();
1150 if (component_defs
->has_local_ass_withId(id
)) {
1151 Common::Assignment
* ass
= component_defs
->get_local_ass_byId(id
);
1152 if (!ass
) FATAL_ERROR("Ttcn::RunsOnScope::get_ass_bySRef()");
1154 if (component_defs
->is_own_assignment(ass
)) return ass
;
1155 else if (ass
->get_visibility() == PUBLIC
) {
1159 p_ref
->error("The member definition `%s' in component type `%s'"
1160 " is not visible in this scope", id
.get_dispname().c_str(),
1161 component_defs
->get_id()->get_dispname().c_str());
1163 } else return parent_scope
->get_ass_bySRef(p_ref
);
1167 bool RunsOnScope::has_ass_withId(const Identifier
& p_id
)
1169 return component_defs
->has_ass_withId(p_id
)
1170 || parent_scope
->has_ass_withId(p_id
);
1173 // =================================
1175 // =================================
1177 FriendMod::FriendMod(Identifier
*p_modid
)
1178 : Node(), modid(p_modid
), w_attrib_path(0), parentgroup(0), checked(false)
1180 if (!p_modid
) FATAL_ERROR("NULL parameter: Ttcn::FriendMod::FriendMod()");
1181 set_fullname("<friends>."+modid
->get_dispname());
1184 FriendMod::~FriendMod()
1188 delete w_attrib_path
;
1191 FriendMod
*FriendMod::clone() const
1193 FATAL_ERROR("Ttcn::FriendMod::clone()");
1196 void FriendMod::set_fullname(const string
& p_fullname
)
1198 if(w_attrib_path
) w_attrib_path
->set_fullname(p_fullname
+ ".<attribpath>");
1201 void FriendMod::chk()
1203 if (checked
) return;
1205 Error_Context
cntxt(this, "In friend module declaration");
1207 if (w_attrib_path
) {
1208 w_attrib_path
->chk_global_attrib();
1209 w_attrib_path
->chk_no_qualif();
1215 void FriendMod::set_with_attr(MultiWithAttrib
* p_attrib
)
1217 if (w_attrib_path
) FATAL_ERROR("FriendMod::set_with_attr()");
1218 w_attrib_path
= new WithAttribPath();
1219 if (p_attrib
&& p_attrib
->get_nof_elements() > 0) {
1220 w_attrib_path
->set_with_attr(p_attrib
);
1224 WithAttribPath
* FriendMod::get_attrib_path()
1226 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
1227 return w_attrib_path
;
1230 void FriendMod::set_parent_path(WithAttribPath
* p_path
)
1232 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
1233 w_attrib_path
->set_parent(p_path
);
1236 void FriendMod::set_parent_group(Group
* p_group
)
1238 if(parentgroup
) FATAL_ERROR("FriendMod::set_parent_group");
1239 parentgroup
= p_group
;
1242 // =================================
1244 // =================================
1246 ImpMod::ImpMod(Identifier
*p_modid
)
1247 : Node(), mod(0), my_mod(0), imptype(I_UNDEF
), modid(p_modid
),
1248 language_spec(0), is_recursive(false),
1249 w_attrib_path(0), parentgroup(0), visibility(PRIVATE
)
1251 if (!p_modid
) FATAL_ERROR("NULL parameter: Ttcn::ImpMod::ImpMod()");
1252 set_fullname("<imports>." + modid
->get_dispname());
1258 delete language_spec
;
1260 delete w_attrib_path
;
1263 ImpMod
*ImpMod::clone() const
1265 FATAL_ERROR("No clone for you!");
1268 void ImpMod::set_fullname(const string
& p_fullname
)
1270 if(w_attrib_path
) w_attrib_path
->set_fullname(p_fullname
+ ".<attribpath>");
1275 if (w_attrib_path
) {
1276 w_attrib_path
->chk_global_attrib();
1277 w_attrib_path
->chk_no_qualif();
1281 void ImpMod::chk_imp(ReferenceChain
& refch
, vector
<Common::Module
>& moduleStack
)
1283 Error_Context
cntxt(this, "In import definition");
1285 if (!modules
->has_mod_withId(*modid
)) {
1286 error("There is no module with identifier `%s'",
1287 modid
->get_dispname().c_str());
1291 Common::Module
*m
= modules
->get_mod_byId(*modid
);
1297 error("A module cannot import from itself");
1305 if (refch
.exists(my_mod
->get_fullname())) {
1306 if(my_mod
->get_moduletype()!=Common::Module::MOD_ASN
){ // Do not warning for circular import in ASN.1 module. It is legal
1307 my_mod
->warning("Circular import chain is not recommended: %s",
1308 refch
.get_dispstr(my_mod
->get_fullname()).c_str());
1313 refch
.add(my_mod
->get_fullname());
1315 if (ImpMod::I_IMPORTIMPORT
== imptype
){
1316 Ttcn::Module
* ttcnmodule
=static_cast<Ttcn::Module
*>(m
);
1317 const Imports
& imp
= ttcnmodule
->get_imports();
1319 for (size_t t
= 0; t
< imp
.impmods_v
.size(); t
++) {
1320 const ImpMod
*im
= imp
.impmods_v
[t
];
1321 const Identifier
& im_id
= im
->get_modid();
1322 Common::Module
*cm
= modules
->get_mod_byId(im_id
); // never NULL
1325 if (PRIVATE
!= im
->get_visibility()) {
1326 if (refch
.exists(m
->get_fullname())) {
1327 if(m
->get_moduletype()!=Common::Module::MOD_ASN
){ // Do not warning for circular import in ASN.1 module. It is legal
1328 m
->warning("Circular import chain is not recommended: %s",
1329 refch
.get_dispstr(m
->get_fullname()).c_str());
1334 refch
.add(m
->get_fullname());
1335 cm
->chk_imp(refch
, moduleStack
);
1341 //refch.mark_state();
1342 m
->chk_imp(refch
, moduleStack
);
1343 //refch.prev_state();
1348 size_t state
=moduleStack
.size();
1349 moduleStack
.replace(state
, moduleStack
.size() - state
);
1352 bool ImpMod::has_imported_def(const Identifier
& p_source_modid
,
1353 const Identifier
& p_id
, const Location
*loc
) const
1355 if (!mod
) return false;
1361 Common::Assignment
* return_assignment
= mod
->importAssignment(p_source_modid
, p_id
);
1362 if (return_assignment
!= NULL
) {
1369 case I_IMPORTIMPORT
:
1371 Ttcn::Module
*tm
= static_cast<Ttcn::Module
*>(mod
); // B
1373 const Imports
& imps
= tm
->get_imports();
1375 vector
<ImpMod
> tempusedImpMods
;
1376 for (size_t i
= 0, num
= imps
.impmods_v
.size(); i
< num
; ++i
) {
1377 ReferenceChain
* referencechain
= new ReferenceChain(this, "NEW IMPORT REFERNCECHAIN");
1378 Common::Assignment
* return_assignment
= imps
.impmods_v
[i
]->
1379 get_imported_def(p_source_modid
, p_id
, loc
, referencechain
, tempusedImpMods
); // C
1380 referencechain
->reset();
1381 delete referencechain
;
1383 if (return_assignment
!= NULL
) {
1389 //satisfy destructor
1390 tempusedImpMods
.clear();
1395 FATAL_ERROR("ImpMod::get_imported_def");
1401 Common::Assignment
*ImpMod::get_imported_def(
1402 const Identifier
& p_source_modid
, const Identifier
& p_id
,
1403 const Location
*loc
, ReferenceChain
* refch
,
1404 vector
<ImpMod
>& usedImpMods
) const
1409 Common::Assignment
* result
= NULL
;
1414 result
= mod
->importAssignment(p_source_modid
, p_id
);
1415 if (result
!= NULL
) {
1416 usedImpMods
.add(const_cast<Ttcn::ImpMod
*>(this));
1420 case I_IMPORTIMPORT
:
1422 Ttcn::Module
*tm
= static_cast<Ttcn::Module
*>(mod
);
1424 const Imports
& imps
= tm
->get_imports();
1425 Common::Assignment
* t_ass
= NULL
;
1427 for (size_t i
= 0, num
= imps
.impmods_v
.size(); i
< num
; ++i
) {
1428 vector
<ImpMod
> tempusedImpMods
;
1430 refch
->mark_state();
1431 if (imps
.impmods_v
[i
]->get_visibility() == PUBLIC
) {
1432 t_ass
= imps
.impmods_v
[i
]->get_imported_def(p_source_modid
, p_id
, loc
, refch
, tempusedImpMods
);
1434 else if (imps
.impmods_v
[i
]->get_visibility() == FRIEND
) {
1435 t_ass
= imps
.impmods_v
[i
]->get_imported_def(p_source_modid
, p_id
, loc
, refch
, tempusedImpMods
);
1437 tempusedImpMods
.add(imps
.impmods_v
[i
]);
1440 refch
->prev_state();
1442 if (t_ass
!= NULL
) {
1443 bool visible
= true;
1444 for (size_t j
= 0; j
< tempusedImpMods
.size(); j
++) {
1445 ImpMod
* impmod_l
= tempusedImpMods
[j
];
1446 //check whether the module is TTCN
1447 if (impmod_l
->get_mod()->get_moduletype() == Common::Module::MOD_TTCN
) {
1448 // cast to ttcn module
1449 Ttcn::Module
*ttcn_m
= static_cast<Ttcn::Module
*>(impmod_l
->get_mod());
1450 if (!ttcn_m
->is_visible(mod
->get_modid(), impmod_l
->get_visibility())) {
1456 for (size_t t
= 0; i
< tempusedImpMods
.size(); i
++) {
1457 usedImpMods
.add(tempusedImpMods
[t
]);
1462 } else if(result
!= t_ass
) {
1467 "It is not possible to resolve the reference unambiguously"
1468 ", as it can be resolved to `%s' and to `%s'",
1469 result
->get_fullname().c_str(), t_ass
->get_fullname().c_str());
1475 tempusedImpMods
.clear();
1478 if (result
!= NULL
) {
1479 usedImpMods
.add(const_cast<Ttcn::ImpMod
*>(this));
1485 FATAL_ERROR("ImpMod::get_imported_def");
1490 void ImpMod::set_language_spec(const char *p_language_spec
)
1492 if (language_spec
) FATAL_ERROR("ImpMod::set_language_spec()");
1493 if (p_language_spec
) language_spec
= new string(p_language_spec
);
1496 void ImpMod::generate_code(output_struct
*target
)
1498 const char *module_name
= modid
->get_name().c_str();
1500 target
->header
.includes
= mputprintf(target
->header
.includes
,
1501 "#include \"%s.hh\"\n",
1502 duplicate_underscores
? module_name
: modid
->get_ttcnname().c_str());
1504 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
1505 "%s%s.pre_init_module();\n", module_name
,
1508 if (mod
->get_moduletype() == Common::Module::MOD_TTCN
) {
1509 target
->functions
.post_init
= mputprintf(target
->functions
.post_init
,
1510 "%s%s.post_init_module();\n", module_name
,
1516 void ImpMod::dump(unsigned level
) const
1518 DEBUG(level
, "Import from module %s", modid
->get_dispname().c_str());
1519 if (w_attrib_path
) {
1520 MultiWithAttrib
*attrib
= w_attrib_path
->get_with_attr();
1522 DEBUG(level
+ 1, "Attributes:");
1523 attrib
->dump(level
+ 2);
1528 void ImpMod::set_with_attr(MultiWithAttrib
* p_attrib
)
1530 if (w_attrib_path
) FATAL_ERROR("ImpMod::set_with_attr()");
1531 w_attrib_path
= new WithAttribPath();
1532 if (p_attrib
&& p_attrib
->get_nof_elements() > 0) {
1533 w_attrib_path
->set_with_attr(p_attrib
);
1537 WithAttribPath
* ImpMod::get_attrib_path()
1539 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
1540 return w_attrib_path
;
1543 void ImpMod::set_parent_path(WithAttribPath
* p_path
)
1545 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
1546 w_attrib_path
->set_parent(p_path
);
1549 void ImpMod::set_parent_group(Group
* p_group
)
1551 if(parentgroup
) FATAL_ERROR("ImpMod::set_parent_group");
1552 parentgroup
= p_group
;
1555 // =================================
1557 // =================================
1561 for (size_t i
= 0; i
< impmods_v
.size(); i
++)
1562 delete impmods_v
[i
];
1566 Imports
*Imports::clone() const
1568 FATAL_ERROR("Ttcn::Imports::clone()");
1571 void Imports::add_impmod(ImpMod
*p_impmod
)
1573 if (!p_impmod
) FATAL_ERROR("Ttcn::Imports::add_impmod()");
1574 impmods_v
.add(p_impmod
);
1575 p_impmod
->set_my_mod(my_mod
);
1578 void Imports::set_my_mod(Module
*p_mod
)
1581 for(size_t i
= 0; i
< impmods_v
.size(); i
++)
1582 impmods_v
[i
]->set_my_mod(my_mod
);
1585 bool Imports::has_impmod_withId(const Identifier
& p_id
) const
1587 for (size_t i
= 0, size
= impmods_v
.size(); i
< size
; ++i
) {
1588 const ImpMod
* im
= impmods_v
[i
];
1589 const Identifier
& im_id
= im
->get_modid();
1590 const string
& im_name
= im_id
.get_name();
1591 if (p_id
.get_name() == im_name
) {
1592 // The identifier represents a module imported in the current module
1599 void Imports::chk_imp(ReferenceChain
& refch
, vector
<Common::Module
>& moduleStack
)
1601 if (impmods_v
.size() <= 0) return;
1603 if (!my_mod
) FATAL_ERROR("Ttcn::Imports::chk_imp()");
1608 for(size_t n
= 0; n
< impmods_v
.size(); n
++)
1610 impmods_v
[n
]->chk();
1613 //TODO this whole thing should be moved into impmod::chk
1614 Identifier
address_id(Identifier::ID_TTCN
, string("address"));
1615 for (size_t n
= 0; n
< impmods_v
.size(); n
++) {
1616 ImpMod
*im
= impmods_v
[n
];
1617 im
->chk_imp(refch
, moduleStack
);
1619 const Identifier
& im_id
= im
->get_modid();
1620 Common::Module
*m
= modules
->get_mod_byId(im_id
);
1623 if (m
->get_gen_code()) my_mod
->set_gen_code();
1624 } // next assignment
1627 bool Imports::has_imported_def(const Identifier
& p_id
, const Location
*loc
) const
1629 for (size_t n
= 0; n
< impmods_v
.size(); n
++) {
1630 ImpMod
*im
= impmods_v
[n
];
1631 bool return_bool
= im
->has_imported_def(my_mod
->get_modid(), p_id
, loc
);
1632 if (return_bool
) return true;
1637 Common::Assignment
*Imports::get_imported_def(
1638 const Identifier
& p_source_modid
, const Identifier
& p_id
,
1639 const Location
*loc
, ReferenceChain
* refch
)
1641 vector
<ImpMod
> tempusedImpMods
;
1642 Common::Assignment
* result
= NULL
;
1643 for (size_t n
= 0; n
< impmods_v
.size(); n
++) {
1644 ImpMod
*im
= impmods_v
[n
];
1645 refch
->mark_state();
1646 Common::Assignment
* ass
= im
->get_imported_def(
1647 p_source_modid
, p_id
, loc
, refch
, tempusedImpMods
);
1648 tempusedImpMods
.clear();
1649 refch
->prev_state();
1654 } else if(result
!= ass
&& loc
) {
1659 "It is not possible to resolve the reference unambiguously"
1660 ", as it can be resolved to `%s' and to `%s'",
1661 result
->get_fullname().c_str(), ass
->get_fullname().c_str());
1669 void Imports::get_imported_mods(Module::module_set_t
& p_imported_mods
) const
1671 for (size_t i
= 0; i
< impmods_v
.size(); i
++) {
1672 ImpMod
*im
= impmods_v
[i
];
1673 Common::Module
*m
= im
->get_mod();
1675 if (!p_imported_mods
.has_key(m
)) {
1676 p_imported_mods
.add(m
, 0);
1677 m
->get_visible_mods(p_imported_mods
);
1682 void Imports::generate_code(output_struct
*target
)
1684 target
->header
.includes
= mputstr(target
->header
.includes
,
1685 "#include <TTCN3.hh>\n");
1686 for (size_t i
= 0; i
< impmods_v
.size(); i
++) {
1687 ImpMod
*im
= impmods_v
[i
];
1688 Common::Module
*m
= im
->get_mod();
1689 // inclusion of m's header file can be eliminated if we find another
1690 // imported module that imports m
1691 bool covered
= false;
1692 for (size_t j
= 0; j
< impmods_v
.size(); j
++) {
1693 // skip over the same import definition
1694 if (j
== i
) continue;
1695 ImpMod
*im2
= impmods_v
[j
];
1696 Common::Module
*m2
= im2
->get_mod();
1697 // a module that is equivalent to the current module due to
1698 // circular imports cannot be used to cover anything
1699 if (m2
->is_visible(my_mod
)) continue;
1700 if (m2
->is_visible(m
) && !m
->is_visible(m2
)) {
1701 // m2 covers m (i.e. m is visible from m2)
1702 // and they are not in the same import loop
1707 // do not generate the #include if a covering module is found
1708 if (!covered
) im
->generate_code(target
);
1712 void Imports::generate_code(CodeGenHelper
& cgh
) {
1713 generate_code(cgh
.get_current_outputstruct());
1716 void Imports::dump(unsigned level
) const
1718 DEBUG(level
, "Imports (%lu pcs.)", (unsigned long) impmods_v
.size());
1719 for (size_t i
= 0; i
< impmods_v
.size(); i
++)
1720 impmods_v
[i
]->dump(level
+ 1);
1723 // =================================
1724 // ===== Definitions
1725 // =================================
1727 Definitions::~Definitions()
1729 for(size_t i
= 0; i
< ass_v
.size(); i
++) delete ass_v
[i
];
1734 Definitions
*Definitions::clone() const
1736 FATAL_ERROR("Definitions::clone");
1739 void Definitions::add_ass(Definition
*p_ass
)
1741 // it is too late to add a new one after it has been checked.
1742 if (checked
|| !p_ass
) FATAL_ERROR("Ttcn::OtherDefinitions::add_ass()");
1744 p_ass
->set_my_scope(this);
1747 void Definitions::set_fullname(const string
& p_fullname
)
1749 Common::Assignments::set_fullname(p_fullname
);
1750 for(size_t i
= 0; i
< ass_v
.size(); i
++) {
1751 Definition
*ass
= ass_v
[i
];
1752 ass
->set_fullname(p_fullname
+ "." + ass
->get_id().get_dispname());
1756 bool Definitions::has_local_ass_withId(const Identifier
& p_id
)
1758 if (!checked
) chk_uniq();
1759 return ass_m
.has_key(p_id
.get_name());
1762 Common::Assignment
* Definitions::get_local_ass_byId(const Identifier
& p_id
)
1764 if (!checked
) chk_uniq();
1765 return ass_m
[p_id
.get_name()];
1768 size_t Definitions::get_nof_asss()
1770 if (!checked
) chk_uniq();
1771 return ass_m
.size();
1774 size_t Definitions::get_nof_raw_asss()
1776 return ass_v
.size();
1779 Common::Assignment
* Definitions::get_ass_byIndex(size_t p_i
)
1781 if (!checked
) chk_uniq();
1782 return ass_m
.get_nth_elem(p_i
);
1785 Ttcn::Definition
* Definitions::get_raw_ass_byIndex(size_t p_i
) {
1789 void Definitions::chk_uniq()
1791 if (checked
) return;
1793 for (size_t i
= 0; i
< ass_v
.size(); i
++) {
1794 Definition
*ass
= ass_v
[i
];
1795 const Identifier
& id
= ass
->get_id();
1796 const string
& name
= id
.get_name();
1797 if (ass_m
.has_key(name
)) {
1798 const char *dispname_str
= id
.get_dispname().c_str();
1799 ass
->error("Duplicate definition with name `%s'", dispname_str
);
1800 ass_m
[name
]->note("Previous definition of `%s' is here", dispname_str
);
1802 ass_m
.add(name
, ass
);
1803 if (parent_scope
->is_valid_moduleid(id
)) {
1804 ass
->warning("Definition with name `%s' hides a module identifier",
1805 id
.get_dispname().c_str());
1812 void Definitions::chk()
1814 for (size_t i
= 0; i
< ass_v
.size(); i
++) ass_v
[i
]->chk();
1817 void Definitions::chk_for()
1819 if (checked
) return;
1822 // all checks are done in one iteration because
1823 // forward referencing is not allowed between the definitions
1824 for (size_t i
= 0; i
< ass_v
.size(); i
++) {
1825 Definition
*def
= ass_v
[i
];
1826 const Identifier
& id
= def
->get_id();
1827 const string
& name
= id
.get_name();
1828 if (ass_m
.has_key(name
)) {
1829 const char *dispname_str
= id
.get_dispname().c_str();
1830 def
->error("Duplicate definition with name `%s'", dispname_str
);
1831 ass_m
[name
]->note("Previous definition of `%s' is here", dispname_str
);
1833 ass_m
.add(name
, def
);
1835 if (parent_scope
->has_ass_withId(id
)) {
1836 const char *dispname_str
= id
.get_dispname().c_str();
1837 def
->error("Definition with identifier `%s' is not unique in the "
1838 "scope hierarchy", dispname_str
);
1839 Reference
ref(0, id
.clone());
1840 Common::Assignment
*ass
= parent_scope
->get_ass_bySRef(&ref
);
1841 if (!ass
) FATAL_ERROR("OtherDefinitions::chk_for()");
1842 ass
->note("Previous definition with identifier `%s' in higher "
1843 "scope unit is here", dispname_str
);
1844 } else if (parent_scope
->is_valid_moduleid(id
)) {
1845 def
->warning("Definition with name `%s' hides a module identifier",
1846 id
.get_dispname().c_str());
1854 void Definitions::set_genname(const string
& prefix
)
1856 for (size_t i
= 0; i
< ass_v
.size(); i
++) {
1857 Definition
*def
= ass_v
[i
];
1858 def
->set_genname(prefix
+ def
->get_id().get_name());
1862 void Definitions::generate_code(output_struct
* target
)
1864 for(size_t i
= 0; i
< ass_v
.size(); i
++) ass_v
[i
]->generate_code(target
);
1867 void Definitions::generate_code(CodeGenHelper
& cgh
) {
1869 for(size_t i
= 0; i
< ass_v
.size(); i
++) ass_v
[i
]->generate_code(cgh
);
1872 char* Definitions::generate_code_str(char *str
)
1874 for(size_t i
=0; i
<ass_v
.size(); i
++) {
1875 str
= ass_v
[i
]->update_location_object(str
);
1876 str
=ass_v
[i
]->generate_code_str(str
);
1881 void Definitions::ilt_generate_code(ILT
*ilt
)
1883 for(size_t i
=0; i
<ass_v
.size(); i
++)
1884 ass_v
[i
]->ilt_generate_code(ilt
);
1888 void Definitions::dump(unsigned level
) const
1890 DEBUG(level
, "Definitions: (%lu pcs.)", (unsigned long) ass_v
.size());
1891 for(size_t i
= 0; i
< ass_v
.size(); i
++) ass_v
[i
]->dump(level
+ 1);
1894 // =================================
1896 // =================================
1898 Group::Group(Identifier
*p_id
)
1899 : Node(), Location(), parent_group(0), w_attrib_path(0), id(p_id
),
1902 if (!p_id
) FATAL_ERROR("Group::Group()");
1907 delete w_attrib_path
;
1910 for (size_t i
= 0; i
< group_v
.size(); i
++) delete group_v
[i
];
1914 friendmods_v
.clear();
1918 Group
* Group::clone() const
1920 FATAL_ERROR("Ttcn::Group::clone()");
1923 void Group::set_fullname(const string
& p_fullname
)
1925 Node::set_fullname(p_fullname
);
1926 for(size_t i
= 0; i
< group_v
.size() ; i
++)
1928 group_v
[i
]->set_fullname(p_fullname
+ ".<group " + Int2string(i
) + ">");
1930 if (w_attrib_path
) w_attrib_path
->set_fullname( p_fullname
1934 void Group::add_ass(Definition
* p_ass
)
1937 p_ass
->set_parent_group(this);
1940 void Group::add_group(Group
* p_group
)
1942 group_v
.add(p_group
);
1945 void Group::set_parent_group(Group
* p_parent_group
)
1947 if (parent_group
) FATAL_ERROR("Group::set_parent_group()");
1948 parent_group
= p_parent_group
;
1951 void Group::set_with_attr(MultiWithAttrib
* p_attrib
)
1953 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
1954 w_attrib_path
->set_with_attr(p_attrib
);
1957 WithAttribPath
* Group::get_attrib_path()
1959 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
1960 return w_attrib_path
;
1963 void Group::set_parent_path(WithAttribPath
* p_path
)
1965 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
1966 w_attrib_path
->set_parent(p_path
);
1969 void Group::chk_uniq()
1971 if (checked
) return;
1975 for (size_t i
= 0; i
< ass_v
.size(); i
++) {
1976 Definition
*ass
= ass_v
[i
];
1977 const string
& ass_name
= ass
->get_id().get_name();
1978 if (!ass_m
.has_key(ass_name
)) ass_m
.add(ass_name
, ass
);
1981 for(size_t i
= 0; i
< group_v
.size(); i
++) {
1982 Group
*group
= group_v
[i
];
1983 const Identifier
& group_id
= group
->get_id();
1984 const string
& group_name
= group_id
.get_name();
1985 if (ass_m
.has_key(group_name
)) {
1986 group
->error("Group name `%s' clashes with a definition",
1987 group_id
.get_dispname().c_str());
1988 ass_m
[group_name
]->note("Definition of `%s' is here",
1989 group_id
.get_dispname().c_str());
1991 if (group_m
.has_key(group_name
)) {
1992 group
->error("Duplicate group with name `%s'",
1993 group_id
.get_dispname().c_str());
1994 group_m
[group_name
]->note("Group `%s' is already defined here",
1995 group_id
.get_dispname().c_str());
1996 } else group_m
.add(group_name
, group
);
2003 Error_Context
cntxt(this, "In group `%s'", id
->get_dispname().c_str());
2007 if (w_attrib_path
) {
2008 w_attrib_path
->chk_global_attrib();
2009 w_attrib_path
->chk_no_qualif();
2012 for(size_t i
= 0; i
< group_v
.size(); i
++) group_v
[i
]->chk();
2015 void Group::add_impmod(ImpMod
*p_impmod
)
2017 impmods_v
.add(p_impmod
);
2018 p_impmod
->set_parent_group(this);
2021 void Group::add_friendmod(FriendMod
*p_friendmod
)
2023 friendmods_v
.add(p_friendmod
);
2024 p_friendmod
->set_parent_group(this);
2027 void Group::dump(unsigned level
) const
2029 DEBUG(level
, "Group: %s", id
->get_dispname().c_str());
2030 DEBUG(level
+ 1, "Nested groups: (%lu pcs.)",
2031 (unsigned long) group_v
.size());
2032 for(size_t i
= 0; i
< group_v
.size(); i
++) group_v
[i
]->dump(level
+ 2);
2033 DEBUG(level
+ 1, "Nested definitions: (%lu pcs.)",
2034 (unsigned long) ass_v
.size());
2035 for(size_t i
= 0; i
< ass_v
.size(); i
++) ass_v
[i
]->dump(level
+ 2);
2036 DEBUG(level
+ 1, "Nested imports: (%lu pcs.)",
2037 (unsigned long) impmods_v
.size());
2038 for(size_t i
= 0; i
< impmods_v
.size(); i
++) impmods_v
[i
]->dump(level
+ 2);
2039 if (w_attrib_path
) {
2040 MultiWithAttrib
*attrib
= w_attrib_path
->get_with_attr();
2042 DEBUG(level
+ 1, "Group Attributes:");
2043 attrib
->dump(level
+ 2);
2048 // =================================
2049 // ===== ControlPart
2050 // =================================
2052 ControlPart::ControlPart(StatementBlock
* p_block
)
2053 : Node(), Location(), block(p_block
), w_attrib_path(0)
2055 if (!p_block
) FATAL_ERROR("ControlPart::ControlPart()");
2058 ControlPart::~ControlPart()
2061 delete w_attrib_path
;
2064 ControlPart
* ControlPart::clone() const
2066 FATAL_ERROR("ControlPart::clone");
2069 void ControlPart::set_fullname(const string
& p_fullname
)
2071 block
->set_fullname(p_fullname
);
2072 if(w_attrib_path
) w_attrib_path
->set_fullname(p_fullname
+ ".<attribpath>");
2075 void ControlPart::set_my_scope(Scope
*p_scope
)
2077 bridgeScope
.set_parent_scope(p_scope
);
2078 string
temp("control");
2079 bridgeScope
.set_scopeMacro_name(temp
);
2081 block
->set_my_scope(&bridgeScope
);
2084 void ControlPart::chk()
2086 Error_Context
cntxt(this, "In control part");
2088 if (!semantic_check_only
)
2089 block
->set_code_section(GovernedSimple::CS_INLINE
);
2090 if (w_attrib_path
) {
2091 w_attrib_path
->chk_global_attrib();
2092 w_attrib_path
->chk_no_qualif();
2096 void ControlPart::generate_code(output_struct
*target
, Module
*my_module
)
2098 const char *module_dispname
= my_module
->get_modid().get_dispname().c_str();
2099 target
->functions
.control
=
2100 create_location_object(target
->functions
.control
, "CONTROLPART",
2102 target
->functions
.control
= mputprintf(target
->functions
.control
,
2103 "TTCN_Runtime::begin_controlpart(\"%s\");\n", module_dispname
);
2104 if (debugger_active
) {
2105 target
->functions
.control
= mputprintf(target
->functions
.control
,
2106 "charstring_list no_params = NULL_VALUE;\n"
2107 "TTCN3_Debug_Function debug_scope(NULL, \"control\", \"%s\", no_params, no_params, NULL);\n"
2108 "debug_scope.initial_snapshot();\n", module_dispname
);
2110 target
->functions
.control
=
2111 block
->generate_code(target
->functions
.control
);
2112 target
->functions
.control
= mputstr(target
->functions
.control
,
2113 "TTCN_Runtime::end_controlpart();\n");
2116 void ControlPart::set_with_attr(MultiWithAttrib
* p_attrib
)
2118 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
2119 w_attrib_path
->set_with_attr(p_attrib
);
2122 WithAttribPath
* ControlPart::get_attrib_path()
2124 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
2125 return w_attrib_path
;
2128 void ControlPart::set_parent_path(WithAttribPath
* p_path
)
2130 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
2131 w_attrib_path
->set_parent(p_path
);
2132 block
->set_parent_path(w_attrib_path
);
2135 void ControlPart::dump(unsigned level
) const
2137 DEBUG(level
, "Control part");
2138 block
->dump(level
+ 1);
2139 if (w_attrib_path
) {
2140 MultiWithAttrib
*attrib
= w_attrib_path
->get_with_attr();
2142 DEBUG(level
+ 1, "Attributes:");
2143 attrib
->dump(level
+ 2);
2148 // =================================
2150 // =================================
2152 Module::Module(Identifier
*p_modid
)
2153 : Common::Module(MOD_TTCN
, p_modid
), language_spec(0), w_attrib_path(0),
2156 asss
= new Definitions();
2157 asss
->set_parent_scope(this);
2158 imp
= new Imports();
2159 imp
->set_my_mod(this);
2160 //modified_encodings = true; // Assume always true for TTCN modules
2165 delete language_spec
;
2167 for (size_t i
= 0; i
< group_v
.size(); i
++) delete group_v
[i
];
2171 for (size_t i
= 0; i
< friendmods_v
.size(); i
++) delete friendmods_v
[i
];
2172 friendmods_v
.clear();
2174 for (size_t i
= 0; i
< runs_on_scopes
.size(); i
++)
2175 delete runs_on_scopes
[i
];
2176 runs_on_scopes
.clear();
2177 delete w_attrib_path
;
2180 void Module::add_group(Group
* p_group
)
2182 group_v
.add(p_group
);
2185 void Module::add_friendmod(FriendMod
*p_friendmod
)
2187 friendmods_v
.add(p_friendmod
);
2188 p_friendmod
->set_my_mod(this);
2191 Module
*Module::clone() const
2193 FATAL_ERROR("Ttcn::Module::clone()");
2196 Common::Assignment
*Module::importAssignment(const Identifier
& p_modid
,
2197 const Identifier
& p_id
) const
2199 if (asss
->has_local_ass_withId(p_id
)) {
2200 Common::Assignment
* ass
= asss
->get_local_ass_byId(p_id
);
2201 if (!ass
) FATAL_ERROR("Ttcn::Module::importAssignment()");
2203 switch(ass
->get_visibility()) {
2205 for (size_t i
= 0; i
< friendmods_v
.size(); i
++) {
2206 if (friendmods_v
[i
]->get_modid() == p_modid
) return ass
;
2214 FATAL_ERROR("Ttcn::Module::importAssignment()");
2220 void Module::set_fullname(const string
& p_fullname
)
2222 Node::set_fullname(p_fullname
);
2223 asss
->set_fullname(p_fullname
);
2224 if (controlpart
) controlpart
->set_fullname(p_fullname
+ ".control");
2225 for(size_t i
= 0; i
< group_v
.size(); i
++)
2227 group_v
[i
]->set_fullname(p_fullname
+ ".<group " + Int2string(i
) + ">");
2229 if (w_attrib_path
) w_attrib_path
->set_fullname(p_fullname
2233 Common::Assignments
*Module::get_scope_asss()
2238 bool Module::has_imported_ass_withId(const Identifier
& p_id
)
2240 const Location
*loc
= NULL
;
2241 for (size_t i
= 0, num
= imp
->get_imports_size(); i
< num
; ++i
) {
2242 const ImpMod
* im
= imp
->get_impmod(i
);
2243 //TODO use a reference instead of an identifier
2244 if(im
->has_imported_def(*modid
, p_id
, loc
)) return true;
2249 Common::Assignment
* Module::get_ass_bySRef(Ref_simple
*p_ref
)
2251 const Identifier
*r_modid
= p_ref
->get_modid();
2252 const Identifier
*r_id
= p_ref
->get_id();
2254 // the reference contains a module name
2255 if (r_modid
->get_name() != modid
->get_name()) {
2256 // the reference points to another module
2257 bool has_impmod_with_name
= false;
2258 Common::Assignment
* result_ass
= NULL
;
2259 for (size_t i
= 0, num
= imp
->get_imports_size(); i
< num
; ++i
) {
2260 const ImpMod
* im
= imp
->get_impmod(i
);
2261 const Identifier
& im_id
= im
->get_modid();
2262 const string
& im_name
= im_id
.get_name();
2263 if (r_modid
->get_name() == im_name
) {
2264 has_impmod_with_name
= true;
2265 vector
<ImpMod
> tempusedImpMods
;
2266 ReferenceChain
* referencechain
= new ReferenceChain(this, "NEW IMPORT REFERNCECHAIN");
2267 Common::Assignment
*t_ass
= im
->get_imported_def(*modid
, *r_id
, p_ref
, referencechain
, tempusedImpMods
);
2268 referencechain
->reset();
2269 delete referencechain
;
2271 if (t_ass
!= NULL
) {
2272 Ttcn::Module
*ttcn_m
= static_cast<Ttcn::Module
*>(im
->get_mod());
2273 if (!ttcn_m
->is_visible(*modid
, t_ass
->get_visibility())) {
2277 if (t_ass
!= NULL
) {
2278 if (result_ass
== NULL
) {
2280 } else if(result_ass
!= t_ass
) {
2282 "It is not possible to resolve the reference unambiguously"
2283 ", as it can be resolved to `%s' and to `%s'",
2284 result_ass
->get_fullname().c_str(), t_ass
->get_fullname().c_str());
2288 tempusedImpMods
.clear();
2291 if (result_ass
) return result_ass
;
2293 if (has_impmod_with_name
) {
2294 p_ref
->error("There is no definition with name `%s' visible from "
2295 "module `%s'", r_id
->get_dispname().c_str(),
2296 r_modid
->get_dispname().c_str());
2298 if (modules
->has_mod_withId(*r_modid
)) {
2299 Common::Module
*m
= modules
->get_mod_byId(*r_modid
);
2300 if (m
->get_asss()->has_ass_withId(*r_id
)) {
2301 p_ref
->error("Definition with name `%s' is not imported from "
2302 "module `%s'", r_id
->get_dispname().c_str(),
2303 r_modid
->get_dispname().c_str());
2305 p_ref
->error("There is no definition with name `%s' in "
2306 "module `%s'", r_id
->get_dispname().c_str(),
2307 r_modid
->get_dispname().c_str());
2310 p_ref
->error("There is no module with name `%s'",
2311 r_modid
->get_dispname().c_str());
2316 // the reference points to the own module
2317 if (asss
->has_local_ass_withId(*r_id
)) {
2318 return asss
->get_local_ass_byId(*r_id
);
2320 p_ref
->error("There is no definition with name `%s' in "
2321 "module `%s'", r_id
->get_dispname().c_str(),
2322 r_modid
->get_dispname().c_str());
2326 // no module name is given in the reference
2327 if (asss
->has_local_ass_withId(*r_id
)) {
2328 return asss
->get_local_ass_byId(*r_id
);
2330 // the reference was not found locally -> look at the import list
2331 Common::Assignment
*t_result
= NULL
, *t_ass
= NULL
;
2332 for (size_t i
= 0, num
= imp
->get_imports_size(); i
< num
; ++i
) {
2333 const ImpMod
* im
= imp
->get_impmod(i
);
2335 vector
<ImpMod
> tempusedImpMods
;
2336 ReferenceChain
* referencechain
= new ReferenceChain(this, "NEW IMPORT REFERNCECHAIN");
2337 t_ass
= im
->get_imported_def(*modid
, *r_id
, p_ref
, referencechain
, tempusedImpMods
);
2338 referencechain
->reset();
2340 delete referencechain
;
2341 if (t_ass
!= NULL
) {
2342 Ttcn::Module
*ttcn_m
= static_cast<Ttcn::Module
*>(im
->get_mod());
2343 if (!ttcn_m
->is_visible(*modid
, t_ass
->get_visibility())) {
2347 if (t_ass
!= NULL
) {
2348 if (t_result
== NULL
) {
2350 } else if(t_result
!= t_ass
) {
2352 "It is not possible to resolve the reference unambiguously"
2353 ", as it can be resolved to `%s' and to `%s'",
2354 t_result
->get_fullname().c_str(), t_ass
->get_fullname().c_str());
2358 tempusedImpMods
.clear();
2361 if (t_result
) return t_result
;
2363 p_ref
->error("There is no local or imported definition with name `%s'"
2364 ,r_id
->get_dispname().c_str());
2370 bool Module::is_valid_moduleid(const Identifier
& p_id
)
2372 // The identifier represents the current module
2373 if (p_id
.get_name() == modid
->get_name()) return true;
2374 // The identifier represents a module imported in the current module
2375 if(imp
->has_impmod_withId(p_id
)) return true;
2379 Common::Assignments
*Module::get_asss()
2384 bool Module::exports_sym(const Identifier
&)
2386 FATAL_ERROR("Ttcn::Module::exports_sym()");
2390 Type
*Module::get_address_type()
2392 Identifier
address_id(Identifier::ID_TTCN
, string("address"));
2393 // return NULL if address type is not defined
2394 if (!asss
->has_local_ass_withId(address_id
)) return 0;
2395 Common::Assignment
*t_ass
= asss
->get_local_ass_byId(address_id
);
2396 if (t_ass
->get_asstype() != Common::Assignment::A_TYPE
)
2397 FATAL_ERROR("Module::get_address_type(): address is not a type");
2398 return t_ass
->get_Type();
2401 void Module::chk_imp(ReferenceChain
& refch
, vector
<Common::Module
>& moduleStack
)
2403 if (imp_checked
) return;
2404 const string
& module_name
= modid
->get_dispname();
2406 Error_Context backup
;
2407 Error_Context
cntxt(this, "In TTCN-3 module `%s'", module_name
.c_str());
2408 imp
->chk_imp(refch
, moduleStack
);
2411 collect_visible_mods();
2416 DEBUG(1, "Checking TTCN-3 module `%s'", modid
->get_dispname().c_str());
2417 Error_Context
cntxt(this, "In TTCN-3 module `%s'",
2418 modid
->get_dispname().c_str());
2419 if (w_attrib_path
) {
2420 w_attrib_path
->chk_global_attrib();
2421 w_attrib_path
->chk_no_qualif();
2423 // Check "extension" attributes in the module's "with" statement
2424 MultiWithAttrib
*multi
= w_attrib_path
->get_with_attr();
2425 if (multi
) for (size_t i
= 0; i
< multi
->get_nof_elements(); ++i
) {
2426 const SingleWithAttrib
*single
= multi
->get_element(i
);
2427 if (single
->get_attribKeyword() != SingleWithAttrib::AT_EXTENSION
) continue;
2428 // Parse the extension attribute
2429 // We circumvent parse_extattributes() in coding_attrib_p.y because
2430 // it processes all attributes in the "with" statement and
2431 // doesn't allow the removal on a case-by-case basis.
2433 init_coding_attrib_lex(single
->get_attribSpec());
2434 int result
= coding_attrib_parse();// 0=OK, 1=error, 2=out of memory
2435 cleanup_coding_attrib_lex();
2442 const size_t num_parsed
= extatrs
->size();
2443 for (size_t a
= 0; a
< num_parsed
; ++a
) {
2444 Ttcn::ExtensionAttribute
& ex
= extatrs
->get(0);
2446 switch (ex
.get_type()) {
2447 case Ttcn::ExtensionAttribute::VERSION_TEMPLATE
:
2448 case Ttcn::ExtensionAttribute::VERSION
: {
2449 char* act_product_number
;
2450 unsigned int act_suffix
, act_rel
, act_patch
, act_build
;
2452 (void)ex
.get_id(act_product_number
, act_suffix
, act_rel
, act_patch
, act_build
, extra_junk
);
2454 if (release
!= UINT_MAX
) {
2455 ex
.error("Duplicate 'version' attribute");
2458 product_number
= mcopystr(act_product_number
);
2459 suffix
= act_suffix
;
2463 extra
= mcopystr(extra_junk
);
2465 // Avoid propagating the attribute needlessly
2466 multi
->delete_element(i
--);
2470 case Ttcn::ExtensionAttribute::REQUIRES
: {
2471 // Imports have already been checked
2472 char* exp_product_number
;
2473 unsigned int exp_suffix
, exp_rel
, exp_patch
, exp_build
;
2475 Common::Identifier
*req_id
= ex
.get_id(exp_product_number
,
2476 exp_suffix
, exp_rel
, exp_patch
, exp_build
, exp_extra
);
2478 if (imp
->has_impmod_withId(*req_id
)) {
2479 Common::Module
* m
= modules
->get_mod_byId(*req_id
);
2480 if (m
->product_number
== NULL
&& exp_product_number
!= NULL
) {
2481 ex
.error("Module '%s' requires module '%s' of product %s"
2482 ", but it is not specified",
2483 this->modid
->get_dispname().c_str(), req_id
->get_dispname().c_str(),
2484 exp_product_number
);
2485 multi
->delete_element(i
--);
2488 } else if (exp_product_number
== NULL
&&
2489 m
->product_number
!= NULL
&& strcmp(m
->product_number
, "") > 0){
2490 ex
.warning("Module '%s' requires module '%s' of any product"
2491 ", while it specifies '%s'",
2492 this->modid
->get_dispname().c_str(),
2493 req_id
->get_dispname().c_str(), m
->product_number
);
2494 } else if (m
->product_number
!= NULL
&& exp_product_number
!= NULL
2495 && 0 != strcmp(m
->product_number
, exp_product_number
)) {
2496 char *req_product_identifier
=
2497 get_product_identifier(exp_product_number
,
2498 exp_suffix
, exp_rel
, exp_patch
, exp_build
);
2499 char *mod_product_identifier
=
2500 get_product_identifier(m
->product_number
,
2501 m
->suffix
, m
->release
, m
->patch
, m
->build
);
2503 ex
.error("Module '%s' requires version %s of module"
2504 " '%s', but only %s is available",
2505 this->modid
->get_dispname().c_str(), req_product_identifier
,
2506 req_id
->get_dispname().c_str(), mod_product_identifier
);
2507 Free(req_product_identifier
);
2508 Free(mod_product_identifier
);
2509 multi
->delete_element(i
--);
2513 // different suffixes are always incompatible
2514 // unless the special version number is used
2515 if (m
->suffix
!= exp_suffix
&& (m
->suffix
!= UINT_MAX
)) {
2516 char *req_product_identifier
=
2517 get_product_identifier(exp_product_number
,exp_suffix
, exp_rel
, exp_patch
, exp_build
);
2518 char *mod_product_identifier
=
2519 get_product_identifier(m
->product_number
,
2520 m
->suffix
, m
->release
, m
->patch
, m
->build
);
2522 ex
.error("Module '%s' requires version %s of module"
2523 " '%s', but only %s is available",
2524 this->modid
->get_dispname().c_str(), req_product_identifier
,
2525 req_id
->get_dispname().c_str(), mod_product_identifier
);
2526 Free(req_product_identifier
);
2527 Free(mod_product_identifier
);
2528 multi
->delete_element(i
--);
2532 if ( m
->release
< exp_rel
2533 ||(m
->release
== exp_rel
&& m
->patch
< exp_patch
)
2534 ||(m
->patch
== exp_patch
&& m
->build
< exp_build
)) {
2535 char *mod_bld_str
= buildstr(m
->build
);
2536 char *exp_bld_str
= buildstr(exp_build
);
2537 if (mod_bld_str
==0 || exp_bld_str
==0) FATAL_ERROR(
2538 "Ttcn::Module::chk() invalid build number");
2539 ex
.error("Module '%s' requires version R%u%c%s of module"
2540 " '%s', but only R%u%c%s is available",
2541 this->modid
->get_dispname().c_str(),
2542 exp_rel
, eri(exp_patch
), exp_bld_str
,
2543 req_id
->get_dispname().c_str(),
2544 m
->release
, eri(m
->patch
), mod_bld_str
);
2549 single
->error("No imported module named '%s'",
2550 req_id
->get_dispname().c_str());
2552 multi
->delete_element(i
--);
2556 case Ttcn::ExtensionAttribute::REQ_TITAN
: {
2557 char* exp_product_number
;
2558 unsigned int exp_suffix
, exp_minor
, exp_patch
, exp_build
;
2560 (void)ex
.get_id(exp_product_number
, exp_suffix
, exp_minor
, exp_patch
, exp_build
, exp_extra
);
2561 if (exp_product_number
!= NULL
&& strcmp(exp_product_number
,"CRL 113 200") != 0) {
2562 ex
.error("This module needs to be compiled with TITAN, but "
2563 " product number %s is not TITAN"
2564 , exp_product_number
);
2566 if (0 == exp_suffix
) {
2567 exp_suffix
= 1; // previous version number format did not list the suffix part
2569 // TTCN3_MAJOR is always 1
2570 int expected_version
= exp_suffix
* 1000000
2571 + exp_minor
* 10000 + exp_patch
* 100 + exp_build
;
2572 if (expected_version
> TTCN3_VERSION_MONOTONE
) {
2573 char *exp_product_identifier
=
2574 get_product_identifier(exp_product_number
, exp_suffix
, exp_minor
, exp_patch
, exp_build
);
2575 ex
.error("This module needs to be compiled with TITAN version"
2576 " %s or higher; version %s detected"
2577 , exp_product_identifier
, PRODUCT_NUMBER
);
2578 Free(exp_product_identifier
);
2580 multi
->delete_element(i
--);
2583 case Ttcn::ExtensionAttribute::PRINTING
: {
2584 ex
.error("Attribute 'printing' not allowed at module level");
2585 multi
->delete_element(i
--);
2591 // Let everything else propagate into the module.
2592 // Extension attributes in the module's "with" statement
2593 // may be impractical, but not outright erroneous.
2604 if (controlpart
) controlpart
->chk();
2605 if (control_ns
&& !*control_ns
) { // set but empty
2606 error("Invalid URI value for control namespace");
2608 if (control_ns_prefix
&& !*control_ns_prefix
) { // set but empty
2609 error("Empty NCName for the control namespace prefix is not allowed");
2611 // TODO proper URI and NCName validation
2614 void Module::chk_friends()
2616 map
<string
, FriendMod
> friends_m
;
2618 for(size_t i
= 0; i
< friendmods_v
.size(); i
++)
2620 FriendMod
* temp_friend
= friendmods_v
[i
];
2621 const Identifier
& friend_id
= temp_friend
->get_modid();
2622 const string
& friend_name
= friend_id
.get_name();
2623 if(friends_m
.has_key(friend_name
))
2625 temp_friend
->error("Duplicate friend module with name `%s'",
2626 friend_id
.get_dispname().c_str());
2627 friends_m
[friend_name
]->note("Friend module `%s' is already defined here",
2628 friend_id
.get_dispname().c_str());
2630 friends_m
.add(friend_name
, temp_friend
);
2633 friendmods_v
[i
]->chk();
2640 void Module::chk_groups()
2642 map
<string
,Common::Assignment
> ass_m
;
2644 for(size_t i
= 0; i
< asss
->get_nof_asss(); i
++)
2646 Common::Assignment
*temp_ass
= asss
->get_ass_byIndex(i
);
2647 if(!temp_ass
->get_parent_group())
2649 const string
& ass_name
= temp_ass
->get_id().get_name();
2650 if (!ass_m
.has_key(ass_name
)) ass_m
.add(ass_name
, temp_ass
);
2654 for(size_t i
= 0; i
< group_v
.size(); i
++)
2656 const Group
* group
= group_v
[i
];
2657 const Identifier
& group_id
= group
->get_id();
2658 const string
& group_name
= group_id
.get_name();
2659 if(ass_m
.has_key(group_name
))
2661 group
->error("Group name `%s' clashes with a definition",
2662 group_id
.get_dispname().c_str());
2663 ass_m
[group_name
]->note("Definition of `%s' is here",
2664 group_id
.get_dispname().c_str());
2666 if(group_m
.has_key(group_name
))
2668 group
->error("Duplicate group with name `%s'",
2669 group_id
.get_dispname().c_str());
2670 group_m
[group_name
]->note("Group `%s' is already defined here",
2671 group_id
.get_dispname().c_str());
2673 group_m
.add(group_name
,group_v
[i
]);
2679 for(size_t i
= 0; i
< group_v
.size(); i
++)
2685 void Module::get_imported_mods(module_set_t
& p_imported_mods
)
2687 imp
->get_imported_mods(p_imported_mods
);
2690 void Module::generate_code_internal(CodeGenHelper
& cgh
) {
2691 imp
->generate_code(cgh
);
2692 asss
->generate_code(cgh
);
2694 controlpart
->generate_code(cgh
.get_outputstruct(modid
->get_ttcnname()), this);
2697 RunsOnScope
*Module::get_runs_on_scope(Type
*comptype
)
2699 RunsOnScope
*ret_val
= new RunsOnScope(comptype
);
2700 runs_on_scopes
.add(ret_val
);
2701 ret_val
->set_parent_scope(asss
);
2702 ret_val
->chk_uniq();
2707 void Module::dump(unsigned level
) const
2709 DEBUG(level
, "TTCN-3 module: %s", modid
->get_dispname().c_str());
2711 if(imp
) imp
->dump(level
);
2712 if(asss
) asss
->dump(level
);
2714 for(size_t i
= 0; i
< group_v
.size(); i
++)
2716 group_v
[i
]->dump(level
);
2719 if(controlpart
) controlpart
->dump(level
);
2721 if (w_attrib_path
) {
2722 MultiWithAttrib
*attrib
= w_attrib_path
->get_with_attr();
2724 DEBUG(level
, "Module Attributes:");
2725 attrib
->dump(level
+ 1);
2730 void Module::set_language_spec(const char *p_language_spec
)
2732 if (language_spec
) FATAL_ERROR("Module::set_language_spec()");
2733 if (p_language_spec
) language_spec
= new string(p_language_spec
);
2736 void Module::add_ass(Definition
* p_ass
)
2738 asss
->add_ass(p_ass
);
2741 void Module::add_impmod(ImpMod
*p_impmod
)
2743 imp
->add_impmod(p_impmod
);
2746 void Module::add_controlpart(ControlPart
* p_controlpart
)
2748 if (!p_controlpart
|| controlpart
) FATAL_ERROR("Module::add_controlpart()");
2749 controlpart
= p_controlpart
;
2750 controlpart
->set_my_scope(asss
);
2753 void Module::set_with_attr(MultiWithAttrib
* p_attrib
)
2755 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
2756 w_attrib_path
->set_with_attr(p_attrib
);
2759 WithAttribPath
* Module::get_attrib_path()
2761 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
2762 return w_attrib_path
;
2765 void Module::set_parent_path(WithAttribPath
* p_path
)
2767 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
2768 w_attrib_path
->set_parent(p_path
);
2771 bool Module::is_visible(const Identifier
& id
, visibility_t visibility
){
2773 if (visibility
== PUBLIC
) {
2776 if (visibility
== FRIEND
) {
2777 for (size_t i
= 0; i
< friendmods_v
.size(); i
++) {
2778 if (friendmods_v
[i
]->get_modid() == id
) {
2786 void Module::generate_json_schema(JSON_Tokenizer
& json
, map
<Type
*, JSON_Tokenizer
>& json_refs
)
2788 // add a new property for this module
2789 json
.put_next_token(JSON_TOKEN_NAME
, modid
->get_ttcnname().c_str());
2791 // add type definitions into an object
2792 json
.put_next_token(JSON_TOKEN_OBJECT_START
);
2794 // cycle through each type, generate schema segment and reference when needed
2795 for (size_t i
= 0; i
< asss
->get_nof_asss(); ++i
) {
2796 Def_Type
* def
= dynamic_cast<Def_Type
*>(asss
->get_ass_byIndex(i
));
2798 Type
* t
= def
->get_Type();
2799 if (t
->has_encoding(Type::CT_JSON
)) {
2800 // insert type's schema segment
2801 t
->generate_json_schema(json
, false, false);
2803 if (json_refs_for_all_types
&& !json_refs
.has_key(t
)) {
2804 // create JSON schema reference for the type
2805 JSON_Tokenizer
* json_ref
= new JSON_Tokenizer
;
2806 json_refs
.add(t
, json_ref
);
2807 t
->generate_json_schema_ref(*json_ref
);
2813 // end of type definitions
2814 json
.put_next_token(JSON_TOKEN_OBJECT_END
);
2816 // insert function data
2817 for (size_t i
= 0; i
< asss
->get_nof_asss(); ++i
) {
2818 Def_ExtFunction
* def
= dynamic_cast<Def_ExtFunction
*>(asss
->get_ass_byIndex(i
));
2820 def
->generate_json_schema_ref(json_refs
);
2825 void Module::generate_debugger_init(output_struct
* output
)
2827 static boolean first
= TRUE
;
2828 // create the initializer function
2829 output
->source
.global_vars
= mputprintf(output
->source
.global_vars
,
2830 "\n/* Initializing the TTCN-3 debugger */\n"
2831 "void init_ttcn3_debugger()\n"
2833 "%s", first
? " ttcn3_debugger.activate();\n" : "");
2836 // initialize global scope and variables (including imported variables)
2837 char* str_glob
= generate_debugger_global_vars(NULL
, this);
2838 for (int i
= 0; i
< imp
->get_imports_size(); ++i
) {
2839 str_glob
= imp
->get_impmod(i
)->get_mod()->generate_debugger_global_vars(str_glob
, this);
2841 if (str_glob
!= NULL
) {
2842 // only add the global scope if it actually has variables
2843 output
->source
.global_vars
= mputprintf(output
->source
.global_vars
,
2844 " /* global variables */\n"
2845 " TTCN3_Debug_Scope* global_scope = ttcn3_debugger.add_global_scope(\"%s\");\n"
2847 get_modid().get_dispname().c_str(), str_glob
);
2851 // initialize components' scopes and their variables
2852 for (size_t i
= 0; i
< asss
->get_nof_asss(); ++i
) {
2853 Def_Type
* def
= dynamic_cast<Def_Type
*>(asss
->get_ass_byIndex(i
));
2855 Type
* comp_type
= def
->get_Type();
2856 if (comp_type
->get_typetype() == Type::T_COMPONENT
) {
2857 char* str_comp
= NULL
;
2858 ComponentTypeBody
* comp_body
= comp_type
->get_CompBody();
2859 for (size_t j
= 0; j
< comp_body
->get_nof_asss(); ++j
) {
2860 str_comp
= generate_code_debugger_add_var(str_comp
, comp_body
->get_ass_byIndex(j
),
2861 this, comp_type
->get_dispname().c_str());
2863 if (str_comp
!= NULL
) {
2864 // only add the component if it actually has variables
2865 output
->source
.global_vars
= mputprintf(output
->source
.global_vars
,
2866 " /* variables of component %s */\n"
2867 " TTCN3_Debug_Scope* %s_scope = ttcn3_debugger.add_component_scope(\"%s\");\n"
2869 , comp_type
->get_dispname().c_str(), comp_type
->get_dispname().c_str()
2870 , comp_type
->get_dispname().c_str(), str_comp
);
2877 // close the initializer function
2878 output
->source
.global_vars
= mputstr(output
->source
.global_vars
, "}\n");
2881 char* Module::generate_debugger_global_vars(char* str
, Common::Module
* current_mod
)
2883 for (size_t i
= 0; i
< asss
->get_nof_asss(); ++i
) {
2884 Common::Assignment
* ass
= asss
->get_ass_byIndex(i
);
2885 switch (ass
->get_asstype()) {
2886 case Common::Assignment::A_TEMPLATE
:
2887 if (ass
->get_FormalParList() != NULL
) {
2888 // don't add parameterized templates, since they are functions in C++
2891 // else fall through
2892 case Common::Assignment::A_CONST
:
2893 case Common::Assignment::A_MODULEPAR
:
2894 case Common::Assignment::A_MODULEPAR_TEMP
:
2895 str
= generate_code_debugger_add_var(str
, ass
, current_mod
, "global");
2897 case Common::Assignment::A_EXT_CONST
: {
2898 Def_ExtConst
* def
= dynamic_cast<Def_ExtConst
*>(ass
);
2900 FATAL_ERROR("Module::generate_debugger_global_vars");
2902 if (def
->is_used()) {
2903 str
= generate_code_debugger_add_var(str
, ass
, current_mod
, "global");
2913 void Module::generate_debugger_functions(output_struct
*output
)
2916 for (size_t i
= 0; i
< asss
->get_nof_asss(); ++i
) {
2917 Def_Type
* def
= dynamic_cast<Def_Type
*>(asss
->get_ass_byIndex(i
));
2919 Type
* t
= def
->get_Type();
2920 if (!t
->is_ref() && t
->get_typetype() != Type::T_COMPONENT
) {
2921 // don't generate code for subtypes
2922 if (t
->get_typetype() != Type::T_SIGNATURE
) {
2923 str
= mputprintf(str
,
2924 " %sif (!strcmp(p_var.type_name, \"%s\")) {\n"
2925 " ((const %s*)p_var.value)->log();\n"
2927 , (str
!= NULL
) ? "else " : ""
2928 , t
->get_dispname().c_str(), t
->get_genname_value(this).c_str());
2930 if (t
->get_typetype() != Type::T_PORT
) {
2931 str
= mputprintf(str
,
2932 " %sif (!strcmp(p_var.type_name, \"%s template\")) {\n"
2933 " ((const %s_template*)p_var.value)->log();\n"
2935 , (str
!= NULL
) ? "else " : ""
2936 , t
->get_dispname().c_str(), t
->get_genname_value(this).c_str());
2942 // don't generate an empty printing function
2943 output
->header
.class_defs
= mputprintf(output
->header
.class_defs
,
2944 "/* Debugger printing function for types declared in this module */\n\n"
2945 "extern CHARSTRING print_var_%s(const TTCN3_Debugger::variable_t& p_var);\n",
2946 get_modid().get_ttcnname().c_str());
2947 output
->source
.global_vars
= mputprintf(output
->source
.global_vars
,
2948 "\n/* Debugger printing function for types declared in this module */\n"
2949 "CHARSTRING print_var_%s(const TTCN3_Debugger::variable_t& p_var)\n"
2951 " TTCN_Logger::begin_event_log2str();\n"
2954 " TTCN_Logger::log_event_str(\"<unrecognized value or template>\");\n"
2956 " return TTCN_Logger::end_event_log2str();\n"
2957 "}\n", get_modid().get_ttcnname().c_str(), str
);
2961 // =================================
2963 // =================================
2965 string
Definition::get_genname() const
2967 if (!genname
.empty()) return genname
;
2968 else return id
->get_name();
2971 namedbool
Definition::has_implicit_omit_attr() const {
2972 if (w_attrib_path
) {
2973 const vector
<SingleWithAttrib
>& real_attribs
=
2974 w_attrib_path
->get_real_attrib();
2975 for (size_t in
= real_attribs
.size(); in
> 0; in
--) {
2976 if (SingleWithAttrib::AT_OPTIONAL
==
2977 real_attribs
[in
-1]->get_attribKeyword()) {
2978 if ("implicit omit" ==
2979 real_attribs
[in
-1]->get_attribSpec().get_spec()) {
2980 return IMPLICIT_OMIT
;
2981 } else if ("explicit omit" ==
2982 real_attribs
[in
-1]->get_attribSpec().get_spec()) {
2983 return NOT_IMPLICIT_OMIT
;
2984 } // error reporting for other values is in chk_global_attrib
2988 return NOT_IMPLICIT_OMIT
;
2991 Definition::~Definition()
2993 delete w_attrib_path
;
2994 delete erroneous_attrs
;
2997 void Definition::set_fullname(const string
& p_fullname
)
2999 Common::Assignment::set_fullname(p_fullname
);
3000 if (w_attrib_path
) w_attrib_path
->set_fullname(p_fullname
+ ".<attribpath>");
3001 if (erroneous_attrs
) erroneous_attrs
->set_fullname(p_fullname
+".<erroneous_attributes>");
3004 bool Definition::is_local() const
3006 if (!my_scope
) FATAL_ERROR("Definition::is_local()");
3007 for (Scope
*scope
= my_scope
; scope
; scope
= scope
->get_parent_scope()) {
3008 if (dynamic_cast<StatementBlock
*>(scope
)) return true;
3013 bool Definition::chk_identical(Definition
*)
3015 FATAL_ERROR("Definition::chk_identical()");
3019 void Definition::chk_erroneous_attr()
3021 if (!w_attrib_path
) return;
3022 const Ttcn::MultiWithAttrib
* attribs
= w_attrib_path
->get_local_attrib();
3023 if (!attribs
) return;
3024 for (size_t i
= 0; i
< attribs
->get_nof_elements(); i
++) {
3025 const Ttcn::SingleWithAttrib
* act_attr
= attribs
->get_element(i
);
3026 if (act_attr
->get_attribKeyword()==Ttcn::SingleWithAttrib::AT_ERRONEOUS
) {
3027 if (!use_runtime_2
) {
3028 error("`erroneous' attributes can be used only with the Function Test Runtime");
3029 note("If you need negative testing use the -R flag when generating the makefile");
3032 size_t nof_qualifiers
= act_attr
->get_attribQualifiers() ? act_attr
->get_attribQualifiers()->get_nof_qualifiers() : 0;
3033 dynamic_array
<Type
*> refd_type_array(nof_qualifiers
); // only the qualifiers pointing to existing fields will be added to erroneous_attrs objects
3034 if (nof_qualifiers
==0) {
3035 act_attr
->error("At least one qualifier must be specified for the `erroneous' attribute");
3037 // check if qualifiers point to existing fields
3038 for (size_t qi
=0; qi
<nof_qualifiers
; qi
++) {
3039 Qualifier
* act_qual
= const_cast<Qualifier
*>(act_attr
->get_attribQualifiers()->get_qualifier(qi
));
3040 act_qual
->set_my_scope(get_my_scope());
3041 Type
* field_type
= get_Type()->get_field_type(act_qual
, Type::EXPECTED_CONSTANT
);
3043 dynamic_array
<size_t> subrefs_array
;
3044 dynamic_array
<Type
*> type_array
;
3045 bool valid_indexes
= get_Type()->get_subrefs_as_array(act_qual
, subrefs_array
, type_array
);
3046 if (!valid_indexes
) field_type
= NULL
;
3047 if (act_qual
->refers_to_string_element()) {
3048 act_qual
->error("Reference to a string element cannot be used in this context");
3052 refd_type_array
.add(field_type
);
3055 // parse the attr. spec.
3056 ErroneousAttributeSpec
* err_attr_spec
= ttcn3_parse_erroneous_attr_spec_string(
3057 act_attr
->get_attribSpec().get_spec().c_str(), act_attr
->get_attribSpec());
3058 if (err_attr_spec
) {
3059 if (!erroneous_attrs
) erroneous_attrs
= new ErroneousAttributes(get_Type());
3060 // attr.spec will be owned by erroneous_attrs object
3061 erroneous_attrs
->add_spec(err_attr_spec
);
3062 err_attr_spec
->set_fullname(get_fullname());
3063 err_attr_spec
->set_my_scope(get_my_scope());
3064 err_attr_spec
->chk();
3065 // create qualifier - err.attr.spec. pairs
3066 for (size_t qi
=0; qi
<nof_qualifiers
; qi
++) {
3067 if (refd_type_array
[qi
] && (err_attr_spec
->get_indicator()!=ErroneousAttributeSpec::I_INVALID
)) {
3068 erroneous_attrs
->add_pair(act_attr
->get_attribQualifiers()->get_qualifier(qi
), err_attr_spec
);
3074 if (erroneous_attrs
) erroneous_attrs
->chk();
3077 char* Definition::generate_code_str(char *str
)
3079 FATAL_ERROR("Definition::generate_code_str()");
3083 void Definition::ilt_generate_code(ILT
*)
3085 FATAL_ERROR("Definition::ilt_generate_code()");
3088 char *Definition::generate_code_init_comp(char *str
, Definition
*)
3090 FATAL_ERROR("Definition::generate_code_init_comp()");
3094 void Definition::set_with_attr(MultiWithAttrib
* p_attrib
)
3096 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
3097 w_attrib_path
->set_with_attr(p_attrib
);
3100 WithAttribPath
* Definition::get_attrib_path()
3102 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
3103 return w_attrib_path
;
3106 void Definition::set_parent_path(WithAttribPath
* p_path
)
3108 if (!w_attrib_path
) w_attrib_path
= new WithAttribPath();
3109 w_attrib_path
->set_parent(p_path
);
3112 void Definition::set_parent_group(Group
* p_group
)
3114 if(parentgroup
) // there would be a leak!
3115 FATAL_ERROR("Definition::set_parent_group()");
3116 parentgroup
= p_group
;
3119 Group
* Definition::get_parent_group()
3124 void Definition::dump_internal(unsigned level
) const
3126 DEBUG(level
, "Move along, nothing to see here");
3129 void Definition::dump(unsigned level
) const
3131 dump_internal(level
);
3132 if (w_attrib_path
) {
3133 MultiWithAttrib
*attrib
= w_attrib_path
->get_with_attr();
3135 DEBUG(level
+ 1, "Definition Attributes:");
3136 attrib
->dump(level
+ 2);
3139 if (erroneous_attrs
) erroneous_attrs
->dump(level
+1);
3142 // =================================
3144 // =================================
3146 Def_Type::Def_Type(Identifier
*p_id
, Type
*p_type
)
3147 : Definition(A_TYPE
, p_id
), type(p_type
)
3149 if(!p_type
) FATAL_ERROR("Ttcn::Def_Type::Def_Type()");
3150 type
->set_ownertype(Type::OT_TYPE_DEF
, this);
3153 Def_Type::~Def_Type()
3158 Def_Type
*Def_Type::clone() const
3160 FATAL_ERROR("Def_Type::clone");
3163 void Def_Type::set_fullname(const string
& p_fullname
)
3165 Definition::set_fullname(p_fullname
);
3166 type
->set_fullname(p_fullname
);
3169 void Def_Type::set_my_scope(Scope
*p_scope
)
3171 bridgeScope
.set_parent_scope(p_scope
);
3172 bridgeScope
.set_scopeMacro_name(id
->get_dispname());
3174 Definition::set_my_scope(&bridgeScope
);
3175 type
->set_my_scope(&bridgeScope
);
3179 Setting
*Def_Type::get_Setting()
3184 Type
*Def_Type::get_Type()
3190 void Def_Type::chk()
3192 if (checked
) return;
3194 Error_Context
cntxt(this, "In %s definition `%s'",
3195 type
->get_typetype() == Type::T_SIGNATURE
? "signature" : "type",
3196 id
->get_dispname().c_str());
3197 type
->set_genname(get_genname());
3198 if (!semantic_check_only
&& type
->get_typetype() == Type::T_COMPONENT
) {
3199 // the prefix of embedded definitions must be set before the checking
3200 type
->get_CompBody()->set_genname(get_genname() + "_component_");
3203 while (w_attrib_path
) { // not a loop, but we can _break_ out of it
3204 w_attrib_path
->chk_global_attrib();
3205 w_attrib_path
->chk_no_qualif();
3206 if (type
->get_typetype() != Type::T_ANYTYPE
) break;
3207 // This is the anytype; it must be empty (we're about to add the fields)
3208 if (type
->get_nof_comps() > 0) FATAL_ERROR("Def_Type::chk");
3210 Ttcn::ExtensionAttributes
*extattrs
= parse_extattributes(w_attrib_path
);
3211 if (extattrs
== 0) break; // NULL means parsing error
3213 size_t num_atrs
= extattrs
->size();
3214 for (size_t k
= 0; k
< num_atrs
; ++k
) {
3215 ExtensionAttribute
&ea
= extattrs
->get(k
);
3216 switch (ea
.get_type()) {
3217 case ExtensionAttribute::ANYTYPELIST
: {
3218 Types
*anytypes
= ea
.get_types();
3219 // List of types to be converted into fields for the anytype.
3220 // Make sure scope is set on all types in the list.
3221 anytypes
->set_my_scope(get_my_scope());
3223 // Convert the list of types into field names for the anytype
3224 for (size_t i
=0; i
< anytypes
->get_nof_types(); ++i
) {
3225 Type
*t
= anytypes
->extract_type_byIndex(i
);
3226 // we are now the owner of the Type.
3227 if (t
->get_typetype()==Type::T_ERROR
) { // should we give up?
3233 const char* btn
= Type::get_typename_builtin(t
->get_typetype());
3237 else if (t
->get_typetype() == Type::T_REFD
) {
3238 // Extract the identifier
3239 Common::Reference
*ref
= t
->get_Reference();
3240 Ttcn::Reference
*tref
= dynamic_cast<Ttcn::Reference
*>(ref
);
3241 if (!tref
) FATAL_ERROR("Def_Type::chk, wrong kind of reference");
3242 const Common::Identifier
*modid
= tref
->get_modid();
3244 ea
.error("Qualified name '%s' cannot be added to the anytype",
3245 tref
->get_dispname().c_str());
3249 field_name
= tref
->get_id()->get_ttcnname();
3252 // Can't happen here
3253 FATAL_ERROR("Unexpected type %d", t
->get_typetype());
3256 const string
& at_field
= anytype_field(field_name
);
3257 Identifier
*field_id
= new Identifier(Identifier::ID_TTCN
, at_field
);
3258 CompField
*cf
= new CompField(field_id
, t
, false, 0);
3259 cf
->set_location(ea
);
3260 cf
->set_fullname(get_fullname());
3266 w_attrib_path
->get_with_attr()->error("Type def can only have anytype");
3272 break; // do not loop
3275 // Now we can check the type
3277 type
->chk_constructor_name(*id
);
3278 if (id
->get_ttcnname() == "address") type
->chk_address();
3279 ReferenceChain
refch(type
, "While checking embedded recursions");
3280 type
->chk_recursions(refch
);
3282 if (type
->get_typetype()==Type::T_FUNCTION
3283 ||type
->get_typetype()==Type::T_ALTSTEP
3284 ||type
->get_typetype()==Type::T_TESTCASE
) {
3285 // TR 922. This is a function/altstep/testcase reference.
3286 // Set this definition as the definition for the formal parameters.
3287 type
->get_fat_parameters()->set_my_def(this);
3291 void Def_Type::generate_code(output_struct
*target
, bool)
3293 type
->generate_code(target
);
3294 if (type
->get_typetype() == Type::T_COMPONENT
) {
3295 // the C++ equivalents of embedded component element definitions must be
3296 // generated from outside Type::generate_code() because the function can
3297 // call itself recursively and create invalid (overlapped) initializer
3299 type
->get_CompBody()->generate_code(target
);
3303 void Def_Type::generate_code(CodeGenHelper
& cgh
) {
3304 type
->generate_code(cgh
.get_outputstruct(get_Type()));
3305 if (type
->get_typetype() == Type::T_COMPONENT
) {
3306 // the C++ equivalents of embedded component element definitions must be
3307 // generated from outside Type::generate_code() because the function can
3308 // call itself recursively and create invalid (overlapped) initializer
3310 type
->get_CompBody()->generate_code(cgh
.get_current_outputstruct());
3312 cgh
.finalize_generation(get_Type());
3316 void Def_Type::dump_internal(unsigned level
) const
3318 DEBUG(level
, "Type def: %s @ %p", id
->get_dispname().c_str(), (const void*)this);
3319 type
->dump(level
+ 1);
3322 void Def_Type::set_with_attr(MultiWithAttrib
* p_attrib
)
3324 if (!w_attrib_path
) {
3325 w_attrib_path
= new WithAttribPath();
3326 type
->set_parent_path(w_attrib_path
);
3328 type
->set_with_attr(p_attrib
);
3331 WithAttribPath
* Def_Type::get_attrib_path()
3333 if (!w_attrib_path
) {
3334 w_attrib_path
= new WithAttribPath();
3335 type
->set_parent_path(w_attrib_path
);
3337 return w_attrib_path
;
3340 void Def_Type::set_parent_path(WithAttribPath
* p_path
)
3342 if (!w_attrib_path
) {
3343 w_attrib_path
= new WithAttribPath();
3344 type
->set_parent_path(w_attrib_path
);
3346 w_attrib_path
->set_parent(p_path
);
3349 // =================================
3351 // =================================
3353 Def_Const::Def_Const(Identifier
*p_id
, Type
*p_type
, Value
*p_value
)
3354 : Definition(A_CONST
, p_id
)
3356 if (!p_type
|| !p_value
) FATAL_ERROR("Ttcn::Def_Const::Def_Const()");
3358 type
->set_ownertype(Type::OT_CONST_DEF
, this);
3360 value_under_check
=false;
3363 Def_Const::~Def_Const()
3369 Def_Const
*Def_Const::clone() const
3371 FATAL_ERROR("Def_Const::clone");
3374 void Def_Const::set_fullname(const string
& p_fullname
)
3376 Definition::set_fullname(p_fullname
);
3377 type
->set_fullname(p_fullname
+ ".<type>");
3378 value
->set_fullname(p_fullname
);
3381 void Def_Const::set_my_scope(Scope
*p_scope
)
3383 Definition::set_my_scope(p_scope
);
3384 type
->set_my_scope(p_scope
);
3385 value
->set_my_scope(p_scope
);
3388 Setting
*Def_Const::get_Setting()
3393 Type
*Def_Const::get_Type()
3400 Value
*Def_Const::get_Value()
3406 void Def_Const::chk()
3409 if (value_under_check
) {
3410 error("Circular reference in constant definition `%s'",
3411 id
->get_dispname().c_str());
3412 value_under_check
= false; // only report the error once for this definition
3416 Error_Context
cntxt(this, "In constant definition `%s'",
3417 id
->get_dispname().c_str());
3418 type
->set_genname(_T_
, get_genname());
3420 value
->set_my_governor(type
);
3421 type
->chk_this_value_ref(value
);
3423 if (w_attrib_path
) {
3424 w_attrib_path
->chk_global_attrib(true);
3425 switch (type
->get_type_refd_last()->get_typetype_ttcn3()) {
3428 case Type::T_CHOICE_T
:
3429 // These types may have qualified attributes
3431 case Type::T_SEQOF
: case Type::T_SETOF
:
3434 w_attrib_path
->chk_no_qualif();
3438 Type
*t
= type
->get_type_refd_last();
3439 switch (t
->get_typetype()) {
3441 error("Constant cannot be defined for port type `%s'",
3442 t
->get_fullname().c_str());
3444 case Type::T_SIGNATURE
:
3445 error("Constant cannot be defined for signature `%s'",
3446 t
->get_fullname().c_str());
3449 value_under_check
= true;
3450 type
->chk_this_value(value
, 0, Type::EXPECTED_CONSTANT
, INCOMPLETE_ALLOWED
,
3451 OMIT_NOT_ALLOWED
, SUB_CHK
, has_implicit_omit_attr());
3452 value_under_check
= false;
3453 chk_erroneous_attr();
3454 if (erroneous_attrs
) value
->set_err_descr(erroneous_attrs
->get_err_descr());
3456 ReferenceChain
refch(type
, "While checking embedded recursions");
3457 value
->chk_recursions(refch
);
3461 if (!semantic_check_only
) {
3462 value
->set_genname_prefix("const_");
3463 value
->set_genname_recursive(get_genname());
3464 value
->set_code_section(GovernedSimple::CS_PRE_INIT
);
3468 bool Def_Const::chk_identical(Definition
*p_def
)
3472 if (p_def
->get_asstype() != A_CONST
) {
3473 const char *dispname_str
= id
->get_dispname().c_str();
3474 error("Local definition `%s' is a constant, but the definition "
3475 "inherited from component type `%s' is a %s", dispname_str
,
3476 p_def
->get_my_scope()->get_fullname().c_str(), p_def
->get_assname());
3477 p_def
->note("The inherited definition of `%s' is here", dispname_str
);
3480 Def_Const
*p_def_const
= dynamic_cast<Def_Const
*>(p_def
);
3481 if (!p_def_const
) FATAL_ERROR("Def_Const::chk_identical()");
3482 if (!type
->is_identical(p_def_const
->type
)) {
3483 const char *dispname_str
= id
->get_dispname().c_str();
3484 type
->error("Local constant `%s' has type `%s', but the constant "
3485 "inherited from component type `%s' has type `%s'", dispname_str
,
3486 type
->get_typename().c_str(),
3487 p_def_const
->get_my_scope()->get_fullname().c_str(),
3488 p_def_const
->type
->get_typename().c_str());
3489 p_def_const
->note("The inherited constant `%s' is here", dispname_str
);
3491 } else if (!(*value
== *p_def_const
->value
)) {
3492 const char *dispname_str
= id
->get_dispname().c_str();
3493 value
->error("Local constant `%s' and the constant inherited from "
3494 "component type `%s' have different values", dispname_str
,
3495 p_def_const
->get_my_scope()->get_fullname().c_str());
3496 p_def_const
->note("The inherited constant `%s' is here", dispname_str
);
3501 void Def_Const::generate_code(output_struct
*target
, bool)
3503 type
->generate_code(target
);
3505 Code::init_cdef(&cdef
);
3506 type
->generate_code_object(&cdef
, value
);
3507 cdef
.init
= update_location_object(cdef
.init
);
3508 cdef
.init
= value
->generate_code_init(cdef
.init
,
3509 value
->get_lhs_name().c_str());
3510 Code::merge_cdef(target
, &cdef
);
3511 Code::free_cdef(&cdef
);
3514 void Def_Const::generate_code(Common::CodeGenHelper
& cgh
) {
3515 // constant definitions always go to its containing module
3516 generate_code(cgh
.get_current_outputstruct());
3519 char *Def_Const::generate_code_str(char *str
)
3521 const string
& t_genname
= get_genname();
3522 const char *genname_str
= t_genname
.c_str();
3523 if (value
->has_single_expr()) {
3524 // the value can be represented by a single C++ expression
3525 // the object is initialized by the constructor
3526 str
= mputprintf(str
, "%s %s(%s);\n",
3527 type
->get_genname_value(my_scope
).c_str(), genname_str
,
3528 value
->get_single_expr().c_str());
3530 // use the default constructor
3531 str
= mputprintf(str
, "%s %s;\n",
3532 type
->get_genname_value(my_scope
).c_str(), genname_str
);
3533 // the value is assigned using subsequent statements
3534 str
= value
->generate_code_init(str
, genname_str
);
3536 if (debugger_active
) {
3537 str
= generate_code_debugger_add_var(str
, this);
3542 void Def_Const::ilt_generate_code(ILT
*ilt
)
3544 const string
& t_genname
= get_genname();
3545 const char *genname_str
= t_genname
.c_str();
3546 char*& def
=ilt
->get_out_def();
3547 char*& init
=ilt
->get_out_branches();
3548 def
= mputprintf(def
, "%s %s;\n", type
->get_genname_value(my_scope
).c_str(),
3550 init
= value
->generate_code_init(init
, genname_str
);
3553 char *Def_Const::generate_code_init_comp(char *str
, Definition
*)
3555 /* This function actually does nothing as \a this and \a base_defn are
3556 * exactly the same. */
3560 void Def_Const::dump_internal(unsigned level
) const
3562 DEBUG(level
, "Constant: %s @%p", id
->get_dispname().c_str(), (const void*)this);
3563 type
->dump(level
+ 1);
3564 value
->dump(level
+ 1);
3567 // =================================
3568 // ===== Def_ExtConst
3569 // =================================
3571 Def_ExtConst::Def_ExtConst(Identifier
*p_id
, Type
*p_type
)
3572 : Definition(A_EXT_CONST
, p_id
)
3574 if (!p_type
) FATAL_ERROR("Ttcn::Def_ExtConst::Def_ExtConst()");
3576 type
->set_ownertype(Type::OT_CONST_DEF
, this);
3577 usage_found
= false;
3580 Def_ExtConst::~Def_ExtConst()
3585 Def_ExtConst
*Def_ExtConst::clone() const
3587 FATAL_ERROR("Def_ExtConst::clone");
3590 void Def_ExtConst::set_fullname(const string
& p_fullname
)
3592 Definition::set_fullname(p_fullname
);
3593 type
->set_fullname(p_fullname
+ ".<type>");
3596 void Def_ExtConst::set_my_scope(Scope
*p_scope
)
3598 Definition::set_my_scope(p_scope
);
3599 type
->set_my_scope(p_scope
);
3602 Type
*Def_ExtConst::get_Type()
3608 void Def_ExtConst::chk()
3611 Error_Context
cntxt(this, "In external constant definition `%s'",
3612 id
->get_dispname().c_str());
3613 type
->set_genname(_T_
, get_genname());
3616 Type
*t
= type
->get_type_refd_last();
3617 switch (t
->get_typetype()) {
3619 error("External constant cannot be defined for port type `%s'",
3620 t
->get_fullname().c_str());
3622 case Type::T_SIGNATURE
:
3623 error("External constant cannot be defined for signature `%s'",
3624 t
->get_fullname().c_str());
3629 if (w_attrib_path
) {
3630 w_attrib_path
->chk_global_attrib();
3631 switch (type
->get_type_refd_last()->get_typetype()) {
3634 case Type::T_CHOICE_T
:
3635 // These types may have qualified attributes
3637 case Type::T_SEQOF
: case Type::T_SETOF
:
3640 w_attrib_path
->chk_no_qualif();
3646 void Def_ExtConst::generate_code(output_struct
*target
, bool)
3648 type
->generate_code(target
);
3649 target
->header
.global_vars
= mputprintf(target
->header
.global_vars
,
3650 "extern const %s& %s;\n", type
->get_genname_value(my_scope
).c_str(),
3651 get_genname().c_str());
3654 void Def_ExtConst::generate_code(Common::CodeGenHelper
& cgh
) {
3655 // constant definitions always go to its containing module
3656 generate_code(cgh
.get_current_outputstruct());
3659 void Def_ExtConst::dump_internal(unsigned level
) const
3661 DEBUG(level
, "External constant: %s @ %p", id
->get_dispname().c_str(), (const void*)this);
3662 type
->dump(level
+ 1);
3665 // =================================
3666 // ===== Def_Modulepar
3667 // =================================
3669 Def_Modulepar::Def_Modulepar(Identifier
*p_id
, Type
*p_type
, Value
*p_defval
)
3670 : Definition(A_MODULEPAR
, p_id
)
3672 if (!p_type
) FATAL_ERROR("Ttcn::Def_Modulepar::Def_Modulepar()");
3674 type
->set_ownertype(Type::OT_MODPAR_DEF
, this);
3675 def_value
= p_defval
;
3678 Def_Modulepar::~Def_Modulepar()
3684 Def_Modulepar
* Def_Modulepar::clone() const
3686 FATAL_ERROR("Def_Modulepar::clone");
3689 void Def_Modulepar::set_fullname(const string
& p_fullname
)
3691 Definition::set_fullname(p_fullname
);
3692 type
->set_fullname(p_fullname
+ ".<type>");
3693 if (def_value
) def_value
->set_fullname(p_fullname
+ ".<default_value>");
3696 void Def_Modulepar::set_my_scope(Scope
*p_scope
)
3698 Definition::set_my_scope(p_scope
);
3699 type
->set_my_scope(p_scope
);
3700 if (def_value
) def_value
->set_my_scope(p_scope
);
3703 Type
*Def_Modulepar::get_Type()
3709 void Def_Modulepar::chk()
3712 Error_Context
cntxt(this, "In module parameter definition `%s'",
3713 id
->get_dispname().c_str());
3714 type
->set_genname(_T_
, get_genname());
3716 if (w_attrib_path
) {
3717 w_attrib_path
->chk_global_attrib();
3718 switch (type
->get_type_refd_last()->get_typetype()) {
3721 case Type::T_CHOICE_T
:
3722 // These types may have qualified attributes
3724 case Type::T_SEQOF
: case Type::T_SETOF
:
3727 w_attrib_path
->chk_no_qualif();
3731 map
<Type
*,void> type_chain
;
3732 map
<Type::typetype_t
, void> not_allowed
;
3733 not_allowed
.add(Type::T_PORT
, 0);
3734 Type
*t
= type
->get_type_refd_last();
3735 // if the type is valid the original will be returned
3736 Type::typetype_t tt
= t
->search_for_not_allowed_type(type_chain
, not_allowed
);
3738 not_allowed
.clear();
3741 error("Type of module parameter cannot be or embed port type `%s'",
3742 t
->get_fullname().c_str());
3744 case Type::T_SIGNATURE
:
3745 error("Type of module parameter cannot be signature `%s'",
3746 t
->get_fullname().c_str());
3748 case Type::T_FUNCTION
:
3749 case Type::T_ALTSTEP
:
3750 case Type::T_TESTCASE
:
3751 if (t
->get_fat_runs_on_self()) {
3752 error("Type of module parameter cannot be of function reference type"
3753 " `%s' which has runs on self clause", t
->get_fullname().c_str());
3761 Error_Context
cntxt2(def_value
, "In default value");
3762 def_value
->set_my_governor(type
);
3763 type
->chk_this_value_ref(def_value
);
3765 type
->chk_this_value(def_value
, 0, Type::EXPECTED_CONSTANT
, INCOMPLETE_ALLOWED
,
3766 OMIT_NOT_ALLOWED
, SUB_CHK
, has_implicit_omit_attr());
3767 if (!semantic_check_only
) {
3768 def_value
->set_genname_prefix("modulepar_");
3769 def_value
->set_genname_recursive(get_genname());
3770 def_value
->set_code_section(GovernedSimple::CS_PRE_INIT
);
3772 } else checked
= true;
3778 void Def_Modulepar::generate_code(output_struct
*target
, bool)
3780 type
->generate_code(target
);
3782 Code::init_cdef(&cdef
);
3783 const string
& t_genname
= get_genname();
3784 const char *name
= t_genname
.c_str();
3785 type
->generate_code_object(&cdef
, my_scope
, t_genname
, "modulepar_", false);
3787 cdef
.init
= update_location_object(cdef
.init
);
3788 cdef
.init
= def_value
->generate_code_init(cdef
.init
, def_value
->get_lhs_name().c_str());
3790 Code::merge_cdef(target
, &cdef
);
3791 Code::free_cdef(&cdef
);
3793 if (has_implicit_omit_attr()) {
3794 target
->functions
.post_init
= mputprintf(target
->functions
.post_init
,
3795 "modulepar_%s.set_implicit_omit();\n", name
);
3798 const char *dispname
= id
->get_dispname().c_str();
3799 target
->functions
.set_param
= mputprintf(target
->functions
.set_param
,
3800 "if (!strcmp(par_name, \"%s\")) {\n"
3801 "modulepar_%s.set_param(param);\n"
3803 "} else ", dispname
, name
);
3804 target
->functions
.get_param
= mputprintf(target
->functions
.get_param
,
3805 "if (!strcmp(par_name, \"%s\")) {\n"
3806 "return modulepar_%s.get_param(param_name);\n"
3807 "} else ", dispname
, name
);
3809 if (target
->functions
.log_param
) {
3810 // this is not the first modulepar
3811 target
->functions
.log_param
= mputprintf(target
->functions
.log_param
,
3812 "TTCN_Logger::log_event_str(\", %s := \");\n", dispname
);
3814 // this is the first modulepar
3815 target
->functions
.log_param
= mputprintf(target
->functions
.log_param
,
3816 "TTCN_Logger::log_event_str(\"%s := \");\n", dispname
);
3818 target
->functions
.log_param
= mputprintf(target
->functions
.log_param
,
3819 "%s.log();\n", name
);
3822 void Def_Modulepar::generate_code(Common::CodeGenHelper
& cgh
) {
3823 // module parameter definitions always go to its containing module
3824 generate_code(cgh
.get_current_outputstruct());
3827 void Def_Modulepar::dump_internal(unsigned level
) const
3829 DEBUG(level
, "Module parameter: %s @ %p", id
->get_dispname().c_str(), (const void*)this);
3830 type
->dump(level
+ 1);
3831 if (def_value
) def_value
->dump(level
+ 1);
3832 else DEBUG(level
+ 1, "No default value");
3835 // =================================
3836 // ===== Def_Modulepar_Template
3837 // =================================
3839 Def_Modulepar_Template::Def_Modulepar_Template(Identifier
*p_id
, Type
*p_type
, Template
*p_deftmpl
)
3840 : Definition(A_MODULEPAR_TEMP
, p_id
)
3842 if (!p_type
) FATAL_ERROR("Ttcn::Def_Modulepar_Template::Def_Modulepar_Template()");
3844 type
->set_ownertype(Type::OT_MODPAR_DEF
, this);
3845 def_template
= p_deftmpl
;
3848 Def_Modulepar_Template::~Def_Modulepar_Template()
3851 delete def_template
;
3854 Def_Modulepar_Template
* Def_Modulepar_Template::clone() const
3856 FATAL_ERROR("Def_Modulepar_Template::clone");
3859 void Def_Modulepar_Template::set_fullname(const string
& p_fullname
)
3861 Definition::set_fullname(p_fullname
);
3862 type
->set_fullname(p_fullname
+ ".<type>");
3863 if (def_template
) def_template
->set_fullname(p_fullname
+ ".<default_template>");
3866 void Def_Modulepar_Template::set_my_scope(Scope
*p_scope
)
3868 Definition::set_my_scope(p_scope
);
3869 type
->set_my_scope(p_scope
);
3870 if (def_template
) def_template
->set_my_scope(p_scope
);
3873 Type
*Def_Modulepar_Template::get_Type()
3879 void Def_Modulepar_Template::chk()
3882 Error_Context
cntxt(this, "In template module parameter definition `%s'",
3883 id
->get_dispname().c_str());
3884 if (w_attrib_path
) {
3885 w_attrib_path
->chk_global_attrib();
3886 switch (type
->get_type_refd_last()->get_typetype()) {
3889 case Type::T_CHOICE_T
:
3890 // These types may have qualified attributes
3892 case Type::T_SEQOF
: case Type::T_SETOF
:
3895 w_attrib_path
->chk_no_qualif();
3899 type
->set_genname(_T_
, get_genname());
3901 Type
*t
= type
->get_type_refd_last();
3902 switch (t
->get_typetype()) {
3904 error("Type of template module parameter cannot be port type `%s'",
3905 t
->get_fullname().c_str());
3907 case Type::T_SIGNATURE
:
3908 error("Type of template module parameter cannot be signature `%s'",
3909 t
->get_fullname().c_str());
3911 case Type::T_FUNCTION
:
3912 case Type::T_ALTSTEP
:
3913 case Type::T_TESTCASE
:
3914 if (t
->get_fat_runs_on_self()) {
3915 error("Type of template module parameter cannot be of function reference type"
3916 " `%s' which has runs on self clause", t
->get_fullname().c_str());
3920 if (has_implicit_omit_attr()) {
3921 error("Implicit omit not supported for template module parameters");
3927 Error_Context
cntxt2(def_template
, "In default template");
3928 def_template
->set_my_governor(type
);
3929 def_template
->flatten(false);
3930 if (def_template
->get_templatetype() == Template::CSTR_PATTERN
&&
3931 type
->get_type_refd_last()->get_typetype() == Type::T_USTR
) {
3932 def_template
->set_templatetype(Template::USTR_PATTERN
);
3933 def_template
->get_ustr_pattern()->set_pattern_type(
3934 PatternString::USTR_PATTERN
);
3936 type
->chk_this_template_ref(def_template
);
3938 type
->chk_this_template_generic(def_template
, INCOMPLETE_ALLOWED
,
3939 OMIT_ALLOWED
, ANY_OR_OMIT_ALLOWED
, SUB_CHK
, has_implicit_omit_attr() ? IMPLICIT_OMIT
: NOT_IMPLICIT_OMIT
, 0);
3940 if (!semantic_check_only
) {
3941 def_template
->set_genname_prefix("modulepar_");
3942 def_template
->set_genname_recursive(get_genname());
3943 def_template
->set_code_section(GovernedSimple::CS_PRE_INIT
);
3945 } else checked
= true;
3951 void Def_Modulepar_Template::generate_code(output_struct
*target
, bool)
3953 type
->generate_code(target
);
3955 Code::init_cdef(&cdef
);
3956 const string
& t_genname
= get_genname();
3957 const char *name
= t_genname
.c_str();
3958 type
->generate_code_object(&cdef
, my_scope
, t_genname
, "modulepar_", true);
3960 cdef
.init
= update_location_object(cdef
.init
);
3961 cdef
.init
= def_template
->generate_code_init(cdef
.init
, def_template
->get_lhs_name().c_str());
3963 Code::merge_cdef(target
, &cdef
);
3964 Code::free_cdef(&cdef
);
3966 if (has_implicit_omit_attr()) {
3967 FATAL_ERROR("Def_Modulepar_Template::generate_code()");
3970 const char *dispname
= id
->get_dispname().c_str();
3971 target
->functions
.set_param
= mputprintf(target
->functions
.set_param
,
3972 "if (!strcmp(par_name, \"%s\")) {\n"
3973 "modulepar_%s.set_param(param);\n"
3975 "} else ", dispname
, name
);
3976 target
->functions
.get_param
= mputprintf(target
->functions
.get_param
,
3977 "if (!strcmp(par_name, \"%s\")) {\n"
3978 "return modulepar_%s.get_param(param_name);\n"
3979 "} else ", dispname
, name
);
3981 if (target
->functions
.log_param
) {
3982 // this is not the first modulepar
3983 target
->functions
.log_param
= mputprintf(target
->functions
.log_param
,
3984 "TTCN_Logger::log_event_str(\", %s := \");\n", dispname
);
3986 // this is the first modulepar
3987 target
->functions
.log_param
= mputprintf(target
->functions
.log_param
,
3988 "TTCN_Logger::log_event_str(\"%s := \");\n", dispname
);
3990 target
->functions
.log_param
= mputprintf(target
->functions
.log_param
,
3991 "%s.log();\n", name
);
3994 void Def_Modulepar_Template::generate_code(Common::CodeGenHelper
& cgh
) {
3995 // module parameter definitions always go to its containing module
3996 generate_code(cgh
.get_current_outputstruct());
3999 void Def_Modulepar_Template::dump_internal(unsigned level
) const
4001 DEBUG(level
, "Module parameter: %s @ %p", id
->get_dispname().c_str(), (const void*)this);
4002 type
->dump(level
+ 1);
4003 if (def_template
) def_template
->dump(level
+ 1);
4004 else DEBUG(level
+ 1, "No default template");
4007 // =================================
4008 // ===== Def_Template
4009 // =================================
4011 Def_Template::Def_Template(template_restriction_t p_template_restriction
,
4012 Identifier
*p_id
, Type
*p_type
, FormalParList
*p_fpl
,
4013 Reference
*p_derived_ref
, Template
*p_body
)
4014 : Definition(A_TEMPLATE
, p_id
), type(p_type
), fp_list(p_fpl
),
4015 derived_ref(p_derived_ref
), base_template(0), recurs_deriv_checked(false),
4016 body(p_body
), template_restriction(p_template_restriction
),
4017 gen_restriction_check(false)
4019 if (!p_type
|| !p_body
) FATAL_ERROR("Ttcn::Def_Template::Def_Template()");
4020 type
->set_ownertype(Type::OT_TEMPLATE_DEF
, this);
4021 if (fp_list
) fp_list
->set_my_def(this);
4024 Def_Template::~Def_Template()
4032 Def_Template
*Def_Template::clone() const
4034 FATAL_ERROR("Def_Template::clone");
4037 void Def_Template::set_fullname(const string
& p_fullname
)
4039 Definition::set_fullname(p_fullname
);
4040 type
->set_fullname(p_fullname
+ ".<type>");
4041 if (fp_list
) fp_list
->set_fullname(p_fullname
+ ".<formal_par_list>");
4043 derived_ref
->set_fullname(p_fullname
+ ".<derived_reference>");
4044 body
->set_fullname(p_fullname
);
4047 void Def_Template::set_my_scope(Scope
*p_scope
)
4049 bridgeScope
.set_parent_scope(p_scope
);
4050 bridgeScope
.set_scopeMacro_name(id
->get_dispname());
4052 Definition::set_my_scope(&bridgeScope
);
4053 type
->set_my_scope(&bridgeScope
);
4054 if (derived_ref
) derived_ref
->set_my_scope(&bridgeScope
);
4056 fp_list
->set_my_scope(&bridgeScope
);
4057 body
->set_my_scope(fp_list
);
4058 } else body
->set_my_scope(&bridgeScope
);
4061 Setting
*Def_Template::get_Setting()
4063 return get_Template();
4066 Type
*Def_Template::get_Type()
4068 if (!checked
) chk();
4072 Template
*Def_Template::get_Template()
4074 if (!checked
) chk();
4078 FormalParList
*Def_Template::get_FormalParList()
4080 if (!checked
) chk();
4084 void Def_Template::chk()
4086 if (checked
) return;
4087 Error_Context
cntxt(this, "In template definition `%s'",
4088 id
->get_dispname().c_str());
4089 const string
& t_genname
= get_genname();
4090 type
->set_genname(_T_
, t_genname
);
4092 if (w_attrib_path
) {
4093 w_attrib_path
->chk_global_attrib(true);
4094 switch (type
->get_type_refd_last()->get_typetype_ttcn3()) {
4097 case Type::T_CHOICE_T
:
4098 // These types may have qualified attributes
4100 case Type::T_SEQOF
: case Type::T_SETOF
:
4103 w_attrib_path
->chk_no_qualif();
4109 fp_list
->chk(asstype
);
4110 if (local_scope
) error("Parameterized local template `%s' not supported",
4111 id
->get_dispname().c_str());
4114 // Merge the elements of "all from" into the list
4115 body
->flatten(false);
4117 body
->set_my_governor(type
);
4119 if (body
->get_templatetype() == Template::CSTR_PATTERN
&&
4120 type
->get_type_refd_last()->get_typetype() == Type::T_USTR
) {
4121 body
->set_templatetype(Template::USTR_PATTERN
);
4122 body
->get_ustr_pattern()->set_pattern_type(PatternString::USTR_PATTERN
);
4125 type
->chk_this_template_ref(body
);
4127 Type
*t
= type
->get_type_refd_last();
4128 if (t
->get_typetype() == Type::T_PORT
) {
4129 error("Template cannot be defined for port type `%s'",
4130 t
->get_fullname().c_str());
4133 chk_recursive_derivation();
4134 type
->chk_this_template_generic(body
, INCOMPLETE_ALLOWED
, OMIT_ALLOWED
,
4135 ANY_OR_OMIT_ALLOWED
, SUB_CHK
,
4136 has_implicit_omit_attr() ? IMPLICIT_OMIT
: NOT_IMPLICIT_OMIT
, 0);
4138 chk_erroneous_attr();
4139 if (erroneous_attrs
) body
->set_err_descr(erroneous_attrs
->get_err_descr());
4142 ReferenceChain
refch(type
, "While checking embedded recursions");
4143 body
->chk_recursions(refch
);
4145 if (template_restriction
!=TR_NONE
) {
4146 Error_Context
ec(this, "While checking template restriction `%s'",
4147 Template::get_restriction_name(template_restriction
));
4148 gen_restriction_check
=
4149 body
->chk_restriction("template definition", template_restriction
, body
);
4150 if (fp_list
&& template_restriction
!=TR_PRESENT
) {
4151 size_t nof_fps
= fp_list
->get_nof_fps();
4152 for (size_t i
=0; i
<nof_fps
; i
++) {
4153 FormalPar
* fp
= fp_list
->get_fp_byIndex(i
);
4154 // if formal par is not template then skip restriction checking,
4155 // templates can have only `in' parameters
4156 if (fp
->get_asstype()!=A_PAR_TEMPL_IN
) continue;
4157 template_restriction_t fp_tr
= fp
->get_template_restriction();
4158 switch (template_restriction
) {
4167 fp
->error("Formal parameter with template restriction `%s' "
4168 "not allowed here", Template::get_restriction_name(fp_tr
));
4171 fp
->error("Formal parameter without template restriction "
4172 "not allowed here");
4175 FATAL_ERROR("Ttcn::Def_Template::chk()");
4179 FATAL_ERROR("Ttcn::Def_Template::chk()");
4184 if (!semantic_check_only
) {
4185 if (fp_list
) fp_list
->set_genname(t_genname
);
4186 body
->set_genname_prefix("template_");
4187 body
->set_genname_recursive(t_genname
);
4188 body
->set_code_section(fp_list
? GovernedSimple::CS_INLINE
:
4189 GovernedSimple::CS_POST_INIT
);
4194 void Def_Template::chk_default() const
4196 if (!fp_list
) FATAL_ERROR("Def_Template::chk_default()");
4198 if (fp_list
->has_notused_defval())
4199 fp_list
->error("Only modified templates are allowed to use the not "
4200 "used symbol (`-') as the default parameter");
4203 Common::Assignment
*ass
= derived_ref
->get_refd_assignment(false);
4204 if (!ass
|| ass
->get_asstype() != A_TEMPLATE
) return; // Work locally.
4205 Def_Template
*base
= dynamic_cast<Def_Template
*>(ass
);
4206 if (!base
) FATAL_ERROR("Def_Template::chk_default()");
4207 FormalParList
*base_fpl
= base
->get_FormalParList();
4208 size_t nof_base_fps
= base_fpl
? base_fpl
->get_nof_fps() : 0;
4209 size_t nof_local_fps
= fp_list
? fp_list
->get_nof_fps() : 0;
4210 size_t min_fps
= nof_base_fps
;
4211 if (nof_local_fps
< nof_base_fps
) min_fps
= nof_local_fps
;
4212 for (size_t i
= 0; i
< min_fps
; i
++) {
4213 FormalPar
*base_fp
= base_fpl
->get_fp_byIndex(i
);
4214 FormalPar
*local_fp
= fp_list
->get_fp_byIndex(i
);
4215 if (local_fp
->has_notused_defval()) {
4216 if (base_fp
->has_defval()) {
4217 local_fp
->set_defval(base_fp
->get_defval());
4219 local_fp
->error("Not used symbol (`-') doesn't have the "
4220 "corresponding default parameter in the "
4225 // Additional parameters in the derived template with using the not used
4226 // symbol. TODO: Merge the loops.
4227 for (size_t i
= nof_base_fps
; i
< nof_local_fps
; i
++) {
4228 FormalPar
*local_fp
= fp_list
->get_fp_byIndex(i
);
4229 if (local_fp
->has_notused_defval())
4230 local_fp
->error("Not used symbol (`-') doesn't have the "
4231 "corresponding default parameter in the "
4236 void Def_Template::chk_modified()
4238 if (!derived_ref
) return;
4239 // Do not check the (non-existent) actual parameter list of the derived
4240 // reference against the formal parameter list of the base template.
4241 // According to TTCN-3 syntax the derived reference cannot have parameters
4242 // even if the base template is parameterized.
4243 Common::Assignment
*ass
= derived_ref
->get_refd_assignment(false);
4244 // Checking the existence and type compatibility of the base template.
4246 if (ass
->get_asstype() != A_TEMPLATE
) {
4247 derived_ref
->error("Reference to a template was expected in the "
4248 "`modifies' definition instead of %s",
4249 ass
->get_description().c_str());
4252 base_template
= dynamic_cast<Def_Template
*>(ass
);
4253 if (!base_template
) FATAL_ERROR("Def_Template::chk_modified()");
4254 Type
*base_type
= base_template
->get_Type();
4255 TypeCompatInfo
info_base(my_scope
->get_scope_mod(), type
, base_type
, true,
4257 TypeChain l_chain_base
;
4258 TypeChain r_chain_base
;
4259 if (!type
->is_compatible(base_type
, &info_base
, &l_chain_base
,
4261 if (info_base
.is_subtype_error()) {
4262 type
->error("%s", info_base
.get_subtype_error().c_str());
4264 if (!info_base
.is_erroneous()) {
4265 type
->error("The modified template has different type than base "
4266 "template `%s': `%s' was expected instead of `%s'",
4267 ass
->get_fullname().c_str(),
4268 base_type
->get_typename().c_str(),
4269 type
->get_typename().c_str());
4271 // Always use the format string.
4272 type
->error("%s", info_base
.get_error_str_str().c_str());
4275 if (info_base
.needs_conversion())
4276 body
->set_needs_conversion();
4278 // Check for restriction.
4279 if (Template::is_less_restrictive(base_template
->get_template_restriction(),
4280 template_restriction
)) {
4281 error("The template restriction is not the same or more "
4282 "restrictive as of base template `%s'", ass
->get_fullname().c_str());
4284 // Checking formal parameter lists.
4285 FormalParList
*base_fpl
= base_template
->get_FormalParList();
4286 size_t nof_base_fps
= base_fpl
? base_fpl
->get_nof_fps() : 0;
4287 size_t nof_local_fps
= fp_list
? fp_list
->get_nof_fps() : 0;
4289 if (nof_local_fps
< nof_base_fps
) {
4290 error("The modified template has fewer formal parameters than base "
4291 "template `%s': at least %lu parameter%s expected instead of %lu",
4292 ass
->get_fullname().c_str(), (unsigned long)nof_base_fps
,
4293 nof_base_fps
> 1 ? "s were" : " was", (unsigned long)nof_local_fps
);
4294 min_fps
= nof_local_fps
;
4295 } else min_fps
= nof_base_fps
;
4297 for (size_t i
= 0; i
< min_fps
; i
++) {
4298 FormalPar
*base_fp
= base_fpl
->get_fp_byIndex(i
);
4299 FormalPar
*local_fp
= fp_list
->get_fp_byIndex(i
);
4300 Error_Context
cntxt(local_fp
, "In formal parameter #%lu",
4301 (unsigned long)(i
+ 1));
4302 // Check for parameter kind equivalence (value or template).
4303 if (base_fp
->get_asstype() != local_fp
->get_asstype())
4304 local_fp
->error("The kind of parameter is not the same as in base "
4305 "template `%s': %s was expected instead of %s",
4306 ass
->get_fullname().c_str(), base_fp
->get_assname(),
4307 local_fp
->get_assname());
4308 // Check for type compatibility.
4309 Type
*base_fp_type
= base_fp
->get_Type();
4310 Type
*local_fp_type
= local_fp
->get_Type();
4311 TypeCompatInfo
info_par(my_scope
->get_scope_mod(), base_fp_type
,
4312 local_fp_type
, true, false);
4313 TypeChain l_chain_par
;
4314 TypeChain r_chain_par
;
4315 if (!base_fp_type
->is_compatible(local_fp_type
, &info_par
, &l_chain_par
,
4317 if (info_par
.is_subtype_error()) {
4318 local_fp_type
->error("%s", info_par
.get_subtype_error().c_str());
4320 if (!info_par
.is_erroneous()) {
4321 local_fp_type
->error("The type of parameter is not the same as in "
4322 "base template `%s': `%s' was expected instead "
4324 ass
->get_fullname().c_str(),
4325 base_fp_type
->get_typename().c_str(),
4326 local_fp_type
->get_typename().c_str());
4328 local_fp_type
->error("%s", info_par
.get_error_str_str().c_str());
4331 if (info_par
.needs_conversion())
4332 body
->set_needs_conversion();
4334 // Check for name equivalence.
4335 const Identifier
& base_fp_id
= base_fp
->get_id();
4336 const Identifier
& local_fp_id
= local_fp
->get_id();
4337 if (!(base_fp_id
== local_fp_id
))
4338 local_fp
->error("The name of parameter is not the same as in base "
4339 "template `%s': `%s' was expected instead of `%s'",
4340 ass
->get_fullname().c_str(),
4341 base_fp_id
.get_dispname().c_str(),
4342 local_fp_id
.get_dispname().c_str());
4343 // Check for restrictions: the derived must be same or more restrictive.
4344 if (base_fp
->get_asstype()==local_fp
->get_asstype() &&
4345 Template::is_less_restrictive(base_fp
->get_template_restriction(),
4346 local_fp
->get_template_restriction())) {
4347 local_fp
->error("The restriction of parameter is not the same or more "
4348 "restrictive as in base template `%s'", ass
->get_fullname().c_str());
4351 // Set the pointer to the body of base template.
4352 body
->set_base_template(base_template
->get_Template());
4355 void Def_Template::chk_recursive_derivation()
4357 if (recurs_deriv_checked
) return;
4358 if (base_template
) {
4359 ReferenceChain
refch(this, "While checking the chain of base templates");
4360 refch
.add(get_fullname());
4361 for (Def_Template
*iter
= base_template
; iter
; iter
= iter
->base_template
)
4363 if (iter
->recurs_deriv_checked
) break;
4364 else if (refch
.add(iter
->get_fullname()))
4365 iter
->recurs_deriv_checked
= true;
4369 recurs_deriv_checked
= true;
4372 void Def_Template::generate_code(output_struct
*target
, bool)
4374 type
->generate_code(target
);
4376 // Parameterized template. Generate code for a function which returns
4377 // a $(genname)_template and has the appropriate parameters.
4378 const string
& t_genname
= get_genname();
4379 const char *template_name
= t_genname
.c_str();
4380 const char *template_dispname
= id
->get_dispname().c_str();
4381 const string
& type_genname
= type
->get_genname_template(my_scope
);
4382 const char *type_genname_str
= type_genname
.c_str();
4384 // assemble the function body first (this also determines which parameters
4386 size_t nof_base_pars
= 0;
4387 char* function_body
= create_location_object(memptystr(), "TEMPLATE",
4389 if (debugger_active
) {
4390 function_body
= generate_code_debugger_function_init(function_body
, this);
4392 if (base_template
) {
4393 // modified template
4394 function_body
= mputprintf(function_body
, "%s ret_val(%s",
4396 base_template
->get_genname_from_scope(my_scope
).c_str());
4397 if (base_template
->fp_list
) {
4398 // the base template is also parameterized
4399 function_body
= mputc(function_body
, '(');
4400 nof_base_pars
= base_template
->fp_list
->get_nof_fps();
4401 for (size_t i
= 0; i
< nof_base_pars
; i
++) {
4402 if (i
> 0) function_body
= mputstr(function_body
, ", ");
4403 function_body
= mputstr(function_body
,
4404 fp_list
->get_fp_byIndex(i
)->get_id().get_name().c_str());
4406 function_body
= mputc(function_body
, ')');
4408 function_body
= mputstr(function_body
, ");\n");
4411 function_body
= mputprintf(function_body
, "%s ret_val;\n",
4414 if (erroneous_attrs
&& erroneous_attrs
->get_err_descr()) {
4415 function_body
= erroneous_attrs
->get_err_descr()->
4416 generate_code_str(function_body
, string("ret_val"));
4418 function_body
= body
->generate_code_init(function_body
, "ret_val");
4419 if (template_restriction
!=TR_NONE
&& gen_restriction_check
)
4420 function_body
= Template::generate_restriction_check_code(function_body
,
4421 "ret_val", template_restriction
);
4422 if (debugger_active
) {
4423 function_body
= mputstr(function_body
,
4424 "ttcn3_debugger.set_return_value((TTCN_Logger::begin_event_log2str(), "
4425 "ret_val.log(), TTCN_Logger::end_event_log2str()));\n");
4427 function_body
= mputstr(function_body
, "return ret_val;\n");
4428 // if the template modifies a parameterized template, then the inherited
4429 // formal parameters must always be displayed, otherwise generate a smart
4430 // formal parameter list (where the names of unused parameters are omitted)
4431 char *formal_par_list
= fp_list
->generate_code(memptystr(), nof_base_pars
);
4432 fp_list
->generate_code_defval(target
);
4434 target
->header
.function_prototypes
=
4435 mputprintf(target
->header
.function_prototypes
,
4436 "extern %s %s(%s);\n",
4437 type_genname_str
, template_name
, formal_par_list
);
4438 target
->source
.function_bodies
= mputprintf(target
->source
.function_bodies
,
4442 "}\n\n", type_genname_str
, template_name
, formal_par_list
, function_body
);
4443 Free(formal_par_list
);
4444 Free(function_body
);
4446 // non-parameterized template
4448 Code::init_cdef(&cdef
);
4449 type
->generate_code_object(&cdef
, body
);
4450 cdef
.init
= update_location_object(cdef
.init
);
4451 if (base_template
) {
4452 // modified template
4453 if (base_template
->my_scope
->get_scope_mod_gen() ==
4454 my_scope
->get_scope_mod_gen()) {
4455 // if the base template is in the same module its body has to be
4456 // initialized first
4457 cdef
.init
= base_template
->body
->generate_code_init(cdef
.init
,
4458 base_template
->body
->get_lhs_name().c_str());
4460 if (use_runtime_2
&& body
->get_needs_conversion()) {
4461 Type
*body_type
= body
->get_my_governor()->get_type_refd_last();
4462 Type
*base_type
= base_template
->body
->get_my_governor()
4463 ->get_type_refd_last();
4464 if (!body_type
|| !base_type
)
4465 FATAL_ERROR("Def_Template::generate_code()");
4466 const string
& tmp_id
= body
->get_temporary_id();
4467 const char *tmp_id_str
= tmp_id
.c_str();
4468 // base template initialization
4469 cdef
.init
= mputprintf(cdef
.init
,
4471 "if (!%s(%s, %s)) TTCN_error(\"Values or templates of types `%s' "
4472 "and `%s' are not compatible at run-time\");\n"
4474 body_type
->get_genname_template(my_scope
).c_str(), tmp_id_str
,
4475 TypeConv::get_conv_func(base_type
, body_type
, my_scope
4476 ->get_scope_mod()).c_str(), tmp_id_str
, base_template
4477 ->get_genname_from_scope(my_scope
).c_str(), base_type
4478 ->get_typename().c_str(), body_type
->get_typename().c_str(),
4479 body
->get_lhs_name().c_str(), tmp_id_str
);
4481 cdef
.init
= mputprintf(cdef
.init
, "%s = %s;\n",
4482 body
->get_lhs_name().c_str(),
4483 base_template
->get_genname_from_scope(my_scope
).c_str());
4486 if (use_runtime_2
&& TypeConv::needs_conv_refd(body
))
4487 cdef
.init
= TypeConv::gen_conv_code_refd(cdef
.init
,
4488 body
->get_lhs_name().c_str(), body
);
4490 cdef
.init
= body
->generate_code_init(cdef
.init
,
4491 body
->get_lhs_name().c_str());
4492 if (template_restriction
!= TR_NONE
&& gen_restriction_check
)
4493 cdef
.init
= Template::generate_restriction_check_code(cdef
.init
,
4494 body
->get_lhs_name().c_str(), template_restriction
);
4495 target
->header
.global_vars
= mputstr(target
->header
.global_vars
,
4497 target
->source
.global_vars
= mputstr(target
->source
.global_vars
,
4499 target
->functions
.post_init
= mputstr(target
->functions
.post_init
,
4501 Code::free_cdef(&cdef
);
4505 void Def_Template::generate_code(Common::CodeGenHelper
& cgh
) {
4506 generate_code(cgh
.get_outputstruct(this));
4509 char *Def_Template::generate_code_str(char *str
)
4511 const string
& t_genname
= get_genname();
4512 const char *genname_str
= t_genname
.c_str();
4513 const string
& type_genname
= type
->get_genname_template(my_scope
);
4514 const char *type_genname_str
= type_genname
.c_str();
4516 const char *dispname_str
= id
->get_dispname().c_str();
4517 NOTSUPP("Code generation for parameterized local template `%s'",
4519 str
= mputprintf(str
, "/* NOT SUPPORTED: template %s */\n",
4522 if (base_template
) {
4523 // non-parameterized modified template
4524 if (use_runtime_2
&& body
->get_needs_conversion()) {
4525 Type
*body_type
= body
->get_my_governor()->get_type_refd_last();
4526 Type
*base_type
= base_template
->body
->get_my_governor()
4527 ->get_type_refd_last();
4528 if (!body_type
|| !base_type
)
4529 FATAL_ERROR("Def_Template::generate_code_str()");
4530 const string
& tmp_id
= body
->get_temporary_id();
4531 const char *tmp_id_str
= tmp_id
.c_str();
4532 str
= mputprintf(str
,
4534 "if (!%s(%s, %s)) TTCN_error(\"Values or templates of types `%s' "
4535 "and `%s' are not compatible at run-time\");\n"
4537 body_type
->get_genname_template(my_scope
).c_str(), tmp_id_str
,
4538 TypeConv::get_conv_func(base_type
, body_type
, my_scope
4539 ->get_scope_mod()).c_str(), tmp_id_str
, base_template
4540 ->get_genname_from_scope(my_scope
).c_str(), base_type
4541 ->get_typename().c_str(), body_type
->get_typename().c_str(),
4542 type_genname_str
, genname_str
, tmp_id_str
);
4544 // the object is initialized from the base template by the
4546 str
= mputprintf(str
, "%s %s(%s);\n", type_genname_str
, genname_str
,
4547 base_template
->get_genname_from_scope(my_scope
).c_str());
4549 // the modified body is assigned in the subsequent statements
4550 str
= body
->generate_code_init(str
, genname_str
);
4552 // non-parameterized non-modified template
4553 if (body
->has_single_expr()) {
4554 // the object is initialized by the constructor
4555 str
= mputprintf(str
, "%s %s(%s);\n", type_genname_str
,
4556 genname_str
, body
->get_single_expr(false).c_str());
4557 // make sure the template's code is not generated twice (TR: HU56425)
4558 body
->set_code_generated();
4560 // the default constructor is used
4561 str
= mputprintf(str
, "%s %s;\n", type_genname_str
, genname_str
);
4562 // the body is assigned in the subsequent statements
4563 str
= body
->generate_code_init(str
, genname_str
);
4566 if (template_restriction
!= TR_NONE
&& gen_restriction_check
)
4567 str
= Template::generate_restriction_check_code(str
, genname_str
,
4568 template_restriction
);
4570 if (debugger_active
) {
4571 str
= generate_code_debugger_add_var(str
, this);
4576 void Def_Template::ilt_generate_code(ILT
*ilt
)
4578 const string
& t_genname
= get_genname();
4579 const char *genname_str
= t_genname
.c_str();
4580 char*& def
=ilt
->get_out_def();
4581 char*& init
=ilt
->get_out_branches();
4583 const char *dispname_str
= id
->get_dispname().c_str();
4584 NOTSUPP("Code generation for parameterized local template `%s'",
4586 def
= mputprintf(def
, "/* NOT SUPPORTED: template %s */\n", dispname_str
);
4587 init
= mputprintf(init
, "/* NOT SUPPORTED: template %s */\n",
4590 // non-parameterized template
4591 // use the default constructor for initialization
4592 def
= mputprintf(def
, "%s %s;\n",
4593 type
->get_genname_template(my_scope
).c_str(), genname_str
);
4594 if (base_template
) {
4595 // copy the base template with an assignment
4596 init
= mputprintf(init
, "%s = %s;\n", genname_str
,
4597 base_template
->get_genname_from_scope(my_scope
).c_str());
4599 // finally assign the body
4600 init
= body
->generate_code_init(init
, genname_str
);
4601 if (template_restriction
!=TR_NONE
&& gen_restriction_check
)
4602 init
= Template::generate_restriction_check_code(init
, genname_str
,
4603 template_restriction
);
4607 void Def_Template::dump_internal(unsigned level
) const
4609 DEBUG(level
, "Template: %s", id
->get_dispname().c_str());
4610 if (fp_list
) fp_list
->dump(level
+ 1);
4612 DEBUG(level
+ 1, "modifies: %s", derived_ref
->get_dispname().c_str());
4613 if (template_restriction
!=TR_NONE
)
4614 DEBUG(level
+ 1, "restriction: %s",
4615 Template::get_restriction_name(template_restriction
));
4616 type
->dump(level
+ 1);
4617 body
->dump(level
+ 1);
4620 // =================================
4622 // =================================
4624 Def_Var::Def_Var(Identifier
*p_id
, Type
*p_type
, Value
*p_initial_value
)
4625 : Definition(A_VAR
, p_id
), type(p_type
), initial_value(p_initial_value
)
4627 if (!p_type
) FATAL_ERROR("Ttcn::Def_Var::Def_Var()");
4628 type
->set_ownertype(Type::OT_VAR_DEF
, this);
4634 delete initial_value
;
4637 Def_Var
*Def_Var::clone() const
4639 FATAL_ERROR("Def_Var::clone");
4642 void Def_Var::set_fullname(const string
& p_fullname
)
4644 Definition::set_fullname(p_fullname
);
4645 type
->set_fullname(p_fullname
+ ".<type>");
4647 initial_value
->set_fullname(p_fullname
+ ".<initial_value>");
4650 void Def_Var::set_my_scope(Scope
*p_scope
)
4652 Definition::set_my_scope(p_scope
);
4653 type
->set_my_scope(p_scope
);
4654 if (initial_value
) initial_value
->set_my_scope(p_scope
);
4657 Type
*Def_Var::get_Type()
4666 Error_Context
cntxt(this, "In variable definition `%s'",
4667 id
->get_dispname().c_str());
4668 type
->set_genname(_T_
, get_genname());
4671 Type
*t
= type
->get_type_refd_last();
4672 switch (t
->get_typetype()) {
4674 error("Variable cannot be defined for port type `%s'",
4675 t
->get_fullname().c_str());
4677 case Type::T_SIGNATURE
:
4678 error("Variable cannot be defined for signature `%s'",
4679 t
->get_fullname().c_str());
4682 if (initial_value
) {
4683 initial_value
->set_my_governor(type
);
4684 type
->chk_this_value_ref(initial_value
);
4685 type
->chk_this_value(initial_value
, this, is_local() ?
4686 Type::EXPECTED_DYNAMIC_VALUE
: Type::EXPECTED_STATIC_VALUE
,
4687 INCOMPLETE_ALLOWED
, OMIT_NOT_ALLOWED
, SUB_CHK
);
4688 if (!semantic_check_only
) {
4689 initial_value
->set_genname_recursive(get_genname());
4690 initial_value
->set_code_section(GovernedSimple::CS_INLINE
);
4696 if (w_attrib_path
) {
4697 w_attrib_path
->chk_global_attrib();
4698 w_attrib_path
->chk_no_qualif();
4702 bool Def_Var::chk_identical(Definition
*p_def
)
4706 if (p_def
->get_asstype() != A_VAR
) {
4707 const char *dispname_str
= id
->get_dispname().c_str();
4708 error("Local definition `%s' is a variable, but the definition "
4709 "inherited from component type `%s' is a %s", dispname_str
,
4710 p_def
->get_my_scope()->get_fullname().c_str(), p_def
->get_assname());
4711 p_def
->note("The inherited definition of `%s' is here", dispname_str
);
4714 Def_Var
*p_def_var
= dynamic_cast<Def_Var
*>(p_def
);
4715 if (!p_def_var
) FATAL_ERROR("Def_Var::chk_identical()");
4716 if (!type
->is_identical(p_def_var
->type
)) {
4717 const char *dispname_str
= id
->get_dispname().c_str();
4718 type
->error("Local variable `%s' has type `%s', but the variable "
4719 "inherited from component type `%s' has type `%s'", dispname_str
,
4720 type
->get_typename().c_str(),
4721 p_def_var
->get_my_scope()->get_fullname().c_str(),
4722 p_def_var
->type
->get_typename().c_str());
4723 p_def_var
->note("The inherited variable `%s' is here", dispname_str
);
4726 if (initial_value
) {
4727 if (p_def_var
->initial_value
) {
4728 if (!initial_value
->is_unfoldable() &&
4729 !p_def_var
->initial_value
->is_unfoldable() &&
4730 !(*initial_value
== *p_def_var
->initial_value
)) {
4731 const char *dispname_str
= id
->get_dispname().c_str();
4732 initial_value
->warning("Local variable `%s' and the variable "
4733 "inherited from component type `%s' have different initial values",
4734 dispname_str
, p_def_var
->get_my_scope()->get_fullname().c_str());
4735 p_def_var
->note("The inherited variable `%s' is here", dispname_str
);
4738 const char *dispname_str
= id
->get_dispname().c_str();
4739 initial_value
->warning("Local variable `%s' has initial value, but "
4740 "the variable inherited from component type `%s' does not",
4741 dispname_str
, p_def_var
->get_my_scope()->get_fullname().c_str());
4742 p_def_var
->note("The inherited variable `%s' is here", dispname_str
);
4744 } else if (p_def_var
->initial_value
) {
4745 const char *dispname_str
= id
->get_dispname().c_str();
4746 warning("Local variable `%s' does not have initial value, but the "
4747 "variable inherited from component type `%s' has", dispname_str
,
4748 p_def_var
->get_my_scope()->get_fullname().c_str());
4749 p_def_var
->note("The inherited variable `%s' is here", dispname_str
);
4754 void Def_Var::generate_code(output_struct
*target
, bool clean_up
)
4756 type
->generate_code(target
);
4758 Code::init_cdef(&cdef
);
4759 type
->generate_code_object(&cdef
, my_scope
, get_genname(), 0, false);
4760 Code::merge_cdef(target
, &cdef
);
4761 Code::free_cdef(&cdef
);
4762 if (initial_value
) {
4763 target
->functions
.init_comp
=
4764 initial_value
->generate_code_init(target
->functions
.init_comp
,
4765 initial_value
->get_lhs_name().c_str());
4766 } else if (clean_up
) { // No initial value.
4767 target
->functions
.init_comp
= mputprintf(target
->functions
.init_comp
,
4768 "%s.clean_up();\n", get_genname().c_str());
4772 void Def_Var::generate_code(CodeGenHelper
& cgh
)
4774 generate_code(cgh
.get_outputstruct(this));
4777 char *Def_Var::generate_code_str(char *str
)
4779 const string
& t_genname
= get_genname();
4780 const char *genname_str
= t_genname
.c_str();
4781 if (initial_value
&& initial_value
->has_single_expr()) {
4782 // the initial value can be represented by a single C++ expression
4783 // the object is initialized by the constructor
4784 str
= mputprintf(str
, "%s %s(%s);\n",
4785 type
->get_genname_value(my_scope
).c_str(), genname_str
,
4786 initial_value
->get_single_expr().c_str());
4788 // use the default constructor
4789 str
= mputprintf(str
, "%s %s;\n",
4790 type
->get_genname_value(my_scope
).c_str(), genname_str
);
4791 if (initial_value
) {
4792 // the initial value is assigned using subsequent statements
4793 str
= initial_value
->generate_code_init(str
, genname_str
);
4796 if (debugger_active
) {
4797 str
= generate_code_debugger_add_var(str
, this);
4802 void Def_Var::ilt_generate_code(ILT
*ilt
)
4804 const string
& t_genname
= get_genname();
4805 const char *genname_str
= t_genname
.c_str();
4806 char*& def
=ilt
->get_out_def();
4807 char*& init
=ilt
->get_out_branches();
4808 def
= mputprintf(def
, "%s %s;\n", type
->get_genname_value(my_scope
).c_str(),
4811 init
= initial_value
->generate_code_init(init
, genname_str
);
4814 char *Def_Var::generate_code_init_comp(char *str
, Definition
*base_defn
)
4816 if (initial_value
) {
4817 str
= initial_value
->generate_code_init(str
,
4818 base_defn
->get_genname_from_scope(my_scope
).c_str());
4823 void Def_Var::dump_internal(unsigned level
) const
4825 DEBUG(level
, "Variable %s", id
->get_dispname().c_str());
4826 type
->dump(level
+ 1);
4827 if (initial_value
) initial_value
->dump(level
+ 1);
4830 // =================================
4831 // ===== Def_Var_Template
4832 // =================================
4834 Def_Var_Template::Def_Var_Template(Identifier
*p_id
, Type
*p_type
,
4835 Template
*p_initial_value
, template_restriction_t p_template_restriction
)
4836 : Definition(A_VAR_TEMPLATE
, p_id
), type(p_type
),
4837 initial_value(p_initial_value
), template_restriction(p_template_restriction
)
4839 if (!p_type
) FATAL_ERROR("Ttcn::Def_Var_Template::Def_Var_Template()");
4840 type
->set_ownertype(Type::OT_VARTMPL_DEF
, this);
4843 Def_Var_Template::~Def_Var_Template()
4846 delete initial_value
;
4849 Def_Var_Template
*Def_Var_Template::clone() const
4851 FATAL_ERROR("Def_Var_Template::clone");
4854 void Def_Var_Template::set_fullname(const string
& p_fullname
)
4856 Definition::set_fullname(p_fullname
);
4857 type
->set_fullname(p_fullname
+ ".<type>");
4859 initial_value
->set_fullname(p_fullname
+ ".<initial_value>");
4862 void Def_Var_Template::set_my_scope(Scope
*p_scope
)
4864 Definition::set_my_scope(p_scope
);
4865 type
->set_my_scope(p_scope
);
4866 if (initial_value
) initial_value
->set_my_scope(p_scope
);
4869 Type
*Def_Var_Template::get_Type()
4875 void Def_Var_Template::chk()
4878 Error_Context
cntxt(this, "In template variable definition `%s'",
4879 id
->get_dispname().c_str());
4880 type
->set_genname(_T_
, get_genname());
4883 Type
*t
= type
->get_type_refd_last();
4884 if (t
->get_typetype() == Type::T_PORT
) {
4885 error("Template variable cannot be defined for port type `%s'",
4886 t
->get_fullname().c_str());
4889 if (initial_value
) {
4890 initial_value
->set_my_governor(type
);
4891 initial_value
->flatten(false);
4893 if (initial_value
->get_templatetype() == Template::CSTR_PATTERN
&&
4894 type
->get_type_refd_last()->get_typetype() == Type::T_USTR
) {
4895 initial_value
->set_templatetype(Template::USTR_PATTERN
);
4896 initial_value
->get_ustr_pattern()->set_pattern_type(
4897 PatternString::USTR_PATTERN
);
4900 type
->chk_this_template_ref(initial_value
);
4901 // temporary hack: to allow incomplete body as initial value
4902 // checking as a modified template, but without a base template
4903 type
->chk_this_template_generic(initial_value
, INCOMPLETE_ALLOWED
,
4904 OMIT_ALLOWED
, ANY_OR_OMIT_ALLOWED
, SUB_CHK
, IMPLICIT_OMIT
, 0);
4905 gen_restriction_check
=
4906 initial_value
->chk_restriction("template variable definition",
4907 template_restriction
, initial_value
);
4908 if (!semantic_check_only
) {
4909 initial_value
->set_genname_recursive(get_genname());
4910 initial_value
->set_code_section(GovernedSimple::CS_INLINE
);
4913 if (w_attrib_path
) {
4914 w_attrib_path
->chk_global_attrib();
4915 w_attrib_path
->chk_no_qualif();
4919 bool Def_Var_Template::chk_identical(Definition
*p_def
)
4923 if (p_def
->get_asstype() != A_VAR_TEMPLATE
) {
4924 const char *dispname_str
= id
->get_dispname().c_str();
4925 error("Local definition `%s' is a template variable, but the definition "
4926 "inherited from component type `%s' is a %s", dispname_str
,
4927 p_def
->get_my_scope()->get_fullname().c_str(), p_def
->get_assname());
4928 p_def
->note("The inherited definition of `%s' is here", dispname_str
);
4931 Def_Var_Template
*p_def_var_template
=
4932 dynamic_cast<Def_Var_Template
*>(p_def
);
4933 if (!p_def_var_template
) FATAL_ERROR("Def_Var_Template::chk_identical()");
4934 if (!type
->is_identical(p_def_var_template
->type
)) {
4935 const char *dispname_str
= id
->get_dispname().c_str();
4936 type
->error("Local template variable `%s' has type `%s', but the "
4937 "template variable inherited from component type `%s' has type `%s'",
4938 dispname_str
, type
->get_typename().c_str(),
4939 p_def_var_template
->get_my_scope()->get_fullname().c_str(),
4940 p_def_var_template
->type
->get_typename().c_str());
4941 p_def_var_template
->note("The inherited template variable `%s' is here",
4945 if (initial_value
) {
4946 if (!p_def_var_template
->initial_value
) {
4947 const char *dispname_str
= id
->get_dispname().c_str();
4948 initial_value
->warning("Local template variable `%s' has initial "
4949 "value, but the template variable inherited from component type "
4950 "`%s' does not", dispname_str
,
4951 p_def_var_template
->get_my_scope()->get_fullname().c_str());
4952 p_def_var_template
->note("The inherited template variable `%s' is here",
4955 } else if (p_def_var_template
->initial_value
) {
4956 const char *dispname_str
= id
->get_dispname().c_str();
4957 warning("Local template variable `%s' does not have initial value, but "
4958 "the template variable inherited from component type `%s' has",
4960 p_def_var_template
->get_my_scope()->get_fullname().c_str());
4961 p_def_var_template
->note("The inherited template variable `%s' is here",
4967 void Def_Var_Template::generate_code(output_struct
*target
, bool clean_up
)
4969 type
->generate_code(target
);
4971 Code::init_cdef(&cdef
);
4972 type
->generate_code_object(&cdef
, my_scope
, get_genname(), 0, true);
4973 Code::merge_cdef(target
, &cdef
);
4974 Code::free_cdef(&cdef
);
4975 if (initial_value
) {
4976 if (Common::Type::T_SEQOF
== initial_value
->get_my_governor()->get_typetype() ||
4977 Common::Type::T_ARRAY
== initial_value
->get_my_governor()->get_typetype()) {
4978 target
->functions
.init_comp
= mputprintf(target
->functions
.init_comp
,
4979 "%s.remove_all_permutations();\n", initial_value
->get_lhs_name().c_str());
4981 target
->functions
.init_comp
=
4982 initial_value
->generate_code_init(target
->functions
.init_comp
,
4983 initial_value
->get_lhs_name().c_str());
4984 if (template_restriction
!=TR_NONE
&& gen_restriction_check
)
4985 target
->functions
.init_comp
= Template::generate_restriction_check_code(
4986 target
->functions
.init_comp
, initial_value
->get_lhs_name().c_str(),
4987 template_restriction
);
4988 } else if (clean_up
) { // No initial value.
4989 // Always reset component variables/variable templates on component
4990 // reinitialization. Fix for HM79493.
4991 target
->functions
.init_comp
= mputprintf(target
->functions
.init_comp
,
4992 "%s.clean_up();\n", get_genname().c_str());
4996 void Def_Var_Template::generate_code(CodeGenHelper
& cgh
)
4998 generate_code(cgh
.get_outputstruct(this));
5001 char *Def_Var_Template::generate_code_str(char *str
)
5003 const string
& t_genname
= get_genname();
5004 const char *genname_str
= t_genname
.c_str();
5005 if (initial_value
&& initial_value
->has_single_expr()) {
5006 // The initial value can be represented by a single C++ expression
5007 // the object is initialized by the constructor.
5008 str
= mputprintf(str
, "%s %s(%s);\n",
5009 type
->get_genname_template(my_scope
).c_str(), genname_str
,
5010 initial_value
->get_single_expr(false).c_str());
5012 // Use the default constructor.
5013 str
= mputprintf(str
, "%s %s;\n",
5014 type
->get_genname_template(my_scope
).c_str(), genname_str
);
5015 if (initial_value
) {
5016 // The initial value is assigned using subsequent statements.
5017 if (use_runtime_2
&& TypeConv::needs_conv_refd(initial_value
))
5018 str
= TypeConv::gen_conv_code_refd(str
, genname_str
, initial_value
);
5019 else str
= initial_value
->generate_code_init(str
, genname_str
);
5022 if (initial_value
&& template_restriction
!= TR_NONE
5023 && gen_restriction_check
)
5024 str
= Template::generate_restriction_check_code(str
, genname_str
,
5025 template_restriction
);
5026 if (debugger_active
) {
5027 str
= generate_code_debugger_add_var(str
, this);
5032 void Def_Var_Template::ilt_generate_code(ILT
*ilt
)
5034 const string
& t_genname
= get_genname();
5035 const char *genname_str
= t_genname
.c_str();
5036 char*& def
=ilt
->get_out_def();
5037 char*& init
=ilt
->get_out_branches();
5038 def
= mputprintf(def
, "%s %s;\n",
5039 type
->get_genname_template(my_scope
).c_str(), genname_str
);
5040 if (initial_value
) {
5041 init
= initial_value
->generate_code_init(init
, genname_str
);
5042 if (template_restriction
!=TR_NONE
&& gen_restriction_check
)
5043 init
= Template::generate_restriction_check_code(init
, genname_str
,
5044 template_restriction
);
5048 char *Def_Var_Template::generate_code_init_comp(char *str
,
5049 Definition
*base_defn
)
5051 if (initial_value
) {
5052 str
= initial_value
->generate_code_init(str
,
5053 base_defn
->get_genname_from_scope(my_scope
).c_str());
5054 if (template_restriction
!= TR_NONE
&& gen_restriction_check
)
5055 str
= Template::generate_restriction_check_code(str
,
5056 base_defn
->get_genname_from_scope(my_scope
).c_str(),
5057 template_restriction
);
5062 void Def_Var_Template::dump_internal(unsigned level
) const
5064 DEBUG(level
, "Template variable %s", id
->get_dispname().c_str());
5065 if (template_restriction
!=TR_NONE
)
5066 DEBUG(level
+ 1, "restriction: %s",
5067 Template::get_restriction_name(template_restriction
));
5068 type
->dump(level
+ 1);
5069 if (initial_value
) initial_value
->dump(level
+ 1);
5072 // =================================
5074 // =================================
5076 Def_Timer::~Def_Timer()
5079 delete default_duration
;
5082 Def_Timer
*Def_Timer::clone() const
5084 FATAL_ERROR("Def_Timer::clone");
5087 void Def_Timer::set_fullname(const string
& p_fullname
)
5089 Definition::set_fullname(p_fullname
);
5090 if (dimensions
) dimensions
->set_fullname(p_fullname
+ ".<dimensions>");
5091 if (default_duration
)
5092 default_duration
->set_fullname(p_fullname
+ ".<default_duration>");
5095 void Def_Timer::set_my_scope(Scope
*p_scope
)
5097 Definition::set_my_scope(p_scope
);
5098 if (dimensions
) dimensions
->set_my_scope(p_scope
);
5099 if (default_duration
) default_duration
->set_my_scope(p_scope
);
5102 ArrayDimensions
*Def_Timer::get_Dimensions()
5104 if (!checked
) chk();
5108 void Def_Timer::chk()
5111 Error_Context
cntxt(this, "In timer definition `%s'",
5112 id
->get_dispname().c_str());
5113 if (dimensions
) dimensions
->chk();
5114 if (default_duration
) {
5115 Error_Context
cntxt2(default_duration
, "In default duration");
5116 if (dimensions
) chk_array_duration(default_duration
);
5117 else chk_single_duration(default_duration
);
5118 if (!semantic_check_only
) {
5119 default_duration
->set_code_section(GovernedSimple::CS_POST_INIT
);
5123 if (w_attrib_path
) {
5124 w_attrib_path
->chk_global_attrib();
5125 w_attrib_path
->chk_no_qualif();
5129 bool Def_Timer::chk_identical(Definition
*p_def
)
5133 if (p_def
->get_asstype() != A_TIMER
) {
5134 const char *dispname_str
= id
->get_dispname().c_str();
5135 error("Local definition `%s' is a timer, but the definition inherited "
5136 "from component type `%s' is a %s", dispname_str
,
5137 p_def
->get_my_scope()->get_fullname().c_str(), p_def
->get_assname());
5138 p_def
->note("The inherited definition of `%s' is here", dispname_str
);
5141 Def_Timer
*p_def_timer
= dynamic_cast<Def_Timer
*>(p_def
);
5142 if (!p_def_timer
) FATAL_ERROR("Def_Timer::chk_identical()");
5144 if (p_def_timer
->dimensions
) {
5145 if (!dimensions
->is_identical(p_def_timer
->dimensions
)) {
5146 const char *dispname_str
= id
->get_dispname().c_str();
5147 error("Local timer `%s' and the timer inherited from component type "
5148 "`%s' have different array dimensions", dispname_str
,
5149 p_def_timer
->get_my_scope()->get_fullname().c_str());
5150 p_def_timer
->note("The inherited timer `%s' is here", dispname_str
);
5154 const char *dispname_str
= id
->get_dispname().c_str();
5155 error("Local definition `%s' is a timer array, but the definition "
5156 "inherited from component type `%s' is a single timer", dispname_str
,
5157 p_def_timer
->get_my_scope()->get_fullname().c_str());
5158 p_def_timer
->note("The inherited timer `%s' is here", dispname_str
);
5161 } else if (p_def_timer
->dimensions
) {
5162 const char *dispname_str
= id
->get_dispname().c_str();
5163 error("Local definition `%s' is a single timer, but the definition "
5164 "inherited from component type `%s' is a timer array", dispname_str
,
5165 p_def_timer
->get_my_scope()->get_fullname().c_str());
5166 p_def_timer
->note("The inherited timer `%s' is here", dispname_str
);
5169 if (default_duration
) {
5170 if (p_def_timer
->default_duration
) {
5171 if (!default_duration
->is_unfoldable() &&
5172 !p_def_timer
->default_duration
->is_unfoldable() &&
5173 !(*default_duration
== *p_def_timer
->default_duration
)) {
5174 const char *dispname_str
= id
->get_dispname().c_str();
5175 default_duration
->warning("Local timer `%s' and the timer inherited "
5176 "from component type `%s' have different default durations",
5177 dispname_str
, p_def_timer
->get_my_scope()->get_fullname().c_str());
5178 p_def_timer
->note("The inherited timer `%s' is here", dispname_str
);
5181 const char *dispname_str
= id
->get_dispname().c_str();
5182 default_duration
->error("Local timer `%s' has default duration, but "
5183 "the timer inherited from component type `%s' does not", dispname_str
,
5184 p_def_timer
->get_my_scope()->get_fullname().c_str());
5185 p_def_timer
->note("The inherited timer `%s' is here", dispname_str
);
5188 } else if (p_def_timer
->default_duration
) {
5189 const char *dispname_str
= id
->get_dispname().c_str();
5190 error("Local timer `%s' does not have default duration, but the timer "
5191 "inherited from component type `%s' has", dispname_str
,
5192 p_def_timer
->get_my_scope()->get_fullname().c_str());
5193 p_def_timer
->note("The inherited timer `%s' is here", dispname_str
);
5199 bool Def_Timer::has_default_duration(FieldOrArrayRefs
*p_subrefs
)
5201 // return true in case of any uncertainity
5202 if (!default_duration
) return false;
5203 else if (!dimensions
|| !p_subrefs
) return true;
5204 Value
*v
= default_duration
;
5205 size_t nof_dims
= dimensions
->get_nof_dims();
5206 size_t nof_refs
= p_subrefs
->get_nof_refs();
5207 size_t upper_limit
= nof_dims
< nof_refs
? nof_dims
: nof_refs
;
5208 for (size_t i
= 0; i
< upper_limit
; i
++) {
5209 v
= v
->get_value_refd_last();
5210 if (v
->get_valuetype() != Value::V_SEQOF
) break;
5211 FieldOrArrayRef
*ref
= p_subrefs
->get_ref(i
);
5212 if (ref
->get_type() != FieldOrArrayRef::ARRAY_REF
) return true;
5213 Value
*v_index
= ref
->get_val()->get_value_refd_last();
5214 if (v_index
->get_valuetype() != Value::V_INT
) return true;
5215 Int index
= v_index
->get_val_Int()->get_val()
5216 - dimensions
->get_dim_byIndex(i
)->get_offset();
5217 if (index
>= 0 && index
< static_cast<Int
>(v
->get_nof_comps()))
5218 v
= v
->get_comp_byIndex(index
);
5221 return v
->get_valuetype() != Value::V_NOTUSED
;
5224 void Def_Timer::chk_single_duration(Value
*dur
)
5226 dur
->chk_expr_float(is_local() ?
5227 Type::EXPECTED_DYNAMIC_VALUE
: Type::EXPECTED_STATIC_VALUE
);
5228 Value
*v
= dur
->get_value_refd_last();
5229 if (v
->get_valuetype() == Value::V_REAL
) {
5230 ttcn3float v_real
= v
->get_val_Real();
5231 if ( (v_real
<0.0) || isSpecialFloatValue(v_real
) ) {
5232 dur
->error("A non-negative float value was expected "
5233 "as timer duration instead of `%s'", Real2string(v_real
).c_str());
5238 void Def_Timer::chk_array_duration(Value
*dur
, size_t start_dim
)
5240 ArrayDimension
*dim
= dimensions
->get_dim_byIndex(start_dim
);
5241 bool array_size_known
= !dim
->get_has_error();
5242 size_t array_size
= 0;
5243 if (array_size_known
) array_size
= dim
->get_size();
5244 Value
*v
= dur
->get_value_refd_last();
5245 switch (v
->get_valuetype()) {
5246 case Value::V_ERROR
:
5248 case Value::V_SEQOF
: {
5249 size_t nof_vs
= v
->get_nof_comps();
5250 // Value-list notation.
5251 if (!v
->is_indexed()) {
5252 if (array_size_known
) {
5253 if (array_size
> nof_vs
) {
5254 dur
->error("Too few elements in the default duration of timer "
5255 "array: %lu was expected instead of %lu",
5256 (unsigned long)array_size
, (unsigned long)nof_vs
);
5257 } else if (array_size
< nof_vs
) {
5258 dur
->error("Too many elements in the default duration of timer "
5259 "array: %lu was expected instead of %lu",
5260 (unsigned long)array_size
, (unsigned long)nof_vs
);
5263 bool last_dimension
= start_dim
+ 1 >= dimensions
->get_nof_dims();
5264 for (size_t i
= 0; i
< nof_vs
; i
++) {
5265 Value
*array_v
= v
->get_comp_byIndex(i
);
5266 if (array_v
->get_valuetype() == Value::V_NOTUSED
) continue;
5267 if (last_dimension
) chk_single_duration(array_v
);
5268 else chk_array_duration(array_v
, start_dim
+ 1);
5271 // Indexed-notation.
5272 bool last_dimension
= start_dim
+ 1 >= dimensions
->get_nof_dims();
5273 map
<Int
, Int
> index_map
;
5274 for (size_t i
= 0; i
< nof_vs
; i
++) {
5275 Value
*array_v
= v
->get_comp_byIndex(i
);
5276 if (array_v
->get_valuetype() == Value::V_NOTUSED
) continue;
5277 if (last_dimension
) chk_single_duration(array_v
);
5278 else chk_array_duration(array_v
, start_dim
+ 1);
5279 Error_Context
cntxt(this, "In timer array element %lu",
5280 (unsigned long)(i
+ 1));
5281 Value
*index
= v
->get_index_byIndex(i
);
5282 dim
->chk_index(index
, Type::EXPECTED_DYNAMIC_VALUE
);
5283 if (index
->get_value_refd_last()->get_valuetype() == Value::V_INT
) {
5284 const int_val_t
*index_int
= index
->get_value_refd_last()
5286 if (*index_int
> INT_MAX
) {
5287 index
->error("An integer value less than `%d' was expected for "
5288 "indexing timer array instead of `%s'", INT_MAX
,
5289 (index_int
->t_str()).c_str());
5290 index
->set_valuetype(Value::V_ERROR
);
5292 Int index_val
= index_int
->get_val();
5293 if (index_map
.has_key(index_val
)) {
5294 index
->error("Duplicate index value `%s' for timer array "
5295 "elements `%s' and `%s'",
5296 Int2string(index_val
).c_str(),
5297 Int2string((Int
)i
+ 1).c_str(),
5298 Int2string(*index_map
[index_val
]).c_str());
5299 index
->set_valuetype(Value::V_ERROR
);
5301 index_map
.add(index_val
, new Int((Int
)i
+ 1));
5306 // It's not possible to have "index_map.size() > array_size", since we
5307 // add only correct constant-index values into the map. It's possible
5308 // to create partially initialized timer arrays.
5309 for (size_t i
= 0; i
< index_map
.size(); i
++)
5310 delete index_map
.get_nth_elem(i
);
5315 if (array_size_known
) {
5316 dur
->error("An array value (with %lu elements) was expected as "
5317 "default duration of timer array",
5318 (unsigned long)array_size
);
5320 dur
->error("An array value was expected as default duration of timer "
5323 dur
->set_valuetype(Value::V_ERROR
);
5328 void Def_Timer::generate_code(output_struct
*target
, bool)
5330 const string
& t_genname
= get_genname();
5331 const char *genname_str
= t_genname
.c_str();
5332 const string
& dispname
= id
->get_dispname();
5335 const string
& array_type
= dimensions
->get_timer_type();
5336 const char *array_type_str
= array_type
.c_str();
5337 target
->header
.global_vars
= mputprintf(target
->header
.global_vars
,
5338 "extern %s %s;\n", array_type_str
, genname_str
);
5339 target
->source
.global_vars
= mputprintf(target
->source
.global_vars
,
5340 "%s %s;\n", array_type_str
, genname_str
);
5341 target
->functions
.pre_init
= mputstr(target
->functions
.pre_init
, "{\n"
5342 "static const char * const timer_name = \"");
5343 target
->functions
.pre_init
= mputstr(target
->functions
.pre_init
,
5345 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
5347 "%s.set_name(timer_name);\n"
5348 "}\n", genname_str
);
5349 if (default_duration
) target
->functions
.post_init
=
5350 generate_code_array_duration(target
->functions
.post_init
, genname_str
,
5354 target
->header
.global_vars
= mputprintf(target
->header
.global_vars
,
5355 "extern TIMER %s;\n", genname_str
);
5356 if (default_duration
) {
5357 // has default duration
5358 Value
*v
= default_duration
->get_value_refd_last();
5359 if (v
->get_valuetype() == Value::V_REAL
) {
5360 // duration is known at compilation time -> set in the constructor
5361 target
->source
.global_vars
= mputprintf(target
->source
.global_vars
,
5362 "TIMER %s(\"%s\", %s);\n", genname_str
, dispname
.c_str(),
5363 v
->get_single_expr().c_str());
5365 // duration is known only at runtime -> set in post_init
5366 target
->source
.global_vars
= mputprintf(target
->source
.global_vars
,
5367 "TIMER %s(\"%s\");\n", genname_str
, dispname
.c_str());
5368 expression_struct expr
;
5369 Code::init_expr(&expr
);
5370 expr
.expr
= mputprintf(expr
.expr
, "%s.set_default_duration(",
5372 default_duration
->generate_code_expr(&expr
);
5373 expr
.expr
= mputc(expr
.expr
, ')');
5374 target
->functions
.post_init
=
5375 Code::merge_free_expr(target
->functions
.post_init
, &expr
);
5378 // does not have default duration
5379 target
->source
.global_vars
= mputprintf(target
->source
.global_vars
,
5380 "TIMER %s(\"%s\");\n", genname_str
, dispname
.c_str());
5385 void Def_Timer::generate_code(CodeGenHelper
& cgh
) {
5386 generate_code(cgh
.get_current_outputstruct());
5389 char *Def_Timer::generate_code_array_duration(char *str
,
5390 const char *object_name
, Value
*dur
, size_t start_dim
)
5392 ArrayDimension
*dim
= dimensions
->get_dim_byIndex(start_dim
);
5393 size_t dim_size
= dim
->get_size();
5394 Value
*v
= dur
->get_value_refd_last();
5395 if (v
->get_valuetype() != Value::V_SEQOF
5396 || (v
->get_nof_comps() != dim_size
&& !v
->is_indexed()))
5397 FATAL_ERROR("Def_Timer::generate_code_array_duration()");
5398 // Value-list notation.
5399 if (!v
->is_indexed()) {
5400 if (start_dim
+ 1 < dimensions
->get_nof_dims()) {
5401 // There are more dimensions, the elements of "v" are arrays a
5402 // temporary reference shall be introduced if the next dimension has
5403 // more than 1 elements.
5404 bool temp_ref_needed
=
5405 dimensions
->get_dim_byIndex(start_dim
+ 1)->get_size() > 1;
5406 for (size_t i
= 0; i
< dim_size
; i
++) {
5407 Value
*v_elem
= v
->get_comp_byIndex(i
);
5408 if (v_elem
->get_valuetype() == Value::V_NOTUSED
) continue;
5409 if (temp_ref_needed
) {
5410 const string
& tmp_id
= my_scope
->get_scope_mod_gen()
5411 ->get_temporary_id();
5412 const char *tmp_str
= tmp_id
.c_str();
5413 str
= mputprintf(str
, "{\n"
5414 "%s& %s = %s.array_element(%lu);\n",
5415 dimensions
->get_timer_type(start_dim
+ 1).c_str(),
5416 tmp_str
, object_name
, (unsigned long)i
);
5417 str
= generate_code_array_duration(str
, tmp_str
, v_elem
,
5419 str
= mputstr(str
, "}\n");
5421 char *tmp_str
= mprintf("%s.array_element(%lu)", object_name
,
5423 str
= generate_code_array_duration(str
, tmp_str
, v_elem
,
5429 // We are in the last dimension, the elements of "v" are floats.
5430 for (size_t i
= 0; i
< dim_size
; i
++) {
5431 Value
*v_elem
= v
->get_comp_byIndex(i
);
5432 if (v_elem
->get_valuetype() == Value::V_NOTUSED
) continue;
5433 expression_struct expr
;
5434 Code::init_expr(&expr
);
5435 expr
.expr
= mputprintf(expr
.expr
,
5436 "%s.array_element(%lu).set_default_duration(",
5437 object_name
, (unsigned long)i
);
5438 v_elem
->generate_code_expr(&expr
);
5439 expr
.expr
= mputc(expr
.expr
, ')');
5440 str
= Code::merge_free_expr(str
, &expr
);
5443 // Indexed-list notation.
5445 if (start_dim
+ 1 < dimensions
->get_nof_dims()) {
5446 bool temp_ref_needed
=
5447 dimensions
->get_dim_byIndex(start_dim
+ 1)->get_size() > 1;
5448 for (size_t i
= 0; i
< v
->get_nof_comps(); i
++) {
5449 Value
*v_elem
= v
->get_comp_byIndex(i
);
5450 if (v_elem
->get_valuetype() == Value::V_NOTUSED
) continue;
5451 if (temp_ref_needed
) {
5452 const string
& tmp_id
= my_scope
->get_scope_mod_gen()
5453 ->get_temporary_id();
5454 const string
& idx_id
= my_scope
->get_scope_mod_gen()
5455 ->get_temporary_id();
5456 const char *tmp_str
= tmp_id
.c_str();
5457 str
= mputstr(str
, "{\n");
5458 str
= mputprintf(str
, "int %s;\n", idx_id
.c_str());
5459 str
= v
->get_index_byIndex(i
)->generate_code_init(str
,
5461 str
= mputprintf(str
, "%s& %s = %s.array_element(%s);\n",
5462 dimensions
->get_timer_type(start_dim
+ 1).c_str(),
5463 tmp_str
, object_name
, idx_id
.c_str());
5464 str
= generate_code_array_duration(str
, tmp_str
, v_elem
,
5466 str
= mputstr(str
, "}\n");
5468 const string
& idx_id
= my_scope
->get_scope_mod_gen()
5469 ->get_temporary_id();
5470 str
= mputstr(str
, "{\n");
5471 str
= mputprintf(str
, "int %s;\n", idx_id
.c_str());
5472 str
= v
->get_index_byIndex(i
)->generate_code_init(str
,
5474 char *tmp_str
= mprintf("%s.array_element(%s)", object_name
,
5476 str
= generate_code_array_duration(str
, tmp_str
, v_elem
,
5478 str
= mputstr(str
, "}\n");
5483 for (size_t i
= 0; i
< v
->get_nof_comps(); i
++) {
5484 Value
*v_elem
= v
->get_comp_byIndex(i
);
5485 if (v_elem
->get_valuetype() == Value::V_NOTUSED
) continue;
5486 expression_struct expr
;
5487 Code::init_expr(&expr
);
5488 str
= mputstr(str
, "{\n");
5489 const string
& idx_id
= my_scope
->get_scope_mod_gen()
5490 ->get_temporary_id();
5491 str
= mputprintf(str
, "int %s;\n", idx_id
.c_str());
5492 str
= v
->get_index_byIndex(i
)->generate_code_init(str
,
5494 str
= mputprintf(str
,
5495 "%s.array_element(%s).set_default_duration(",
5496 object_name
, idx_id
.c_str());
5497 v_elem
->generate_code_expr(&expr
);
5498 expr
.expr
= mputc(expr
.expr
, ')');
5499 str
= Code::merge_free_expr(str
, &expr
);
5500 str
= mputstr(str
, "}\n");
5507 char *Def_Timer::generate_code_str(char *str
)
5509 const string
& t_genname
= get_genname();
5510 const char *genname_str
= t_genname
.c_str();
5511 const string
& dispname
= id
->get_dispname();
5514 const string
& array_type
= dimensions
->get_timer_type();
5515 const char *array_type_str
= array_type
.c_str();
5516 str
= mputprintf(str
, "%s %s;\n", array_type_str
, genname_str
);
5517 str
= mputstr(str
, "{\n"
5518 "static const char * const timer_name = \"");
5519 str
= mputstr(str
, dispname
.c_str());
5520 str
= mputprintf(str
, "\";\n"
5521 "%s.set_name(timer_name);\n"
5522 "}\n", genname_str
);
5523 if (default_duration
) str
= generate_code_array_duration(str
,
5524 genname_str
, default_duration
);
5527 if (default_duration
&& default_duration
->has_single_expr()) {
5528 // the default duration can be passed to the constructor
5529 str
= mputprintf(str
, "TIMER %s(\"%s\", %s);\n", genname_str
,
5530 dispname
.c_str(), default_duration
->get_single_expr().c_str());
5532 // only the name is passed to the constructor
5533 str
= mputprintf(str
, "TIMER %s(\"%s\");\n", genname_str
,
5535 if (default_duration
) {
5536 // the default duration is set explicitly
5537 expression_struct expr
;
5538 Code::init_expr(&expr
);
5539 expr
.expr
= mputprintf(expr
.expr
, "%s.set_default_duration(",
5541 default_duration
->generate_code_expr(&expr
);
5542 expr
.expr
= mputc(expr
.expr
, ')');
5543 str
= Code::merge_free_expr(str
, &expr
);
5547 if (debugger_active
) {
5548 str
= generate_code_debugger_add_var(str
, this);
5553 void Def_Timer::ilt_generate_code(ILT
*ilt
)
5555 const string
& t_genname
= get_genname();
5556 const char *genname_str
= t_genname
.c_str();
5557 const string
& dispname
= id
->get_dispname();
5559 char*& def
= ilt
->get_out_def();
5560 char*& init
= ilt
->get_out_branches();
5564 const string
& array_type
= dimensions
->get_timer_type();
5565 const char *array_type_str
= array_type
.c_str();
5566 def
= mputprintf(def
, "%s %s;\n", array_type_str
, genname_str
);
5567 def
= mputstr(def
, "{\n"
5568 "static const char * const timer_names[] = { ");
5569 def
= dimensions
->generate_element_names(def
, dispname
);
5570 def
= mputprintf(def
, " };\n"
5571 "%s.set_name(%lu, timer_names);\n"
5572 "}\n", genname_str
, (unsigned long) dimensions
->get_array_size());
5573 if (default_duration
) init
= generate_code_array_duration(init
,
5574 genname_str
, default_duration
);
5577 if (default_duration
) {
5578 // has default duration
5579 Value
*v
= default_duration
->get_value_refd_last();
5580 if (v
->get_valuetype() == Value::V_REAL
) {
5581 // duration is known at compilation time -> set in the constructor
5582 def
= mputprintf(def
, "TIMER %s(\"%s\", %s);\n", genname_str
,
5583 dispname
.c_str(), v
->get_single_expr().c_str());
5585 // duration is known only at runtime -> set when control reaches the
5587 def
= mputprintf(def
, "TIMER %s(\"%s\");\n", genname_str
,
5589 expression_struct expr
;
5590 Code::init_expr(&expr
);
5591 expr
.expr
= mputprintf(expr
.expr
, "%s.set_default_duration(",
5593 default_duration
->generate_code_expr(&expr
);
5594 expr
.expr
= mputc(expr
.expr
, ')');
5595 init
= Code::merge_free_expr(init
, &expr
);
5598 // does not have default duration
5599 def
= mputprintf(def
, "TIMER %s(\"%s\");\n", genname_str
,
5605 char *Def_Timer::generate_code_init_comp(char *str
, Definition
*base_defn
)
5607 if (default_duration
) {
5608 Def_Timer
*base_timer_defn
= dynamic_cast<Def_Timer
*>(base_defn
);
5609 if (!base_timer_defn
|| !base_timer_defn
->default_duration
)
5610 FATAL_ERROR("Def_Timer::generate_code_init_comp()");
5611 // initializer is not needed if the default durations are the same
5612 // constants in both timers
5613 if (default_duration
->is_unfoldable() ||
5614 base_timer_defn
->default_duration
->is_unfoldable() ||
5615 !(*default_duration
== *base_timer_defn
->default_duration
)) {
5617 str
= generate_code_array_duration(str
,
5618 base_timer_defn
->get_genname_from_scope(my_scope
).c_str(),
5621 expression_struct expr
;
5622 Code::init_expr(&expr
);
5623 expr
.expr
= mputprintf(expr
.expr
, "%s.set_default_duration(",
5624 base_timer_defn
->get_genname_from_scope(my_scope
).c_str());
5625 default_duration
->generate_code_expr(&expr
);
5626 expr
.expr
= mputc(expr
.expr
, ')');
5627 str
= Code::merge_free_expr(str
, &expr
);
5634 void Def_Timer::dump_internal(unsigned level
) const
5636 DEBUG(level
, "Timer: %s", id
->get_dispname().c_str());
5637 if (dimensions
) dimensions
->dump(level
+ 1);
5638 if (default_duration
) {
5639 DEBUG(level
+ 1, "Default duration:");
5640 default_duration
->dump(level
+ 1);
5644 // =================================
5646 // =================================
5648 Def_Port::Def_Port(Identifier
*p_id
, Reference
*p_tref
,
5649 ArrayDimensions
*p_dims
)
5650 : Definition(A_PORT
, p_id
), type_ref(p_tref
), port_type(0),
5653 if (!p_tref
) FATAL_ERROR("Def_Port::Def_Port()");
5656 Def_Port::~Def_Port()
5662 Def_Port
*Def_Port::clone() const
5664 FATAL_ERROR("Def_Port::clone");
5667 void Def_Port::set_fullname(const string
& p_fullname
)
5669 Definition::set_fullname(p_fullname
);
5670 type_ref
->set_fullname(p_fullname
+ ".<type_ref>");
5671 if (dimensions
) dimensions
->set_fullname(p_fullname
);
5674 void Def_Port::set_my_scope(Scope
*p_scope
)
5676 Definition::set_my_scope(p_scope
);
5677 type_ref
->set_my_scope(p_scope
);
5678 if (dimensions
) dimensions
->set_my_scope(p_scope
);
5681 Type
*Def_Port::get_Type()
5687 ArrayDimensions
*Def_Port::get_Dimensions()
5689 if (!checked
) chk();
5693 void Def_Port::chk()
5695 if (checked
) return;
5697 Error_Context
cntxt(this, "In port definition `%s'",
5698 id
->get_dispname().c_str());
5699 Common::Assignment
*ass
= type_ref
->get_refd_assignment();
5701 if (ass
->get_asstype() == A_TYPE
) {
5702 Type
*t
= ass
->get_Type()->get_type_refd_last();
5703 if (t
->get_typetype() == Type::T_PORT
) port_type
= t
;
5704 else type_ref
->error("Type reference `%s' does not refer to a "
5705 "port type", type_ref
->get_dispname().c_str());
5706 } else type_ref
->error("Reference `%s' does not refer to a "
5707 "type", type_ref
->get_dispname().c_str());
5709 if (dimensions
) dimensions
->chk();
5710 if (w_attrib_path
) {
5711 w_attrib_path
->chk_global_attrib();
5712 w_attrib_path
->chk_no_qualif();
5716 bool Def_Port::chk_identical(Definition
*p_def
)
5720 if (p_def
->get_asstype() != A_PORT
) {
5721 const char *dispname_str
= id
->get_dispname().c_str();
5722 error("Local definition `%s' is a port, but the definition inherited "
5723 "from component type `%s' is a %s", dispname_str
,
5724 p_def
->get_my_scope()->get_fullname().c_str(), p_def
->get_assname());
5725 p_def
->note("The inherited definition of `%s' is here", dispname_str
);
5728 Def_Port
*p_def_port
= dynamic_cast<Def_Port
*>(p_def
);
5729 if (!p_def_port
) FATAL_ERROR("Def_Port::chk_identical()");
5730 if (port_type
&& p_def_port
->port_type
&&
5731 port_type
!= p_def_port
->port_type
) {
5732 const char *dispname_str
= id
->get_dispname().c_str();
5733 type_ref
->error("Local port `%s' has type `%s', but the port inherited "
5734 "from component type `%s' has type `%s'", dispname_str
,
5735 port_type
->get_typename().c_str(),
5736 p_def_port
->get_my_scope()->get_fullname().c_str(),
5737 p_def_port
->port_type
->get_typename().c_str());
5738 p_def_port
->note("The inherited port `%s' is here", dispname_str
);
5742 if (p_def_port
->dimensions
) {
5743 if (!dimensions
->is_identical(p_def_port
->dimensions
)) {
5744 const char *dispname_str
= id
->get_dispname().c_str();
5745 error("Local port `%s' and the port inherited from component type "
5746 "`%s' have different array dimensions", dispname_str
,
5747 p_def_port
->get_my_scope()->get_fullname().c_str());
5748 p_def_port
->note("The inherited port `%s' is here", dispname_str
);
5752 const char *dispname_str
= id
->get_dispname().c_str();
5753 error("Local definition `%s' is a port array, but the definition "
5754 "inherited from component type `%s' is a single port", dispname_str
,
5755 p_def_port
->get_my_scope()->get_fullname().c_str());
5756 p_def_port
->note("The inherited port `%s' is here", dispname_str
);
5759 } else if (p_def_port
->dimensions
) {
5760 const char *dispname_str
= id
->get_dispname().c_str();
5761 error("Local definition `%s' is a single port, but the definition "
5762 "inherited from component type `%s' is a port array", dispname_str
,
5763 p_def_port
->get_my_scope()->get_fullname().c_str());
5764 p_def_port
->note("The inherited port `%s' is here", dispname_str
);
5770 void Def_Port::generate_code(output_struct
*target
, bool)
5772 const string
& t_genname
= get_genname();
5773 const char *genname_str
= t_genname
.c_str();
5774 const string
& type_genname
= port_type
->get_genname_value(my_scope
);
5775 const string
& dispname
= id
->get_dispname();
5778 const string
& array_type
= dimensions
->get_port_type(type_genname
);
5779 const char *array_type_str
= array_type
.c_str();
5780 target
->header
.global_vars
= mputprintf(target
->header
.global_vars
,
5781 "extern %s %s;\n", array_type_str
, genname_str
);
5782 target
->source
.global_vars
= mputprintf(target
->source
.global_vars
,
5783 "%s %s;\n", array_type_str
, genname_str
);
5784 target
->functions
.pre_init
= mputstr(target
->functions
.pre_init
, "{\n"
5785 "static const char * const port_name = \"");
5786 target
->functions
.pre_init
= mputstr(target
->functions
.pre_init
,
5788 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
5790 "%s.set_name(port_name);\n"
5791 "}\n", genname_str
);
5794 const char *type_genname_str
= type_genname
.c_str();
5795 target
->header
.global_vars
= mputprintf(target
->header
.global_vars
,
5796 "extern %s %s;\n", type_genname_str
, genname_str
);
5797 target
->source
.global_vars
= mputprintf(target
->source
.global_vars
,
5798 "%s %s(\"%s\");\n", type_genname_str
, genname_str
, dispname
.c_str());
5800 target
->functions
.init_comp
= mputprintf(target
->functions
.init_comp
,
5801 "%s.activate_port();\n", genname_str
);
5804 void Def_Port::generate_code(CodeGenHelper
& cgh
) {
5805 generate_code(cgh
.get_current_outputstruct());
5808 char *Def_Port::generate_code_init_comp(char *str
, Definition
*base_defn
)
5810 return mputprintf(str
, "%s.activate_port();\n",
5811 base_defn
->get_genname_from_scope(my_scope
).c_str());
5814 void Def_Port::dump_internal(unsigned level
) const
5816 DEBUG(level
, "Port: %s", id
->get_dispname().c_str());
5817 DEBUG(level
+ 1, "Port type:");
5818 type_ref
->dump(level
+ 2);
5819 if (dimensions
) dimensions
->dump(level
+ 1);
5822 // =================================
5823 // ===== Def_Function_Base
5824 // =================================
5826 Def_Function_Base::asstype_t
Def_Function_Base::determine_asstype(
5827 bool is_external
, bool has_return_type
, bool returns_template
)
5830 if (has_return_type
) {
5831 if (returns_template
) return A_EXT_FUNCTION_RTEMP
;
5832 else return A_EXT_FUNCTION_RVAL
;
5834 if (returns_template
)
5835 FATAL_ERROR("Def_Function_Base::determine_asstype()");
5836 return A_EXT_FUNCTION
;
5838 } else { // not an external function
5839 if (has_return_type
) {
5840 if (returns_template
) return A_FUNCTION_RTEMP
;
5841 else return A_FUNCTION_RVAL
;
5843 if (returns_template
)
5844 FATAL_ERROR("Def_Function_Base::determine_asstype()");
5850 Def_Function_Base::Def_Function_Base(const Def_Function_Base
& p
)
5851 : Definition(p
), prototype(PROTOTYPE_NONE
), input_type(0), output_type(0)
5853 fp_list
= p
.fp_list
->clone();
5854 fp_list
->set_my_def(this);
5855 return_type
= p
.return_type
? p
.return_type
->clone() : 0;
5856 template_restriction
= p
.template_restriction
;
5859 Def_Function_Base::Def_Function_Base(bool is_external
, Identifier
*p_id
,
5860 FormalParList
*p_fpl
, Type
*p_return_type
, bool returns_template
,
5861 template_restriction_t p_template_restriction
)
5862 : Definition(determine_asstype(is_external
, p_return_type
!= 0,
5863 returns_template
), p_id
), fp_list(p_fpl
), return_type(p_return_type
),
5864 prototype(PROTOTYPE_NONE
), input_type(0), output_type(0),
5865 template_restriction(p_template_restriction
)
5867 if (!p_fpl
) FATAL_ERROR("Def_Function_Base::Def_Function_Base()");
5868 fp_list
->set_my_def(this);
5869 if (return_type
) return_type
->set_ownertype(Type::OT_FUNCTION_DEF
, this);
5872 Def_Function_Base::~Def_Function_Base()
5878 void Def_Function_Base::set_fullname(const string
& p_fullname
)
5880 Definition::set_fullname(p_fullname
);
5881 fp_list
->set_fullname(p_fullname
+ ".<formal_par_list>");
5882 if (return_type
) return_type
->set_fullname(p_fullname
+ ".<return_type>");
5885 void Def_Function_Base::set_my_scope(Scope
*p_scope
)
5887 Definition::set_my_scope(p_scope
);
5888 fp_list
->set_my_scope(p_scope
);
5889 if (return_type
) return_type
->set_my_scope(p_scope
);
5892 Type
*Def_Function_Base::get_Type()
5894 if (!checked
) chk();
5898 FormalParList
*Def_Function_Base::get_FormalParList()
5900 if (!checked
) chk();
5904 const char *Def_Function_Base::get_prototype_name() const
5906 switch (prototype
) {
5907 case PROTOTYPE_NONE
:
5908 return "<no prototype>";
5909 case PROTOTYPE_CONVERT
:
5911 case PROTOTYPE_FAST
:
5913 case PROTOTYPE_BACKTRACK
:
5915 case PROTOTYPE_SLIDING
:
5918 return "<unknown prototype>";
5922 void Def_Function_Base::chk_prototype()
5924 switch (prototype
) {
5925 case PROTOTYPE_NONE
:
5926 // return immediately
5928 case PROTOTYPE_CONVERT
:
5929 case PROTOTYPE_FAST
:
5930 case PROTOTYPE_BACKTRACK
:
5931 case PROTOTYPE_SLIDING
:
5932 // perform the checks below
5935 FATAL_ERROR("Def_Function_Base::chk_prototype()");
5937 // checking the formal parameter list
5938 if (prototype
== PROTOTYPE_CONVERT
) {
5939 if (fp_list
->get_nof_fps() == 1) {
5940 FormalPar
*par
= fp_list
->get_fp_byIndex(0);
5941 if (par
->get_asstype() == A_PAR_VAL_IN
) {
5942 input_type
= par
->get_Type();
5944 par
->error("The parameter must be an `in' value parameter for "
5945 "attribute `prototype(%s)' instead of %s", get_prototype_name(),
5946 par
->get_assname());
5949 fp_list
->error("The function must have one parameter instead of %lu "
5950 "for attribute `prototype(%s)'", (unsigned long) fp_list
->get_nof_fps(),
5951 get_prototype_name());
5953 } else { // not PROTOTYPE_CONVERT
5954 if (fp_list
->get_nof_fps() == 2) {
5955 FormalPar
*first_par
= fp_list
->get_fp_byIndex(0);
5956 if (prototype
== PROTOTYPE_SLIDING
) {
5957 if (first_par
->get_asstype() == A_PAR_VAL_INOUT
) {
5958 Type
*first_par_type
= first_par
->get_Type();
5959 switch (first_par_type
->get_type_refd_last()
5960 ->get_typetype_ttcn3()) {
5965 input_type
= first_par_type
;
5968 first_par_type
->error("The type of the first parameter must be "
5969 "`octetstring' or `charstring' or `bitstring' for attribute "
5970 "`prototype(%s)' instead of `%s'", get_prototype_name(),
5971 first_par_type
->get_typename().c_str());
5974 first_par
->error("The first parameter must be an `inout' value "
5975 "parameter for attribute `prototype(%s)' instead of %s",
5976 get_prototype_name(), first_par
->get_assname());
5979 if (first_par
->get_asstype() == A_PAR_VAL_IN
) {
5980 input_type
= first_par
->get_Type();
5982 first_par
->error("The first parameter must be an `in' value "
5983 "parameter for attribute `prototype(%s)' instead of %s",
5984 get_prototype_name(), first_par
->get_assname());
5987 FormalPar
*second_par
= fp_list
->get_fp_byIndex(1);
5988 if (second_par
->get_asstype() == A_PAR_VAL_OUT
) {
5989 output_type
= second_par
->get_Type();
5991 second_par
->error("The second parameter must be an `out' value "
5992 "parameter for attribute `prototype(%s)' instead of %s",
5993 get_prototype_name(), second_par
->get_assname());
5996 fp_list
->error("The function must have two parameters for attribute "
5997 "`prototype(%s)' instead of %lu", get_prototype_name(),
5998 (unsigned long) fp_list
->get_nof_fps());
6001 // checking the return type
6002 if (prototype
== PROTOTYPE_FAST
) {
6004 return_type
->error("The function cannot have return type for "
6005 "attribute `prototype(%s)'", get_prototype_name());
6009 if (asstype
== A_FUNCTION_RTEMP
|| asstype
== A_EXT_FUNCTION_RTEMP
)
6010 return_type
->error("The function must return a value instead of a "
6011 "template for attribute `prototype(%s)'", get_prototype_name());
6012 if (prototype
== PROTOTYPE_CONVERT
) {
6013 output_type
= return_type
;
6015 switch (return_type
->get_type_refd_last()->get_typetype_ttcn3()) {
6020 return_type
->error("The return type of the function must be "
6021 "`integer' instead of `%s' for attribute `prototype(%s)'",
6022 return_type
->get_typename().c_str(), get_prototype_name());
6026 error("The function must have return type for attribute "
6027 "`prototype(%s)'", get_prototype_name());
6030 // checking the 'runs on' clause
6031 if (get_RunsOnType()) {
6032 error("The function cannot have `runs on' clause for attribute "
6033 "`prototype(%s)'", get_prototype_name());
6037 Type
*Def_Function_Base::get_input_type()
6039 if (!checked
) chk();
6043 Type
*Def_Function_Base::get_output_type()
6045 if (!checked
) chk();
6050 // =================================
6051 // ===== Def_Function
6052 // =================================
6054 Def_Function::Def_Function(Identifier
*p_id
, FormalParList
*p_fpl
,
6055 Reference
*p_runs_on_ref
, Type
*p_return_type
,
6056 bool returns_template
,
6057 template_restriction_t p_template_restriction
,
6058 StatementBlock
*p_block
)
6059 : Def_Function_Base(false, p_id
, p_fpl
, p_return_type
, returns_template
,
6060 p_template_restriction
),
6061 runs_on_ref(p_runs_on_ref
), runs_on_type(0), block(p_block
),
6062 is_startable(false), transparent(false)
6064 if (!p_block
) FATAL_ERROR("Def_Function::Def_Function()");
6065 block
->set_my_def(this);
6068 Def_Function::~Def_Function()
6074 Def_Function
*Def_Function::clone() const
6076 FATAL_ERROR("Def_Function::clone");
6079 void Def_Function::set_fullname(const string
& p_fullname
)
6081 Def_Function_Base::set_fullname(p_fullname
);
6082 if (runs_on_ref
) runs_on_ref
->set_fullname(p_fullname
+ ".<runs_on_type>");
6083 block
->set_fullname(p_fullname
+ ".<statement_block>");
6086 void Def_Function::set_my_scope(Scope
*p_scope
)
6088 bridgeScope
.set_parent_scope(p_scope
);
6089 bridgeScope
.set_scopeMacro_name(id
->get_dispname());
6091 Def_Function_Base::set_my_scope(&bridgeScope
);
6092 if (runs_on_ref
) runs_on_ref
->set_my_scope(&bridgeScope
);
6093 block
->set_my_scope(fp_list
);
6096 Type
*Def_Function::get_RunsOnType()
6098 if (!checked
) chk();
6099 return runs_on_type
;
6102 RunsOnScope
*Def_Function::get_runs_on_scope(Type
*comptype
)
6104 Module
*my_module
= dynamic_cast<Module
*>(my_scope
->get_scope_mod());
6105 if (!my_module
) FATAL_ERROR("Def_Function::get_runs_on_scope()");
6106 return my_module
->get_runs_on_scope(comptype
);
6109 void Def_Function::chk()
6111 if (checked
) return;
6113 Error_Context
cntxt(this, "In function definition `%s'",
6114 id
->get_dispname().c_str());
6115 // checking the `runs on' clause
6117 Error_Context
cntxt2(runs_on_ref
, "In `runs on' clause");
6118 runs_on_type
= runs_on_ref
->chk_comptype_ref();
6119 // override the scope of the formal parameter list
6121 Scope
*runs_on_scope
= get_runs_on_scope(runs_on_type
);
6122 runs_on_scope
->set_parent_scope(my_scope
);
6123 fp_list
->set_my_scope(runs_on_scope
);
6126 // checking the formal parameter list
6127 fp_list
->chk(asstype
);
6128 // checking of return type
6130 Error_Context
cntxt2(return_type
, "In return type");
6132 return_type
->chk_as_return_type(asstype
== A_FUNCTION_RVAL
,"function");
6134 // decision of startability
6135 is_startable
= runs_on_ref
!= 0;
6136 if (is_startable
&& !fp_list
->get_startability()) is_startable
= false;
6137 if (is_startable
&& return_type
&& return_type
->is_component_internal())
6138 is_startable
= false;
6139 // checking of statement block
6142 // checking the presence of return statements
6143 switch (block
->has_return()) {
6144 case StatementBlock::RS_NO
:
6145 error("The function has return type, but it does not have any return "
6148 case StatementBlock::RS_MAYBE
:
6149 error("The function has return type, but control might leave it "
6150 "without reaching a return statement");
6155 if (!semantic_check_only
) {
6156 fp_list
->set_genname(get_genname());
6157 block
->set_code_section(GovernedSimple::CS_INLINE
);
6159 if (w_attrib_path
) {
6160 w_attrib_path
->chk_global_attrib();
6161 w_attrib_path
->chk_no_qualif();
6162 Ttcn::ExtensionAttributes
* extattrs
= parse_extattributes(w_attrib_path
);
6163 if (extattrs
!= 0) { // NULL means parsing error
6164 size_t num_atrs
= extattrs
->size();
6165 for (size_t i
=0; i
< num_atrs
; ++i
) {
6166 ExtensionAttribute
&ea
= extattrs
->get(i
);
6167 switch (ea
.get_type()) {
6168 case ExtensionAttribute::PROTOTYPE
: {
6169 if (get_prototype() != Def_Function_Base::PROTOTYPE_NONE
) {
6170 ea
.error("Duplicate attribute `prototype'");
6172 Def_Function_Base::prototype_t proto
= ea
.get_proto();
6173 set_prototype(proto
);
6176 case ExtensionAttribute::ANYTYPELIST
: // ignore it
6177 case ExtensionAttribute::NONE
: // erroneous, do not issue an error
6180 case ExtensionAttribute::TRANSPARENT
:
6184 case ExtensionAttribute::ENCODE
:
6185 case ExtensionAttribute::DECODE
:
6186 case ExtensionAttribute::ERRORBEHAVIOR
:
6187 case ExtensionAttribute::PRINTING
:
6188 ea
.error("Extension attribute 'encode', 'decode', 'errorbehavior'"
6189 " or 'printing' can only be applied to external functions");
6192 default: // complain
6193 ea
.error("Function definition can only have the 'prototype'"
6194 " extension attribute");
6204 bool Def_Function::chk_startable()
6206 if (!checked
) chk();
6207 if (is_startable
) return true;
6208 if (!runs_on_ref
) error("Function `%s' cannot be started on a parallel "
6209 "test component because it does not have `runs on' clause",
6210 get_fullname().c_str());
6211 fp_list
->chk_startability("Function", get_fullname().c_str());
6212 if (return_type
&& return_type
->is_component_internal()) {
6213 map
<Type
*,void> type_chain
;
6214 char* err_str
= mprintf("the return type or embedded in the return type "
6215 "of function `%s' if it is started on a parallel test component",
6216 get_fullname().c_str());
6217 return_type
->chk_component_internal(type_chain
, err_str
);
6223 void Def_Function::generate_code(output_struct
*target
, bool)
6225 transparency_holder
glass(*this);
6226 const string
& t_genname
= get_genname();
6227 const char *genname_str
= t_genname
.c_str();
6228 const char *dispname_str
= id
->get_dispname().c_str();
6229 string return_type_name
;
6232 return_type_name
= "void";
6234 case A_FUNCTION_RVAL
:
6235 return_type_name
= return_type
->get_genname_value(my_scope
);
6237 case A_FUNCTION_RTEMP
:
6238 return_type_name
= return_type
->get_genname_template(my_scope
);
6241 FATAL_ERROR("Def_Function::generate_code()");
6243 const char *return_type_str
= return_type_name
.c_str();
6245 // assemble the function body first (this also determines which parameters
6247 char* body
= create_location_object(memptystr(), "FUNCTION", dispname_str
);
6248 if (!enable_set_bound_out_param
)
6249 body
= fp_list
->generate_code_set_unbound(body
); // conform the standard out parameter is unbound
6250 body
= fp_list
->generate_shadow_objects(body
);
6251 if (debugger_active
) {
6252 body
= generate_code_debugger_function_init(body
, this);
6254 body
= block
->generate_code(body
);
6255 // smart formal parameter list (names of unused parameters are omitted)
6256 char *formal_par_list
= fp_list
->generate_code(memptystr());
6257 fp_list
->generate_code_defval(target
);
6258 // function prototype
6259 target
->header
.function_prototypes
=
6260 mputprintf(target
->header
.function_prototypes
, "extern %s %s(%s);\n",
6261 return_type_str
, genname_str
, formal_par_list
);
6264 target
->source
.function_bodies
= mputprintf(target
->source
.function_bodies
,
6268 "}\n\n", return_type_str
, genname_str
, formal_par_list
, body
);
6269 Free(formal_par_list
);
6273 size_t nof_fps
= fp_list
->get_nof_fps();
6274 // use the full list of formal parameters here (since they are all logged)
6275 char *full_formal_par_list
= fp_list
->generate_code(memptystr(), nof_fps
);
6276 // starter function (stub)
6277 // function prototype
6278 target
->header
.function_prototypes
=
6279 mputprintf(target
->header
.function_prototypes
,
6280 "extern void start_%s(const COMPONENT& component_reference%s%s);\n",
6281 genname_str
, nof_fps
>0?", ":"", full_formal_par_list
);
6283 body
= mprintf("void start_%s(const COMPONENT& component_reference%s"
6286 "TTCN_Logger::begin_event(TTCN_Logger::PARALLEL_PTC);\n"
6287 "TTCN_Logger::log_event_str(\"Starting function %s(\");\n",
6288 genname_str
, nof_fps
>0?", ":"", full_formal_par_list
, dispname_str
);
6289 for (size_t i
= 0; i
< nof_fps
; i
++) {
6290 if (i
> 0) body
= mputstr(body
,
6291 "TTCN_Logger::log_event_str(\", \");\n");
6292 body
= mputprintf(body
, "%s.log();\n",
6293 fp_list
->get_fp_byIndex(i
)->get_reference_name(my_scope
).c_str());
6295 body
= mputprintf(body
,
6296 "TTCN_Logger::log_event_str(\") on component \");\n"
6297 "component_reference.log();\n"
6298 "TTCN_Logger::log_char('.');\n"
6299 "TTCN_Logger::end_event();\n"
6300 "Text_Buf text_buf;\n"
6301 "TTCN_Runtime::prepare_start_component(component_reference, "
6302 "\"%s\", \"%s\", text_buf);\n",
6303 my_scope
->get_scope_mod()->get_modid().get_dispname().c_str(),
6305 for (size_t i
= 0; i
< nof_fps
; i
++) {
6306 body
= mputprintf(body
, "%s.encode_text(text_buf);\n",
6307 fp_list
->get_fp_byIndex(i
)->get_reference_name(my_scope
).c_str());
6309 body
= mputstr(body
, "TTCN_Runtime::send_start_component(text_buf);\n"
6311 target
->source
.function_bodies
= mputstr(target
->source
.function_bodies
,
6315 // an entry in start_ptc_function
6316 body
= mprintf("if (!strcmp(function_name, \"%s\")) {\n",
6319 body
= fp_list
->generate_code_object(body
, "", ' ');
6320 for (size_t i
= 0; i
< nof_fps
; i
++) {
6321 body
= mputprintf(body
, "%s.decode_text(function_arguments);\n",
6322 fp_list
->get_fp_byIndex(i
)->get_reference_name(my_scope
).c_str());
6324 body
= mputprintf(body
,
6325 "TTCN_Logger::begin_event(TTCN_Logger::PARALLEL_PTC);\n"
6326 "TTCN_Logger::log_event_str(\"Starting function %s(\");\n",
6328 for (size_t i
= 0; i
< nof_fps
; i
++) {
6329 if (i
> 0) body
= mputstr(body
,
6330 "TTCN_Logger::log_event_str(\", \");\n");
6331 body
= mputprintf(body
, "%s.log();\n",
6332 fp_list
->get_fp_byIndex(i
)->get_reference_name(my_scope
).c_str());
6334 body
= mputstr(body
, "TTCN_Logger::log_event_str(\").\");\n"
6335 "TTCN_Logger::end_event();\n");
6337 body
= mputprintf(body
,
6338 "TTCN_Logger::log_str(TTCN_Logger::PARALLEL_PTC, \"Starting function "
6339 "%s().\");\n", dispname_str
);
6341 body
= mputstr(body
,
6342 "TTCN_Runtime::function_started(function_arguments);\n");
6343 char *actual_par_list
=
6344 fp_list
->generate_code_actual_parlist(memptystr(), "");
6345 bool return_value_kept
= false;
6346 if (asstype
== A_FUNCTION_RVAL
) {
6347 // the return value is kept only if the function returns a value
6348 // (rather than a template) and the return type has the "done"
6349 // extension attribute
6350 for (Type
*t
= return_type
; ; t
= t
->get_type_refd()) {
6351 if (t
->has_done_attribute()) {
6352 return_value_kept
= true;
6354 } else if (!t
->is_ref()) break;
6357 if (return_value_kept
) {
6358 const string
& return_type_dispname
= return_type
->get_typename();
6359 const char *return_type_dispname_str
= return_type_dispname
.c_str();
6360 body
= mputprintf(body
, "%s ret_val(%s(%s));\n"
6361 "TTCN_Logger::begin_event(TTCN_PARALLEL);\n"
6362 "TTCN_Logger::log_event_str(\"Function %s returned %s : \");\n"
6364 "Text_Buf text_buf;\n"
6365 "TTCN_Runtime::prepare_function_finished(\"%s\", text_buf);\n"
6366 "ret_val.encode_text(text_buf);\n"
6367 "TTCN_Runtime::send_function_finished(text_buf);\n",
6368 return_type_str
, genname_str
, actual_par_list
, dispname_str
,
6369 return_type_dispname_str
, return_type_dispname_str
);
6371 body
= mputprintf(body
, "%s(%s);\n"
6372 "TTCN_Runtime::function_finished(\"%s\");\n",
6373 genname_str
, actual_par_list
, dispname_str
);
6375 Free(actual_par_list
);
6376 body
= mputstr(body
, "return TRUE;\n"
6378 target
->functions
.start
= mputstr(target
->functions
.start
, body
);
6380 Free(full_formal_par_list
);
6383 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
6384 "%s.add_function(\"%s\", (genericfunc_t)&%s, ", get_module_object_name(),
6385 dispname_str
, genname_str
);
6387 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
6388 "(genericfunc_t)&start_%s);\n", genname_str
);
6390 target
->functions
.pre_init
= mputstr(target
->functions
.pre_init
,
6394 void Def_Function::generate_code(CodeGenHelper
& cgh
) {
6395 generate_code(cgh
.get_current_outputstruct());
6398 void Def_Function::dump_internal(unsigned level
) const
6400 DEBUG(level
, "Function: %s", id
->get_dispname().c_str());
6401 DEBUG(level
+ 1, "Parameters:");
6402 fp_list
->dump(level
+ 1);
6404 DEBUG(level
+ 1, "Runs on clause:");
6405 runs_on_ref
->dump(level
+ 2);
6408 DEBUG(level
+ 1, "Return type:");
6409 return_type
->dump(level
+ 2);
6410 if (asstype
== A_FUNCTION_RTEMP
) DEBUG(level
+ 1, "Returns template");
6412 if (prototype
!= PROTOTYPE_NONE
)
6413 DEBUG(level
+ 1, "Prototype: %s", get_prototype_name());
6414 //DEBUG(level + 1, "Statement block:");
6415 block
->dump(level
+ 1);
6418 void Def_Function::set_parent_path(WithAttribPath
* p_path
) {
6419 Def_Function_Base::set_parent_path(p_path
);
6420 block
->set_parent_path(w_attrib_path
);
6423 // =================================
6424 // ===== Def_ExtFunction
6425 // =================================
6427 Def_ExtFunction::~Def_ExtFunction()
6429 delete encoding_options
;
6431 if (NULL
!= json_printing
) {
6432 delete json_printing
;
6436 Def_ExtFunction
*Def_ExtFunction::clone() const
6438 FATAL_ERROR("Def_ExtFunction::clone");
6441 void Def_ExtFunction::set_fullname(const string
& p_fullname
)
6443 Def_Function_Base::set_fullname(p_fullname
);
6444 if (eb_list
) eb_list
->set_fullname(p_fullname
+ ".<errorbehavior_list>");
6447 void Def_ExtFunction::set_encode_parameters(Type::MessageEncodingType_t
6448 p_encoding_type
, string
*p_encoding_options
)
6450 function_type
= EXTFUNC_ENCODE
;
6451 encoding_type
= p_encoding_type
;
6452 delete encoding_options
;
6453 encoding_options
= p_encoding_options
;
6456 void Def_ExtFunction::set_decode_parameters(Type::MessageEncodingType_t
6457 p_encoding_type
, string
*p_encoding_options
)
6459 function_type
= EXTFUNC_DECODE
;
6460 encoding_type
= p_encoding_type
;
6461 delete encoding_options
;
6462 encoding_options
= p_encoding_options
;
6465 void Def_ExtFunction::add_eb_list(Ttcn::ErrorBehaviorList
*p_eb_list
)
6467 if (!p_eb_list
) FATAL_ERROR("Def_ExtFunction::add_eb_list()");
6469 eb_list
->steal_ebs(p_eb_list
);
6472 eb_list
= p_eb_list
;
6473 eb_list
->set_fullname(get_fullname() + ".<errorbehavior_list>");
6477 void Def_ExtFunction::chk_function_type()
6479 switch (function_type
) {
6480 case EXTFUNC_MANUAL
:
6482 eb_list
->error("Attribute `errorbehavior' can only be used together "
6483 "with `encode' or `decode'");
6487 case EXTFUNC_ENCODE
:
6488 switch (prototype
) {
6489 case PROTOTYPE_NONE
:
6490 error("Attribute `encode' cannot be used without `prototype'");
6492 case PROTOTYPE_BACKTRACK
:
6493 case PROTOTYPE_SLIDING
:
6494 error("Attribute `encode' cannot be used with `prototype(%s)'",
6495 get_prototype_name());
6496 default: /* CONVERT and FAST allowed */
6501 if (!input_type
->has_encoding(encoding_type
, encoding_options
)) {
6502 if (Common::Type::CT_CUSTOM
== encoding_type
) {
6503 input_type
->error("Input type `%s' does not support custom encoding '%s'",
6504 input_type
->get_typename().c_str(), encoding_options
->c_str());
6507 input_type
->error("Input type `%s' does not support %s encoding",
6508 input_type
->get_typename().c_str(),
6509 Type::get_encoding_name(encoding_type
));
6513 if (Common::Type::CT_XER
== encoding_type
6514 && input_type
->get_type_refd_last()->is_untagged()) {
6515 // "untagged" on the (toplevel) input type will have no effect.
6516 warning("UNTAGGED encoding attribute is ignored on top-level type");
6518 if (Common::Type::CT_CUSTOM
== encoding_type
) {
6519 if (PROTOTYPE_CONVERT
!= prototype
) {
6520 error("Only `prototype(convert)' is allowed for custom encoding functions");
6523 // let the input type know that this is its encoding function
6524 input_type
->get_type_refd()->set_coding_function(true,
6525 get_genname_from_scope(input_type
->get_type_refd()->get_my_scope()));
6526 // treat this as a manual external function during code generation
6527 function_type
= EXTFUNC_MANUAL
;
6533 if(encoding_type
== Common::Type::CT_TEXT
) { // TEXT encoding supports both octetstring and charstring stream types
6534 Type
*stream_type
= Type::get_stream_type(encoding_type
,0);
6535 Type
*stream_type2
= Type::get_stream_type(encoding_type
,1);
6536 if ( (!stream_type
->is_identical(output_type
)) && (!stream_type2
->is_identical(output_type
)) ) {
6537 output_type
->error("The output type of %s encoding should be `%s' or `%s' "
6538 "instead of `%s'", Type::get_encoding_name(encoding_type
),
6539 stream_type
->get_typename().c_str(),
6540 stream_type2
->get_typename().c_str(),
6541 output_type
->get_typename().c_str());
6544 Type
*stream_type
= Type::get_stream_type(encoding_type
);
6545 if (!stream_type
->is_identical(output_type
)) {
6546 output_type
->error("The output type of %s encoding should be `%s' "
6547 "instead of `%s'", Type::get_encoding_name(encoding_type
),
6548 stream_type
->get_typename().c_str(),
6549 output_type
->get_typename().c_str());
6553 if (eb_list
) eb_list
->chk();
6554 chk_allowed_encode();
6556 case EXTFUNC_DECODE
:
6557 if (prototype
== PROTOTYPE_NONE
) {
6558 error("Attribute `decode' cannot be used without `prototype'");
6561 if(encoding_type
== Common::Type::CT_TEXT
) { // TEXT encoding supports both octetstring and charstring stream types
6562 Type
*stream_type
= Type::get_stream_type(encoding_type
,0);
6563 Type
*stream_type2
= Type::get_stream_type(encoding_type
,1);
6564 if ( (!stream_type
->is_identical(input_type
)) && (!stream_type2
->is_identical(input_type
)) ) {
6565 input_type
->error("The input type of %s decoding should be `%s' or `%s' "
6566 "instead of `%s'", Type::get_encoding_name(encoding_type
),
6567 stream_type
->get_typename().c_str(),
6568 stream_type2
->get_typename().c_str(),
6569 input_type
->get_typename().c_str());
6572 Type
*stream_type
= Type::get_stream_type(encoding_type
);
6573 if (!stream_type
->is_identical(input_type
)) {
6574 input_type
->error("The input type of %s decoding should be `%s' "
6575 "instead of `%s'", Type::get_encoding_name(encoding_type
),
6576 stream_type
->get_typename().c_str(),
6577 input_type
->get_typename().c_str());
6582 if (output_type
&& !output_type
->has_encoding(encoding_type
, encoding_options
)) {
6583 if (Common::Type::CT_CUSTOM
== encoding_type
) {
6584 output_type
->error("Output type `%s' does not support custom encoding '%s'",
6585 output_type
->get_typename().c_str(), encoding_options
->c_str());
6588 output_type
->error("Output type `%s' does not support %s encoding",
6589 output_type
->get_typename().c_str(),
6590 Type::get_encoding_name(encoding_type
));
6594 if (Common::Type::CT_CUSTOM
== encoding_type
) {
6595 if (PROTOTYPE_SLIDING
!= prototype
) {
6596 error("Only `prototype(sliding)' is allowed for custom decoding functions");
6598 else if (output_type
) {
6599 // let the output type know that this is its decoding function
6600 output_type
->get_type_refd()->set_coding_function(false,
6601 get_genname_from_scope(output_type
->get_type_refd()->get_my_scope()));
6602 // treat this as a manual external function during code generation
6603 function_type
= EXTFUNC_MANUAL
;
6607 if (eb_list
) eb_list
->chk();
6608 chk_allowed_encode();
6611 FATAL_ERROR("Def_ExtFunction::chk()");
6615 void Def_ExtFunction::chk_allowed_encode()
6617 switch (encoding_type
) {
6619 if (enable_ber()) return; // ok
6622 if (enable_raw()) return; // ok
6625 if (enable_text()) return; // ok
6628 if (enable_xer()) return; // ok
6631 if (enable_per()) return; // ok?
6634 if (enable_json()) return;
6636 case Type::CT_CUSTOM
:
6637 return; // cannot be disabled
6639 FATAL_ERROR("Def_ExtFunction::chk_allowed_encode");
6643 error("%s encoding is disallowed by license or commandline options",
6644 Type::get_encoding_name(encoding_type
));
6647 void Def_ExtFunction::chk()
6649 if (checked
) return;
6651 Error_Context
cntxt(this, "In external function definition `%s'",
6652 id
->get_dispname().c_str());
6653 fp_list
->chk(asstype
);
6655 Error_Context
cntxt2(return_type
, "In return type");
6657 return_type
->chk_as_return_type(asstype
== A_EXT_FUNCTION_RVAL
,
6658 "external function");
6660 if (!semantic_check_only
) fp_list
->set_genname(get_genname());
6661 if (w_attrib_path
) {
6662 w_attrib_path
->chk_global_attrib();
6663 w_attrib_path
->chk_no_qualif();
6664 const Ttcn::ExtensionAttributes
* extattrs
= parse_extattributes(w_attrib_path
);
6665 if (extattrs
!= 0) {
6666 size_t num_atrs
= extattrs
->size();
6667 for (size_t i
=0; i
< num_atrs
; ++i
) {
6668 ExtensionAttribute
&ea
= extattrs
->get(i
);
6669 switch (ea
.get_type()) {
6670 case ExtensionAttribute::PROTOTYPE
: {
6671 if (get_prototype() != Def_Function_Base::PROTOTYPE_NONE
) {
6672 ea
.error("Duplicate attribute `prototype'");
6674 Def_Function_Base::prototype_t proto
= ea
.get_proto();
6675 set_prototype(proto
);
6678 case ExtensionAttribute::ENCODE
: {
6679 switch (get_function_type()) {
6680 case Def_ExtFunction::EXTFUNC_MANUAL
:
6682 case Def_ExtFunction::EXTFUNC_ENCODE
: {
6683 ea
.error("Duplicate attribute `encode'");
6685 case Def_ExtFunction::EXTFUNC_DECODE
: {
6686 ea
.error("Attributes `decode' and `encode' "
6687 "cannot be used at the same time");
6690 FATAL_ERROR("coding_attrib_parse(): invalid external function type");
6692 Type::MessageEncodingType_t et
;
6694 ea
.get_encdec_parameters(et
, opt
);
6695 set_encode_parameters(et
, opt
);
6698 case ExtensionAttribute::ERRORBEHAVIOR
: {
6699 add_eb_list(ea
.get_eb_list());
6702 case ExtensionAttribute::DECODE
: {
6703 switch (get_function_type()) {
6704 case Def_ExtFunction::EXTFUNC_MANUAL
:
6706 case Def_ExtFunction::EXTFUNC_ENCODE
: {
6707 ea
.error("Attributes `encode' and `decode' "
6708 "cannot be used at the same time");
6710 case Def_ExtFunction::EXTFUNC_DECODE
: {
6711 ea
.error("Duplicate attribute `decode'");
6714 FATAL_ERROR("coding_attrib_parse(): invalid external function type");
6716 Type::MessageEncodingType_t et
;
6718 ea
.get_encdec_parameters(et
, opt
);
6719 set_decode_parameters(et
, opt
);
6722 case ExtensionAttribute::PRINTING
: {
6723 json_printing
= ea
.get_printing();
6726 case ExtensionAttribute::ANYTYPELIST
:
6727 // ignore, because we can't distinguish between a local
6728 // "extension anytype" (which is bogus) and an inherited one
6729 // (which was meant for a type definition)
6732 case ExtensionAttribute::NONE
:
6733 // Ignore, do not issue "wrong type" error
6738 "Only the following extension attributes may be applied to "
6739 "external functions: 'prototype', 'encode', 'decode', 'errorbehavior'");
6747 chk_function_type();
6749 if (NULL
!= json_printing
&& (EXTFUNC_ENCODE
!= function_type
||
6750 Type::CT_JSON
!= encoding_type
)) {
6751 error("Attribute 'printing' is only allowed for JSON encoding functions.");
6755 char *Def_ExtFunction::generate_code_encode(char *str
)
6757 const char *function_name
= id
->get_dispname().c_str();
6758 const char *first_par_name
=
6759 fp_list
->get_fp_byIndex(0)->get_id().get_name().c_str();
6760 // producing debug printout of the input PDU
6761 str
= mputprintf(str
,
6763 "// written by %s in " __FILE__
" at %d\n"
6765 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6766 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6767 "TTCN_Logger::log_event_str(\"%s(): Encoding %s: \");\n"
6769 "TTCN_Logger::end_event();\n"
6772 , __FUNCTION__
, __LINE__
6774 , function_name
, input_type
->get_typename().c_str(), first_par_name
);
6775 // setting error behavior
6776 if (eb_list
) str
= eb_list
->generate_code(str
);
6777 else str
= mputstr(str
, "TTCN_EncDec::set_error_behavior("
6778 "TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);\n");
6779 // encoding PDU into the buffer
6780 str
= mputstr(str
, "TTCN_Buffer ttcn_buffer;\n");
6781 str
= mputprintf(str
, "%s.encode(%s_descr_, ttcn_buffer, TTCN_EncDec::CT_%s",
6783 input_type
->get_genname_typedescriptor(my_scope
).c_str(),
6784 Type::get_encoding_name(encoding_type
));
6785 if (encoding_type
== Type::CT_JSON
) {
6786 if (json_printing
!= NULL
) {
6787 str
= json_printing
->generate_code(str
);
6789 str
= mputstr(str
, ", 0");
6792 if (encoding_options
) str
= mputprintf(str
, ", %s",
6793 encoding_options
->c_str());
6794 str
= mputstr(str
, ");\n");
6795 const char *result_name
;
6796 switch (prototype
) {
6797 case PROTOTYPE_CONVERT
:
6798 result_name
= "ret_val";
6799 // creating a local variable for the result stream
6800 str
= mputprintf(str
, "%s ret_val;\n",
6801 output_type
->get_genname_value(my_scope
).c_str());
6803 case PROTOTYPE_FAST
:
6804 result_name
= fp_list
->get_fp_byIndex(1)->get_id().get_name().c_str();
6807 FATAL_ERROR("Def_ExtFunction::generate_code_encode()");
6810 // taking the result from the buffer and producing debug printout
6811 str
= mputprintf(str
, "ttcn_buffer.get_string(%s);\n"
6812 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6813 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6814 "TTCN_Logger::log_event_str(\"%s(): Stream after encoding: \");\n"
6816 "TTCN_Logger::end_event();\n"
6817 "}\n", result_name
, function_name
, result_name
);
6818 // returning the result stream if necessary
6819 if (prototype
== PROTOTYPE_CONVERT
) {
6820 if (debugger_active
) {
6822 "ttcn3_debugger.set_return_value((TTCN_Logger::begin_event_log2str(), "
6823 "ret_val.log(), TTCN_Logger::end_event_log2str()));\n");
6825 str
= mputstr(str
, "return ret_val;\n");
6830 char *Def_ExtFunction::generate_code_decode(char *str
)
6832 const char *function_name
= id
->get_dispname().c_str();
6833 const char *first_par_name
=
6834 fp_list
->get_fp_byIndex(0)->get_id().get_name().c_str();
6835 // producing debug printout of the input stream
6836 str
= mputprintf(str
,
6838 "// written by %s in " __FILE__
" at %d\n"
6840 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6841 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6842 "TTCN_Logger::log_event_str(\"%s(): Stream before decoding: \");\n"
6844 "TTCN_Logger::end_event();\n"
6847 , __FUNCTION__
, __LINE__
6849 , function_name
, first_par_name
);
6850 // setting error behavior
6851 if (eb_list
) str
= eb_list
->generate_code(str
);
6852 else if (prototype
== PROTOTYPE_BACKTRACK
|| prototype
== PROTOTYPE_SLIDING
) {
6853 str
= mputstr(str
, "TTCN_EncDec::set_error_behavior("
6854 "TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);\n");
6855 } else str
= mputstr(str
, "TTCN_EncDec::set_error_behavior("
6856 "TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);\n");
6857 // creating a buffer from the input stream
6858 str
= mputprintf(str
, "TTCN_EncDec::clear_error();\n"
6859 "TTCN_Buffer ttcn_buffer(%s);\n", first_par_name
);
6860 const char *result_name
;
6861 if (prototype
== PROTOTYPE_CONVERT
) {
6862 // creating a local variable for the result
6863 str
= mputprintf(str
, "%s ret_val;\n",
6864 output_type
->get_genname_value(my_scope
).c_str());
6865 result_name
= "ret_val";
6867 result_name
= fp_list
->get_fp_byIndex(1)->get_id().get_name().c_str();
6869 if(encoding_type
==Type::CT_TEXT
){
6870 str
= mputprintf(str
,
6871 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6872 " TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_LOG_MATCHING, TTCN_EncDec::EB_WARNING);\n"
6875 str
= mputprintf(str
, "%s.decode(%s_descr_, ttcn_buffer, "
6876 "TTCN_EncDec::CT_%s", result_name
,
6877 output_type
->get_genname_typedescriptor(my_scope
).c_str(),
6878 Type::get_encoding_name(encoding_type
));
6879 if (encoding_options
) str
= mputprintf(str
, ", %s",
6880 encoding_options
->c_str());
6881 str
= mputstr(str
, ");\n");
6882 // producing debug printout of the result PDU
6883 str
= mputprintf(str
,
6884 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6885 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6886 "TTCN_Logger::log_event_str(\"%s(): Decoded %s: \");\n"
6888 "TTCN_Logger::end_event();\n"
6889 "}\n", function_name
, output_type
->get_typename().c_str(), result_name
);
6890 if (prototype
!= PROTOTYPE_SLIDING
) {
6891 // checking for remaining data in the buffer if decoding was successful
6892 str
= mputprintf(str
, "if (TTCN_EncDec::get_last_error_type() == "
6893 "TTCN_EncDec::ET_NONE) {\n"
6894 "if (ttcn_buffer.get_pos() < ttcn_buffer.get_len()-1 && "
6895 "TTCN_Logger::log_this_event(TTCN_WARNING)) {\n"
6896 "ttcn_buffer.cut();\n"
6897 "%s remaining_stream;\n"
6898 "ttcn_buffer.get_string(remaining_stream);\n"
6899 "TTCN_Logger::begin_event(TTCN_WARNING);\n"
6900 "TTCN_Logger::log_event_str(\"%s(): Warning: Data remained at the end "
6901 "of the stream after successful decoding: \");\n"
6902 "remaining_stream.log();\n"
6903 "TTCN_Logger::end_event();\n"
6904 "}\n", input_type
->get_genname_value(my_scope
).c_str(), function_name
);
6905 // closing the block and returning the appropriate result or status code
6906 if (prototype
== PROTOTYPE_BACKTRACK
) {
6907 if (debugger_active
) {
6908 str
= mputstr(str
, "ttcn3_debugger.set_return_value(\"0\");\n");
6913 if (debugger_active
) {
6914 str
= mputstr(str
, "ttcn3_debugger.set_return_value(\"1\");\n");
6920 str
= mputstr(str
, "}\n");
6921 if (prototype
== PROTOTYPE_CONVERT
) {
6922 if (debugger_active
) {
6924 "ttcn3_debugger.set_return_value((TTCN_Logger::begin_event_log2str(), "
6925 "ret_val.log(), TTCN_Logger::end_event_log2str()));\n");
6927 str
= mputstr(str
, "return ret_val;\n");
6931 // result handling and debug printout for sliding decoders
6932 str
= mputprintf(str
, "switch (TTCN_EncDec::get_last_error_type()) {\n"
6933 "case TTCN_EncDec::ET_NONE:\n"
6934 // TTCN_Buffer::get_string will call OCTETSTRING::clean_up()
6935 "ttcn_buffer.cut();\n"
6936 "ttcn_buffer.get_string(%s);\n"
6937 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6938 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6939 "TTCN_Logger::log_event_str(\"%s(): Stream after decoding: \");\n"
6941 "TTCN_Logger::end_event();\n"
6944 "case TTCN_EncDec::ET_INCOMPL_MSG:\n"
6945 "case TTCN_EncDec::ET_LEN_ERR:\n"
6949 "}\n", first_par_name
, function_name
, first_par_name
,
6950 debugger_active
? "ttcn3_debugger.set_return_value(\"0\");\n" : "",
6951 debugger_active
? "ttcn3_debugger.set_return_value(\"2\");\n" : "",
6952 debugger_active
? "ttcn3_debugger.set_return_value(\"1\");\n" : "");
6957 void Def_ExtFunction::generate_code(output_struct
*target
, bool)
6959 const string
& t_genname
= get_genname();
6960 const char *genname_str
= t_genname
.c_str();
6961 string return_type_name
;
6963 case A_EXT_FUNCTION
:
6964 return_type_name
= "void";
6966 case A_EXT_FUNCTION_RVAL
:
6967 return_type_name
= return_type
->get_genname_value(my_scope
);
6969 case A_EXT_FUNCTION_RTEMP
:
6970 return_type_name
= return_type
->get_genname_template(my_scope
);
6973 FATAL_ERROR("Def_ExtFunction::generate_code()");
6975 const char *return_type_str
= return_type_name
.c_str();
6976 char *formal_par_list
= fp_list
->generate_code(memptystr(), fp_list
->get_nof_fps());
6977 fp_list
->generate_code_defval(target
);
6978 // function prototype
6979 target
->header
.function_prototypes
=
6980 mputprintf(target
->header
.function_prototypes
, "extern %s %s(%s);\n",
6981 return_type_str
, genname_str
, formal_par_list
);
6983 if (function_type
!= EXTFUNC_MANUAL
) {
6984 // function body written by the compiler
6987 body
= mprintf("// written by %s in " __FILE__
" at %d\n"
6988 , __FUNCTION__
, __LINE__
);
6990 body
= mputprintf(body
,
6993 , return_type_str
, genname_str
, formal_par_list
);
6994 if (debugger_active
) {
6995 body
= generate_code_debugger_function_init(body
, this);
6997 switch (function_type
) {
6998 case EXTFUNC_ENCODE
:
6999 body
= generate_code_encode(body
);
7001 case EXTFUNC_DECODE
:
7002 body
= generate_code_decode(body
);
7005 FATAL_ERROR("Def_ExtFunction::generate_code()");
7007 body
= mputstr(body
, "}\n\n");
7008 target
->source
.function_bodies
= mputstr(target
->source
.function_bodies
,
7013 Free(formal_par_list
);
7015 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
7016 "%s.add_function(\"%s\", (genericfunc_t)&%s, NULL);\n",
7017 get_module_object_name(), id
->get_dispname().c_str(), genname_str
);
7020 void Def_ExtFunction::generate_code(CodeGenHelper
& cgh
) {
7021 generate_code(cgh
.get_current_outputstruct());
7024 void Def_ExtFunction::dump_internal(unsigned level
) const
7026 DEBUG(level
, "External function: %s", id
->get_dispname().c_str());
7027 DEBUG(level
+ 1, "Parameters:");
7028 fp_list
->dump(level
+ 2);
7030 DEBUG(level
+ 1, "Return type:");
7031 return_type
->dump(level
+ 2);
7032 if(asstype
== A_EXT_FUNCTION_RTEMP
) DEBUG(level
+ 1, "Returns template");
7034 if (prototype
!= PROTOTYPE_NONE
)
7035 DEBUG(level
+ 1, "Prototype: %s", get_prototype_name());
7036 if (function_type
!= EXTFUNC_MANUAL
) {
7037 DEBUG(level
+ 1, "Automatically generated: %s",
7038 function_type
== EXTFUNC_ENCODE
? "encoder" : "decoder");
7039 DEBUG(level
+ 2, "Encoding type: %s",
7040 Type::get_encoding_name(encoding_type
));
7041 if (encoding_options
)
7042 DEBUG(level
+ 2, "Encoding options: %s", encoding_options
->c_str());
7044 if (eb_list
) eb_list
->dump(level
+ 1);
7047 void Def_ExtFunction::generate_json_schema_ref(map
<Type
*, JSON_Tokenizer
>& json_refs
)
7049 // only do anything if this is a JSON encoding or decoding function
7050 if (encoding_type
== Type::CT_JSON
&&
7051 (function_type
== EXTFUNC_ENCODE
|| function_type
== EXTFUNC_DECODE
)) {
7052 // retrieve the encoded type
7054 if (function_type
== EXTFUNC_ENCODE
) {
7055 // for encoding functions it's always the first parameter
7056 type
= fp_list
->get_fp_byIndex(0)->get_Type();
7058 // for decoding functions it depends on the prototype
7059 switch (prototype
) {
7060 case PROTOTYPE_CONVERT
:
7063 case PROTOTYPE_FAST
:
7064 case PROTOTYPE_BACKTRACK
:
7065 case PROTOTYPE_SLIDING
:
7066 type
= fp_list
->get_fp_byIndex(1)->get_Type();
7069 FATAL_ERROR("Def_ExtFunction::generate_json_schema_ref");
7073 // step over the type reference created for this function
7074 type
= type
->get_type_refd();
7076 JSON_Tokenizer
* json
= NULL
;
7077 if (json_refs
.has_key(type
)) {
7078 // the schema segment containing the type's reference already exists
7079 json
= json_refs
[type
];
7081 // the schema segment doesn't exist yet, create it and insert the reference
7082 json
= new JSON_Tokenizer
;
7083 json_refs
.add(type
, json
);
7084 type
->generate_json_schema_ref(*json
);
7087 // insert a property to specify which function this is (encoding or decoding)
7088 json
->put_next_token(JSON_TOKEN_NAME
, (function_type
== EXTFUNC_ENCODE
) ?
7089 "encoding" : "decoding");
7091 // place the function's info in an object
7092 json
->put_next_token(JSON_TOKEN_OBJECT_START
);
7094 // insert information related to the function's prototype in an array
7095 json
->put_next_token(JSON_TOKEN_NAME
, "prototype");
7096 json
->put_next_token(JSON_TOKEN_ARRAY_START
);
7098 // 1st element: external function prototype name (as string)
7100 case PROTOTYPE_CONVERT
:
7101 json
->put_next_token(JSON_TOKEN_STRING
, "\"convert\"");
7103 case PROTOTYPE_FAST
:
7104 json
->put_next_token(JSON_TOKEN_STRING
, "\"fast\"");
7106 case PROTOTYPE_BACKTRACK
:
7107 json
->put_next_token(JSON_TOKEN_STRING
, "\"backtrack\"");
7109 case PROTOTYPE_SLIDING
:
7110 json
->put_next_token(JSON_TOKEN_STRING
, "\"sliding\"");
7113 FATAL_ERROR("Def_ExtFunction::generate_json_schema_ref");
7116 // 2nd element: external function name
7117 char* func_name_str
= mprintf("\"%s\"", id
->get_dispname().c_str());
7118 json
->put_next_token(JSON_TOKEN_STRING
, func_name_str
);
7119 Free(func_name_str
);
7121 // the rest of the elements contain the names of the function's parameters (1 or 2)
7122 for (size_t i
= 0; i
< fp_list
->get_nof_fps(); ++i
) {
7123 char* param_str
= mprintf("\"%s\"",
7124 fp_list
->get_fp_byIndex(i
)->get_id().get_dispname().c_str());
7125 json
->put_next_token(JSON_TOKEN_STRING
, param_str
);
7129 // end of the prototype's array
7130 json
->put_next_token(JSON_TOKEN_ARRAY_END
);
7132 // insert error behavior data
7133 if (eb_list
!= NULL
) {
7134 json
->put_next_token(JSON_TOKEN_NAME
, "errorBehavior");
7135 json
->put_next_token(JSON_TOKEN_OBJECT_START
);
7137 // add each error behavior modification as a property
7138 for (size_t i
= 0; i
< eb_list
->get_nof_ebs(); ++i
) {
7139 ErrorBehaviorSetting
* eb
= eb_list
->get_ebs_byIndex(i
);
7140 json
->put_next_token(JSON_TOKEN_NAME
, eb
->get_error_type().c_str());
7141 char* handling_str
= mprintf("\"%s\"", eb
->get_error_handling().c_str());
7142 json
->put_next_token(JSON_TOKEN_STRING
, handling_str
);
7146 json
->put_next_token(JSON_TOKEN_OBJECT_END
);
7149 // insert printing type
7150 if (json_printing
!= NULL
) {
7151 json
->put_next_token(JSON_TOKEN_NAME
, "printing");
7152 json
->put_next_token(JSON_TOKEN_STRING
,
7153 (json_printing
->get_printing() == PrintingType::PT_PRETTY
) ?
7154 "\"pretty\"" : "\"compact\"");
7157 // end of this function's object
7158 json
->put_next_token(JSON_TOKEN_OBJECT_END
);
7162 // =================================
7163 // ===== Def_Altstep
7164 // =================================
7166 Def_Altstep::Def_Altstep(Identifier
*p_id
, FormalParList
*p_fpl
,
7167 Reference
*p_runs_on_ref
, StatementBlock
*p_sb
,
7169 : Definition(A_ALTSTEP
, p_id
), fp_list(p_fpl
), runs_on_ref(p_runs_on_ref
),
7170 runs_on_type(0), sb(p_sb
), ags(p_ags
)
7172 if (!p_fpl
|| !p_sb
|| !p_ags
)
7173 FATAL_ERROR("Def_Altstep::Def_Altstep()");
7174 fp_list
->set_my_def(this);
7175 sb
->set_my_def(this);
7176 ags
->set_my_def(this);
7177 ags
->set_my_sb(sb
, 0);
7180 Def_Altstep::~Def_Altstep()
7188 Def_Altstep
*Def_Altstep::clone() const
7190 FATAL_ERROR("Def_Altstep::clone");
7193 void Def_Altstep::set_fullname(const string
& p_fullname
)
7195 Definition::set_fullname(p_fullname
);
7196 fp_list
->set_fullname(p_fullname
+ ".<formal_par_list>");
7197 if (runs_on_ref
) runs_on_ref
->set_fullname(p_fullname
+ ".<runs_on_type>");
7198 sb
->set_fullname(p_fullname
+".<block>");
7199 ags
->set_fullname(p_fullname
+ ".<guards>");
7202 void Def_Altstep::set_my_scope(Scope
*p_scope
)
7204 bridgeScope
.set_parent_scope(p_scope
);
7205 bridgeScope
.set_scopeMacro_name(id
->get_dispname());
7207 Definition::set_my_scope(&bridgeScope
);
7208 // the scope of the parameter list is set during checking
7209 if (runs_on_ref
) runs_on_ref
->set_my_scope(&bridgeScope
);
7210 sb
->set_my_scope(fp_list
);
7211 ags
->set_my_scope(sb
);
7214 Type
*Def_Altstep::get_RunsOnType()
7216 if (!checked
) chk();
7217 return runs_on_type
;
7220 FormalParList
*Def_Altstep::get_FormalParList()
7222 if (!checked
) chk();
7226 RunsOnScope
*Def_Altstep::get_runs_on_scope(Type
*comptype
)
7228 Module
*my_module
= dynamic_cast<Module
*>(my_scope
->get_scope_mod());
7229 if (!my_module
) FATAL_ERROR("Def_Altstep::get_runs_on_scope()");
7230 return my_module
->get_runs_on_scope(comptype
);
7233 void Def_Altstep::chk()
7235 if (checked
) return;
7237 Error_Context
cntxt(this, "In altstep definition `%s'",
7238 id
->get_dispname().c_str());
7239 Scope
*parlist_scope
= my_scope
;
7241 Error_Context
cntxt2(runs_on_ref
, "In `runs on' clause");
7242 runs_on_type
= runs_on_ref
->chk_comptype_ref();
7244 Scope
*runs_on_scope
= get_runs_on_scope(runs_on_type
);
7245 runs_on_scope
->set_parent_scope(my_scope
);
7246 parlist_scope
= runs_on_scope
;
7249 fp_list
->set_my_scope(parlist_scope
);
7250 fp_list
->chk(asstype
);
7252 ags
->set_is_altstep();
7253 ags
->set_my_ags(ags
);
7254 ags
->set_my_laic_stmt(ags
, 0);
7256 if (!semantic_check_only
) {
7257 fp_list
->set_genname(get_genname());
7258 sb
->set_code_section(GovernedSimple::CS_INLINE
);
7259 ags
->set_code_section(GovernedSimple::CS_INLINE
);
7261 if (w_attrib_path
) {
7262 w_attrib_path
->chk_global_attrib();
7263 w_attrib_path
->chk_no_qualif();
7267 void Def_Altstep::generate_code(output_struct
*target
, bool)
7269 const string
& t_genname
= get_genname();
7270 const char *genname_str
= t_genname
.c_str();
7271 const char *dispname_str
= id
->get_dispname().c_str();
7273 // function for altstep instance:
7274 // assemble the function body first (this also determines which parameters
7276 char* body
= create_location_object(memptystr(), "ALTSTEP", dispname_str
);
7277 body
= fp_list
->generate_shadow_objects(body
);
7278 if (debugger_active
) {
7279 body
= generate_code_debugger_function_init(body
, this);
7281 body
= sb
->generate_code(body
);
7282 body
= ags
->generate_code_altstep(body
);
7283 // generate a smart formal parameter list (omits unused parameter names)
7284 char *formal_par_list
= fp_list
->generate_code(memptystr());
7285 fp_list
->generate_code_defval(target
);
7287 // function for altstep instance: prototype
7288 target
->header
.function_prototypes
=
7289 mputprintf(target
->header
.function_prototypes
,
7290 "extern alt_status %s_instance(%s);\n", genname_str
, formal_par_list
);
7292 // function for altstep instance: body
7293 target
->source
.function_bodies
= mputprintf(target
->source
.function_bodies
,
7294 "alt_status %s_instance(%s)\n"
7297 "}\n\n", genname_str
, formal_par_list
, body
);
7298 Free(formal_par_list
);
7301 char *actual_par_list
=
7302 fp_list
->generate_code_actual_parlist(memptystr(), "");
7304 // use a full formal parameter list for the rest of the functions
7305 char *full_formal_par_list
= fp_list
->generate_code(memptystr(),
7306 fp_list
->get_nof_fps());
7308 // wrapper function for stand-alone instantiation: prototype
7309 target
->header
.function_prototypes
=
7310 mputprintf(target
->header
.function_prototypes
,
7311 "extern void %s(%s);\n", genname_str
, full_formal_par_list
);
7313 // wrapper function for stand-alone instantiation: body
7314 target
->source
.function_bodies
=
7315 mputprintf(target
->source
.function_bodies
, "void %s(%s)\n"
7318 "boolean block_flag = FALSE;\n"
7319 "alt_status altstep_flag = ALT_UNCHECKED, "
7320 "default_flag = ALT_UNCHECKED;\n"
7322 "TTCN_Snapshot::take_new(block_flag);\n"
7323 "if (altstep_flag != ALT_NO) {\n"
7324 "altstep_flag = %s_instance(%s);\n"
7325 "if (altstep_flag == ALT_YES || altstep_flag == ALT_BREAK) return;\n"
7326 "else if (altstep_flag == ALT_REPEAT) goto altstep_begin;\n"
7328 "if (default_flag != ALT_NO) {\n"
7329 "default_flag = TTCN_Default::try_altsteps();\n"
7330 "if (default_flag == ALT_YES || default_flag == ALT_BREAK) return;\n"
7331 "else if (default_flag == ALT_REPEAT) goto altstep_begin;\n"
7333 "if (altstep_flag == ALT_NO && default_flag == ALT_NO) "
7334 "TTCN_error(\"None of the branches can be chosen in altstep %s.\");\n"
7335 "else block_flag = TRUE;\n"
7337 "}\n\n", genname_str
, full_formal_par_list
, genname_str
, actual_par_list
,
7340 // class for keeping the altstep in the default context
7341 // the class is for internal use, we do not need to publish it in the
7343 char* str
= mprintf("class %s_Default : public Default_Base {\n", genname_str
);
7344 str
= fp_list
->generate_code_object(str
, "par_");
7345 str
= mputprintf(str
, "public:\n"
7347 "alt_status call_altstep();\n"
7348 "};\n\n", genname_str
, full_formal_par_list
);
7349 target
->source
.class_defs
= mputstr(target
->source
.class_defs
, str
);
7351 // member functions of the class
7352 str
= mprintf("%s_Default::%s_Default(%s)\n"
7353 " : Default_Base(\"%s\")", genname_str
, genname_str
, full_formal_par_list
,
7355 for (size_t i
= 0; i
< fp_list
->get_nof_fps(); i
++) {
7356 const char *fp_name_str
=
7357 fp_list
->get_fp_byIndex(i
)->get_id().get_name().c_str();
7358 str
= mputprintf(str
, ", par_%s(%s)", fp_name_str
, fp_name_str
);
7360 str
= mputstr(str
, "\n{\n}\n\n");
7361 char *actual_par_list_prefixed
=
7362 fp_list
->generate_code_actual_parlist(memptystr(), "par_");
7363 str
= mputprintf(str
, "alt_status %s_Default::call_altstep()\n"
7365 "return %s_instance(%s);\n"
7366 "}\n\n", genname_str
, genname_str
, actual_par_list_prefixed
);
7367 Free(actual_par_list_prefixed
);
7368 target
->source
.methods
= mputstr(target
->source
.methods
, str
);
7371 // function for default activation: prototype
7372 target
->header
.function_prototypes
=
7373 mputprintf(target
->header
.function_prototypes
,
7374 "extern Default_Base *activate_%s(%s);\n", genname_str
,
7375 full_formal_par_list
);
7377 // function for default activation: body
7378 str
= mprintf("Default_Base *activate_%s(%s)\n"
7379 "{\n", genname_str
, full_formal_par_list
);
7380 str
= mputprintf(str
, "return new %s_Default(%s);\n"
7381 "}\n\n", genname_str
, actual_par_list
);
7382 target
->source
.function_bodies
= mputstr(target
->source
.function_bodies
,
7386 Free(full_formal_par_list
);
7387 Free(actual_par_list
);
7389 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
7390 "%s.add_altstep(\"%s\", (genericfunc_t)&%s_instance, (genericfunc_t )&activate_%s, "
7391 "(genericfunc_t )&%s);\n", get_module_object_name(), dispname_str
, genname_str
,
7392 genname_str
, genname_str
);
7395 void Def_Altstep::generate_code(CodeGenHelper
& cgh
) {
7396 generate_code(cgh
.get_current_outputstruct());
7399 void Def_Altstep::dump_internal(unsigned level
) const
7401 DEBUG(level
, "Altstep: %s", id
->get_dispname().c_str());
7402 DEBUG(level
+ 1, "Parameters:");
7403 fp_list
->dump(level
+ 1);
7405 DEBUG(level
+ 1, "Runs on clause:");
7406 runs_on_ref
->dump(level
+ 2);
7409 DEBUG(level + 1, "Local definitions:");
7410 sb->dump(level + 2);
7412 DEBUG(level
+ 1, "Guards:");
7413 ags
->dump(level
+ 2);
7416 void Def_Altstep::set_parent_path(WithAttribPath
* p_path
) {
7417 Definition::set_parent_path(p_path
);
7418 sb
->set_parent_path(w_attrib_path
);
7421 // =================================
7422 // ===== Def_Testcase
7423 // =================================
7425 Def_Testcase::Def_Testcase(Identifier
*p_id
, FormalParList
*p_fpl
,
7426 Reference
*p_runs_on_ref
, Reference
*p_system_ref
,
7427 StatementBlock
*p_block
)
7428 : Definition(A_TESTCASE
, p_id
), fp_list(p_fpl
), runs_on_ref(p_runs_on_ref
),
7429 runs_on_type(0), system_ref(p_system_ref
), system_type(0), block(p_block
)
7431 if (!p_fpl
|| !p_runs_on_ref
|| !p_block
)
7432 FATAL_ERROR("Def_Testcase::Def_Testcase()");
7433 fp_list
->set_my_def(this);
7434 block
->set_my_def(this);
7437 Def_Testcase::~Def_Testcase()
7445 Def_Testcase
*Def_Testcase::clone() const
7447 FATAL_ERROR("Def_Testcase::clone");
7450 void Def_Testcase::set_fullname(const string
& p_fullname
)
7452 Definition::set_fullname(p_fullname
);
7453 fp_list
->set_fullname(p_fullname
+ ".<formal_par_list>");
7454 runs_on_ref
->set_fullname(p_fullname
+ ".<runs_on_type>");
7455 if (system_ref
) system_ref
->set_fullname(p_fullname
+ ".<system_type>");
7456 block
->set_fullname(p_fullname
+ ".<statement_block>");
7459 void Def_Testcase::set_my_scope(Scope
*p_scope
)
7461 bridgeScope
.set_parent_scope(p_scope
);
7462 bridgeScope
.set_scopeMacro_name(id
->get_dispname());
7464 Definition::set_my_scope(&bridgeScope
);
7465 // the scope of the parameter list is set during checking
7466 runs_on_ref
->set_my_scope(&bridgeScope
);
7467 if (system_ref
) system_ref
->set_my_scope(&bridgeScope
);
7468 block
->set_my_scope(fp_list
);
7471 Type
*Def_Testcase::get_RunsOnType()
7473 if (!checked
) chk();
7474 return runs_on_type
;
7477 Type
*Def_Testcase::get_SystemType()
7479 if (!checked
) chk();
7483 FormalParList
*Def_Testcase::get_FormalParList()
7485 if (!checked
) chk();
7489 RunsOnScope
*Def_Testcase::get_runs_on_scope(Type
*comptype
)
7491 Module
*my_module
= dynamic_cast<Module
*>(my_scope
->get_scope_mod());
7492 if (!my_module
) FATAL_ERROR("Def_Testcase::get_runs_on_scope()");
7493 return my_module
->get_runs_on_scope(comptype
);
7496 void Def_Testcase::chk()
7498 if (checked
) return;
7500 Error_Context
cntxt(this, "In testcase definition `%s'",
7501 id
->get_dispname().c_str());
7502 Scope
*parlist_scope
= my_scope
;
7504 Error_Context
cntxt2(runs_on_ref
, "In `runs on' clause");
7505 runs_on_type
= runs_on_ref
->chk_comptype_ref();
7507 Scope
*runs_on_scope
= get_runs_on_scope(runs_on_type
);
7508 runs_on_scope
->set_parent_scope(my_scope
);
7509 parlist_scope
= runs_on_scope
;
7513 Error_Context
cntxt2(system_ref
, "In `system' clause");
7514 system_type
= system_ref
->chk_comptype_ref();;
7516 fp_list
->set_my_scope(parlist_scope
);
7517 fp_list
->chk(asstype
);
7519 if (!semantic_check_only
) {
7520 fp_list
->set_genname(get_genname());
7521 block
->set_code_section(GovernedSimple::CS_INLINE
);
7523 if (w_attrib_path
) {
7524 w_attrib_path
->chk_global_attrib();
7525 w_attrib_path
->chk_no_qualif();
7529 void Def_Testcase::generate_code(output_struct
*target
, bool)
7531 const string
& t_genname
= get_genname();
7532 const char *genname_str
= t_genname
.c_str();
7533 const char *dispname_str
= id
->get_dispname().c_str();
7535 // assemble the function body first (this also determines which parameters
7538 // Checking whether the testcase was invoked from another one.
7539 // At this point the location information should refer to the execute()
7540 // statement rather than this testcase.
7541 char* body
= mputstr(memptystr(), "TTCN_Runtime::check_begin_testcase(has_timer, "
7543 body
= create_location_object(body
, "TESTCASE", dispname_str
);
7544 body
= fp_list
->generate_shadow_objects(body
);
7545 body
= mputprintf(body
, "try {\n"
7546 "TTCN_Runtime::begin_testcase(\"%s\", \"%s\", ",
7547 my_scope
->get_scope_mod()->get_modid().get_dispname().c_str(),
7549 ComponentTypeBody
*runs_on_body
= runs_on_type
->get_CompBody();
7550 body
= runs_on_body
->generate_code_comptype_name(body
);
7551 body
= mputstr(body
, ", ");
7553 body
= system_type
->get_CompBody()->generate_code_comptype_name(body
);
7554 else body
= runs_on_body
->generate_code_comptype_name(body
);
7555 body
= mputstr(body
, ", has_timer, timer_value);\n");
7556 if (debugger_active
) {
7557 body
= generate_code_debugger_function_init(body
, this);
7559 body
= block
->generate_code(body
);
7560 body
= mputprintf(body
,
7561 "} catch (const TC_Error& tc_error) {\n"
7562 "} catch (const TC_End& tc_end) {\n"
7563 "TTCN_Logger::log_str(TTCN_FUNCTION, \"Test case %s was stopped.\");\n"
7564 "}\n", dispname_str
);
7565 body
= mputstr(body
, "return TTCN_Runtime::end_testcase();\n");
7567 // smart formal parameter list (names of unused parameters are omitted)
7568 char *formal_par_list
= fp_list
->generate_code(memptystr());
7569 fp_list
->generate_code_defval(target
);
7570 if (fp_list
->get_nof_fps() > 0)
7571 formal_par_list
= mputstr(formal_par_list
, ", ");
7572 formal_par_list
= mputstr(formal_par_list
,
7573 "boolean has_timer, double timer_value");
7575 // function prototype
7576 target
->header
.function_prototypes
=
7577 mputprintf(target
->header
.function_prototypes
,
7578 "extern verdicttype testcase_%s(%s);\n", genname_str
, formal_par_list
);
7581 target
->source
.function_bodies
= mputprintf(target
->source
.function_bodies
,
7582 "verdicttype testcase_%s(%s)\n"
7585 "}\n\n", genname_str
, formal_par_list
, body
);
7586 Free(formal_par_list
);
7589 if (fp_list
->get_nof_fps() == 0) {
7590 // adding to the list of startable testcases
7591 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
7592 "%s.add_testcase_nonpard(\"%s\", testcase_%s);\n",
7593 get_module_object_name(), dispname_str
, genname_str
);
7595 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
7596 "%s.add_testcase_pard(\"%s\", (genericfunc_t)&testcase_%s);\n",
7597 get_module_object_name(), dispname_str
, genname_str
);
7599 // If every formal parameter has a default value, the testcase
7600 // might be callable after all.
7601 bool callable
= true;
7602 for (size_t i
= 0; i
< fp_list
->get_nof_fps(); ++i
) {
7603 FormalPar
*fp
= fp_list
->get_fp_byIndex(i
);
7604 if (!fp
->has_defval()) {
7611 // Write a wrapper, which acts as a no-param testcase
7612 // by calling the parameterized testcase with the default values.
7613 target
->header
.function_prototypes
=
7614 mputprintf(target
->header
.function_prototypes
,
7615 "extern verdicttype testcase_%s_defparams(boolean has_timer, double timer_value);\n",
7617 target
->source
.function_bodies
= mputprintf(target
->source
.function_bodies
,
7618 "verdicttype testcase_%s_defparams(boolean has_timer, double timer_value) {\n"
7619 " return testcase_%s(",
7620 genname_str
, genname_str
);
7622 for (size_t i
= 0; i
< fp_list
->get_nof_fps(); ++i
) {
7623 FormalPar
*fp
= fp_list
->get_fp_byIndex(i
);
7624 ActualPar
*ap
= fp
->get_defval();
7625 switch (ap
->get_selection()) {
7626 case ActualPar::AP_VALUE
:
7627 target
->source
.function_bodies
= mputstr(target
->source
.function_bodies
,
7628 ap
->get_Value()->get_genname_own(my_scope
).c_str());
7630 case ActualPar::AP_TEMPLATE
:
7631 target
->source
.function_bodies
= mputstr(target
->source
.function_bodies
,
7632 ap
->get_TemplateInstance()->get_Template()->get_genname_own(my_scope
).c_str());
7634 case ActualPar::AP_REF
:
7635 target
->source
.function_bodies
= mputstr(target
->source
.function_bodies
,
7636 ap
->get_Ref()->get_refd_assignment()->get_genname_from_scope(my_scope
).c_str());
7638 case ActualPar::AP_DEFAULT
:
7639 // Can't happen. This ActualPar was created by
7640 // Ttcn::FormalPar::chk_actual_par as the default value for
7641 // a FormalPar, and it only ever creates vale, template or ref.
7644 FATAL_ERROR("Def_Testcase::generate_code()");
7647 // always append a comma, because has_timer and timer_value follows
7648 target
->source
.function_bodies
= mputstrn(target
->source
.function_bodies
,
7652 target
->source
.function_bodies
= mputstr(target
->source
.function_bodies
,
7653 "has_timer, timer_value);\n"
7655 // Add the non-parameterized wrapper *after* the parameterized one,
7656 // with the same name. Linear search will always find the first
7657 // (user-visible, parameterized) testcase.
7658 // TTCN_Module::execute_testcase knows that if after a parameterized
7659 // testcase another testcase with the same name follows,
7660 // it's the callable, non-parameterized wrapper.
7662 // TTCN_Module::list_testcases skips parameterized testcases;
7663 // it will now list the non-parameterized wrapper.
7664 target
->functions
.pre_init
= mputprintf(target
->functions
.pre_init
,
7665 "%s.add_testcase_nonpard(\"%s\", testcase_%s_defparams);\n",
7666 get_module_object_name(), dispname_str
, genname_str
);
7668 } // has formal parameters
7671 void Def_Testcase::generate_code(CodeGenHelper
& cgh
) {
7672 generate_code(cgh
.get_current_outputstruct());
7675 void Def_Testcase::dump_internal(unsigned level
) const
7677 DEBUG(level
, "Testcase: %s", id
->get_dispname().c_str());
7678 DEBUG(level
+ 1, "Parameters:");
7679 fp_list
->dump(level
+ 1);
7680 DEBUG(level
+ 1, "Runs on clause:");
7681 runs_on_ref
->dump(level
+ 2);
7683 DEBUG(level
+ 1, "System clause:");
7684 system_ref
->dump(level
+ 2);
7686 DEBUG(level
+ 1, "Statement block:");
7687 block
->dump(level
+ 2);
7690 void Def_Testcase::set_parent_path(WithAttribPath
* p_path
) {
7691 Definition::set_parent_path(p_path
);
7693 block
->set_parent_path(w_attrib_path
);
7696 // =================================
7698 // =================================
7700 FormalPar::FormalPar(asstype_t p_asstype
, Type
*p_type
, Identifier
* p_name
,
7701 TemplateInstance
*p_defval
, bool p_lazy_eval
)
7702 : Definition(p_asstype
, p_name
), type(p_type
), my_parlist(0),
7703 used_as_lvalue(false), template_restriction(TR_NONE
),
7704 lazy_eval(p_lazy_eval
), defval_generated(false), usage_found(false)
7706 switch (p_asstype
) {
7710 case A_PAR_VAL_INOUT
:
7711 case A_PAR_TEMPL_IN
:
7712 case A_PAR_TEMPL_OUT
:
7713 case A_PAR_TEMPL_INOUT
:
7717 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): invalid parameter type");
7720 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): NULL pointer");
7721 type
->set_ownertype(Type::OT_FORMAL_PAR
, this);
7722 defval
.ti
= p_defval
;
7725 FormalPar::FormalPar(asstype_t p_asstype
,
7726 template_restriction_t p_template_restriction
, Type
*p_type
,
7727 Identifier
* p_name
, TemplateInstance
*p_defval
, bool p_lazy_eval
)
7728 : Definition(p_asstype
, p_name
), type(p_type
), my_parlist(0),
7729 used_as_lvalue(false), template_restriction(p_template_restriction
),
7730 lazy_eval(p_lazy_eval
), defval_generated(false), usage_found(false)
7732 switch (p_asstype
) {
7733 case A_PAR_TEMPL_IN
:
7734 case A_PAR_TEMPL_OUT
:
7735 case A_PAR_TEMPL_INOUT
:
7738 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): parameter not template");
7741 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): NULL pointer");
7742 type
->set_ownertype(Type::OT_FORMAL_PAR
, this);
7743 defval
.ti
= p_defval
;
7746 FormalPar::FormalPar(asstype_t p_asstype
, Identifier
* p_name
,
7747 TemplateInstance
*p_defval
)
7748 : Definition(p_asstype
, p_name
), type(0), my_parlist(0),
7749 used_as_lvalue(false), template_restriction(TR_NONE
), lazy_eval(false),
7750 defval_generated(false), usage_found(false)
7752 if (p_asstype
!= A_PAR_TIMER
)
7753 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): invalid parameter type");
7754 defval
.ti
= p_defval
;
7757 FormalPar::~FormalPar()
7760 if (checked
) delete defval
.ap
;
7761 else delete defval
.ti
;
7764 FormalPar
* FormalPar::clone() const
7766 FATAL_ERROR("FormalPar::clone");
7769 void FormalPar::set_fullname(const string
& p_fullname
)
7771 Definition::set_fullname(p_fullname
);
7772 if (type
) type
->set_fullname(p_fullname
+ ".<type>");
7774 if (defval
.ap
) defval
.ap
->set_fullname(p_fullname
+ ".<default_value>");
7776 if (defval
.ti
) defval
.ti
->set_fullname(p_fullname
+ ".<default_value>");
7780 void FormalPar::set_my_scope(Scope
*p_scope
)
7782 Definition::set_my_scope(p_scope
);
7783 if (type
) type
->set_my_scope(p_scope
);
7785 if (defval
.ap
) defval
.ap
->set_my_scope(p_scope
);
7787 if (defval
.ti
) defval
.ti
->set_my_scope(p_scope
);
7791 bool FormalPar::is_local() const
7796 Type
*FormalPar::get_Type()
7798 if (!checked
) chk();
7799 if (!type
) FATAL_ERROR("FormalPar::get_Type()");
7803 void FormalPar::chk()
7805 if (checked
) return;
7807 TemplateInstance
*default_value
= defval
.ti
;
7811 Type
*t
= type
->get_type_refd_last();
7812 // checks for forbidden type <-> parameter combinations
7813 switch (t
->get_typetype()) {
7817 case A_PAR_VAL_INOUT
:
7818 asstype
= A_PAR_PORT
;
7821 error("Port type `%s' cannot be used as %s",
7822 t
->get_fullname().c_str(), get_assname());
7825 case Type::T_SIGNATURE
:
7827 case A_PAR_TEMPL_IN
:
7828 case A_PAR_TEMPL_OUT
:
7829 case A_PAR_TEMPL_INOUT
:
7832 error("Signature `%s' cannot be used as %s",
7833 t
->get_fullname().c_str(), get_assname());
7840 FATAL_ERROR("FormalPar::chk()");
7842 asstype
= A_PAR_VAL_IN
;
7847 } else if (asstype
!= A_PAR_TIMER
) FATAL_ERROR("FormalPar::chk()");
7849 if (default_value
) {
7850 Error_Context
cntxt(default_value
, "In default value");
7851 defval
.ap
= chk_actual_par(default_value
, Type::EXPECTED_STATIC_VALUE
);
7852 delete default_value
;
7853 if (!semantic_check_only
)
7854 defval
.ap
->set_code_section(GovernedSimple::CS_POST_INIT
);
7858 bool FormalPar::has_defval() const
7860 if (checked
) return defval
.ap
!= 0;
7861 else return defval
.ti
!= 0;
7864 bool FormalPar::has_notused_defval() const
7866 if (checked
) FATAL_ERROR("FormalPar::has_notused_defval");
7867 if (!defval
.ti
|| !defval
.ti
->get_Template())
7869 return defval
.ti
->get_Template()->get_templatetype()
7870 == Template::TEMPLATE_NOTUSED
;
7873 ActualPar
*FormalPar::get_defval() const
7875 if (!checked
) FATAL_ERROR("FormalPar::get_defval()");
7879 // Extract the TemplateInstance from an ActualPar.
7880 void FormalPar::set_defval(ActualPar
*defpar
)
7882 // ActualPar::clone() is not implemented, since we need such a function
7883 // only here only for AP_{VALUE,TEMPLATE} parameters. AP_ERROR can also
7884 // happen for Def_Template nodes, but they will be errors later.
7885 // FIXME: This function is Def_Template specific.
7886 if (!defval
.ti
->get_Template() || defval
.ti
->get_Template()
7887 ->get_templatetype() != Template::TEMPLATE_NOTUSED
)
7888 FATAL_ERROR("FormalPar::set_defval()");
7889 TemplateInstance
*reversed_ti
= 0;
7890 switch (defpar
->get_selection()) {
7891 case ActualPar::AP_VALUE
:
7892 reversed_ti
= new TemplateInstance(type
->clone(), 0, new Template
7893 (defpar
->get_Value()->clone())); // Trust the clone().
7895 case ActualPar::AP_TEMPLATE
:
7896 reversed_ti
= defpar
->get_TemplateInstance()->clone();
7898 case ActualPar::AP_ERROR
:
7899 break; // Can happen, but let it go.
7900 case ActualPar::AP_REF
:
7901 case ActualPar::AP_DEFAULT
:
7903 FATAL_ERROR("FormalPar::set_defval()");
7907 reversed_ti
->set_my_scope(get_my_scope());
7908 defval
.ti
= reversed_ti
;
7912 ActualPar
*FormalPar::chk_actual_par(TemplateInstance
*actual_par
,
7913 Type::expected_value_t exp_val
)
7915 if (!checked
) chk();
7919 return chk_actual_par_value(actual_par
, exp_val
);
7921 case A_PAR_VAL_INOUT
:
7922 return chk_actual_par_by_ref(actual_par
, false, exp_val
);
7923 case A_PAR_TEMPL_IN
:
7924 return chk_actual_par_template(actual_par
, exp_val
);
7925 case A_PAR_TEMPL_OUT
:
7926 case A_PAR_TEMPL_INOUT
:
7927 return chk_actual_par_by_ref(actual_par
, true, exp_val
);
7929 return chk_actual_par_timer(actual_par
, exp_val
);
7931 return chk_actual_par_port(actual_par
, exp_val
);
7933 FATAL_ERROR("FormalPar::chk_actual_par()");
7935 return 0; // to avoid warnings
7938 ActualPar
*FormalPar::chk_actual_par_value(TemplateInstance
*actual_par
,
7939 Type::expected_value_t exp_val
)
7941 actual_par
->chk_Type(type
);
7942 Ref_base
*derived_ref
= actual_par
->get_DerivedRef();
7944 derived_ref
->error("An in-line modified template cannot be used as %s",
7946 actual_par
->chk_DerivedRef(type
);
7948 Template
*ap_template
= actual_par
->get_Template();
7949 if (ap_template
->is_Value()) {
7950 Value
*v
= ap_template
->get_Value();
7951 v
->set_my_governor(type
);
7952 type
->chk_this_value_ref(v
);
7953 type
->chk_this_value(v
, 0, exp_val
, INCOMPLETE_NOT_ALLOWED
,
7954 OMIT_NOT_ALLOWED
, SUB_CHK
);
7955 return new ActualPar(v
);
7957 actual_par
->error("A specific value without matching symbols "
7958 "was expected for a %s", get_assname());
7959 return new ActualPar();
7963 static void chk_defpar_value(const Value
* v
)
7965 Common::Reference
*vref
= v
->get_reference();
7966 Common::Assignment
*ass2
= vref
->get_refd_assignment();
7968 Scope
*scope
= ass2
->get_my_scope();
7969 ComponentTypeBody
*ctb
= dynamic_cast<ComponentTypeBody
*>(scope
);
7970 if (ctb
) { // this is a component variable
7971 v
->error("default value cannot refer to"
7972 " a template field of the component in the `runs on' clause");
7976 static void chk_defpar_template(const Template
*body
,
7977 Type::expected_value_t exp_val
)
7979 switch (body
->get_templatetype()) {
7980 case Template::TEMPLATE_ERROR
:
7981 break; // could be erroneous in the source; skip it
7982 case Template::TEMPLATE_NOTUSED
:
7983 case Template::OMIT_VALUE
:
7984 case Template::ANY_VALUE
:
7985 case Template::ANY_OR_OMIT
:
7986 break; // acceptable (?)
7987 case Template::TEMPLATE_INVOKE
: // calling a function is not acceptable
7988 body
->error("default value can not be a function invocation");
7990 case Template::VALUE_RANGE
: {
7991 ValueRange
*range
= body
->get_value_range();
7992 Value
*low
= range
->get_min_v();
7993 Type::typetype_t tt_low
= low
->get_expr_returntype(exp_val
);
7994 Value
*high
= range
->get_max_v();
7995 Type::typetype_t tt_high
= high
->get_expr_returntype(exp_val
);
7996 if (tt_low
== tt_high
) break;
7999 case Template::BSTR_PATTERN
:
8000 case Template::HSTR_PATTERN
:
8001 case Template::OSTR_PATTERN
:
8002 case Template::CSTR_PATTERN
:
8003 case Template::USTR_PATTERN
:
8004 break; // should be acceptable in all cases (if only fixed strings possible)
8006 case Template::SPECIFIC_VALUE
: {
8007 Common::Value
*v
= body
->get_specific_value();
8008 if (v
->get_valuetype() == Value::V_REFD
) chk_defpar_value(v
);
8011 case Template::ALL_FROM
:
8012 case Template::VALUE_LIST_ALL_FROM
:
8013 FATAL_ERROR("should have been flattened");
8015 case Template::SUPERSET_MATCH
:
8016 case Template::SUBSET_MATCH
:
8017 case Template::PERMUTATION_MATCH
:
8018 case Template::TEMPLATE_LIST
:
8019 case Template::COMPLEMENTED_LIST
:
8020 case Template::VALUE_LIST
: {
8021 // in template charstring par := charstring : ("foo", "bar", "baz")
8022 size_t num
= body
->get_nof_comps();
8023 for (size_t i
= 0; i
< num
; ++i
) {
8024 const Template
*tpl
= body
->get_temp_byIndex(i
);
8025 chk_defpar_template(tpl
, exp_val
);
8029 case Template::NAMED_TEMPLATE_LIST
: {
8030 size_t num
= body
->get_nof_comps();
8031 for (size_t i
= 0; i
< num
; ++i
) {
8032 const NamedTemplate
*nt
= body
->get_namedtemp_byIndex(i
);
8033 const Template
*tpl
= nt
->get_template();
8034 chk_defpar_template(tpl
, exp_val
);
8038 case Template::INDEXED_TEMPLATE_LIST
: {
8039 size_t num
= body
->get_nof_comps();
8040 for (size_t i
= 0; i
< num
; ++i
) {
8041 const IndexedTemplate
*it
= body
->get_indexedtemp_byIndex(i
);
8042 const Template
*tpl
= it
->get_template();
8043 chk_defpar_template(tpl
, exp_val
);
8047 case Template::TEMPLATE_REFD
: {
8048 Ref_base
*ref
= body
->get_reference();
8050 Ttcn::ActualParList
*aplist
= ref
->get_parlist();
8052 size_t num
= aplist
->get_nof_pars();
8053 for (size_t i
= 0; i
< num
; ++i
) {
8054 const Ttcn::ActualPar
*ap
= aplist
->get_par(i
);
8056 switch (ap
->get_selection()) {
8057 case ActualPar::AP_ERROR
: {
8059 case ActualPar::AP_VALUE
: {
8060 Value
*v
= ap
->get_Value(); // "v_variable" as the parameter of the template
8062 switch (v
->get_valuetype()) {
8063 case Value::V_REFD
: {
8064 chk_defpar_value(v
);
8070 case ActualPar::AP_TEMPLATE
: {
8071 // A component cannot contain a template definition, parameterized or not.
8072 // Therefore the template this actual par references, cannot be
8073 // a field of a component => no error possible, nothing to do.
8075 case ActualPar::AP_REF
: {
8076 // A template cannot have an out/inout parameter
8077 FATAL_ERROR("Template with out parameter?");
8079 case ActualPar::AP_DEFAULT
: {
8080 ap
= ap
->get_ActualPar();
8084 } // switch actual par selection
8088 } // switch templatetype
8092 // This function is called in two situations:
8093 // 1. FormalParList::chk calls FormalPar::chk to compute the default value
8094 // (make an ActualPar from a TemplateInstance).
8095 // In this case, defval.ti==0, and actual_par contains its old value.
8096 // This case is called only if the formal par has a default value.
8097 // 2. FormalParList::chk_actual_parlist calls FormalPar::chk_actual_par
8098 // to check the parameters supplied by the execute statement to the tc.
8099 // In this case, defval.ap has the value computed in case 1.
8100 ActualPar
*FormalPar::chk_actual_par_template(TemplateInstance
*actual_par
,
8101 Type::expected_value_t exp_val
)
8103 actual_par
->chk(type
);
8104 // actual_par->template_body may change: SPECIFIC_VALUE to TEMPLATE_REFD
8105 Definition
*fplist_def
= my_parlist
->get_my_def();
8106 // The parameter list belongs to this definition. If it's a function
8107 // or testcase, it may have a "runs on" clause.
8108 Def_Function
*parent_fn
= dynamic_cast<Def_Function
*>(fplist_def
);
8109 Type
*runs_on_type
= 0;
8110 if (parent_fn
) runs_on_type
= parent_fn
->get_RunsOnType();
8111 else { // not a function; maybe a testcase
8112 Def_Testcase
*parent_tc
= dynamic_cast<Def_Testcase
*>(fplist_def
);
8113 if (parent_tc
) runs_on_type
= parent_tc
->get_RunsOnType();
8116 // If it _has_ a runs on clause, the type must be a component.
8117 if (runs_on_type
->get_typetype() != Type::T_COMPONENT
) FATAL_ERROR("not component?");
8118 // The default value "shall not refer to elements of the component type
8119 // in the runs on clause"
8120 ComponentTypeBody
*runs_on_component
= runs_on_type
->get_CompBody();
8121 size_t compass
= runs_on_component
->get_nof_asss();
8122 for (size_t c
= 0; c
< compass
; c
++) {
8123 Assignment
*ass
= runs_on_component
->get_ass_byIndex(c
);
8128 Ttcn::Template
* body
= actual_par
->get_Template();
8129 if (exp_val
== Type::EXPECTED_STATIC_VALUE
8130 ||exp_val
== Type::EXPECTED_CONSTANT
) {
8131 chk_defpar_template(body
, exp_val
);
8133 // Rip out the type, derived ref and template from actual_par
8134 // (which may come from a function invocation or the definition
8135 // of the default value) and give it to the new ActualPar.
8136 ActualPar
*ret_val
= new ActualPar(
8137 new TemplateInstance(actual_par
->get_Type(),
8138 actual_par
->get_DerivedRef(), actual_par
->get_Template()));
8139 // Zero out these members because the caller will soon call delete
8140 // on actual_par, but they now belong to ret_val.
8141 // FIXME: should this really be in here, or outside in the caller before the delete ?
8142 actual_par
->release();
8144 if (template_restriction
!=TR_NONE
) {
8145 bool needs_runtime_check
=
8146 ret_val
->get_TemplateInstance()->chk_restriction(
8147 "template formal parameter", template_restriction
,
8148 ret_val
->get_TemplateInstance());
8149 if (needs_runtime_check
)
8150 ret_val
->set_gen_restriction_check(template_restriction
);
8155 ActualPar
*FormalPar::chk_actual_par_by_ref(TemplateInstance
*actual_par
,
8156 bool is_template
, Type::expected_value_t exp_val
)
8158 Type
*ap_type
= actual_par
->get_Type();
8160 ap_type
->warning("Explicit type specification is useless for an %s",
8162 actual_par
->chk_Type(type
);
8164 Ref_base
*derived_ref
= actual_par
->get_DerivedRef();
8166 derived_ref
->error("An in-line modified template cannot be used as %s",
8168 actual_par
->chk_DerivedRef(type
);
8170 // needed for the error messages
8171 const char *expected_string
= is_template
?
8172 "template variable or template parameter" :
8173 "variable or value parameter";
8174 Template
*ap_template
= actual_par
->get_Template();
8175 if (ap_template
->is_Ref()) {
8176 Ref_base
*ref
= ap_template
->get_Ref();
8177 Common::Assignment
*ass
= ref
->get_refd_assignment();
8180 return new ActualPar();
8182 bool asstype_correct
= false;
8183 switch (ass
->get_asstype()) {
8185 ass
->use_as_lvalue(*ref
);
8186 if (get_asstype() == A_PAR_VAL_OUT
|| get_asstype() == A_PAR_TEMPL_OUT
) {
8187 ass
->warning("Passing an `in' parameter as another function's `out' parameter");
8192 case A_PAR_VAL_INOUT
:
8193 if (!is_template
) asstype_correct
= true;
8195 case A_PAR_TEMPL_IN
:
8196 ass
->use_as_lvalue(*ref
);
8197 if (get_asstype() == A_PAR_VAL_OUT
|| get_asstype() == A_PAR_TEMPL_OUT
) {
8198 ass
->warning("Passing an `in' parameter as another function's `out' parameter");
8201 case A_VAR_TEMPLATE
:
8202 case A_PAR_TEMPL_OUT
:
8203 case A_PAR_TEMPL_INOUT
:
8204 if (is_template
) asstype_correct
= true;
8209 if (asstype_correct
) {
8210 FieldOrArrayRefs
*t_subrefs
= ref
->get_subrefs();
8211 Type
*ref_type
= ass
->get_Type()->get_field_type(t_subrefs
, exp_val
);
8213 if (!type
->is_identical(ref_type
)) {
8214 ref
->error("Type mismatch: Reference to a %s of type "
8215 "`%s' was expected instead of `%s'", expected_string
,
8216 type
->get_typename().c_str(), ref_type
->get_typename().c_str());
8217 } else if (type
->get_sub_type() && ref_type
->get_sub_type() &&
8218 (type
->get_sub_type()->get_subtypetype()==ref_type
->get_sub_type()->get_subtypetype()) &&
8219 (!type
->get_sub_type()->is_compatible(ref_type
->get_sub_type()))) {
8220 ref
->error("Subtype mismatch: subtype %s has no common value with subtype %s",
8221 type
->get_sub_type()->to_string().c_str(),
8222 ref_type
->get_sub_type()->to_string().c_str());
8224 if (t_subrefs
&& t_subrefs
->refers_to_string_element()) {
8225 ref
->error("Reference to a string element of type `%s' cannot be "
8226 "used in this context", ref_type
->get_typename().c_str());
8230 ref
->error("Reference to a %s was expected for an %s instead of %s",
8231 expected_string
, get_assname(), ass
->get_description().c_str());
8233 ActualPar
* ret_val_ap
= new ActualPar(ref
);
8234 // restriction checking if this is a reference to a template variable
8235 // this is an 'out' or 'inout' template parameter
8236 if (is_template
&& asstype_correct
) {
8237 template_restriction_t refd_tr
;
8238 switch (ass
->get_asstype()) {
8239 case A_VAR_TEMPLATE
: {
8240 Def_Var_Template
* dvt
= dynamic_cast<Def_Var_Template
*>(ass
);
8241 if (!dvt
) FATAL_ERROR("FormalPar::chk_actual_par_by_ref()");
8242 refd_tr
= dvt
->get_template_restriction();
8244 case A_PAR_TEMPL_IN
:
8245 case A_PAR_TEMPL_OUT
:
8246 case A_PAR_TEMPL_INOUT
: {
8247 FormalPar
* fp
= dynamic_cast<FormalPar
*>(ass
);
8248 if (!fp
) FATAL_ERROR("FormalPar::chk_actual_par_by_ref()");
8249 refd_tr
= fp
->get_template_restriction();
8252 FATAL_ERROR("FormalPar::chk_actual_par_by_ref()");
8255 refd_tr
= Template::get_sub_restriction(refd_tr
, ref
);
8256 if (template_restriction
!=refd_tr
) {
8257 bool pre_call_check
=
8258 Template::is_less_restrictive(template_restriction
, refd_tr
);
8259 bool post_call_check
=
8260 Template::is_less_restrictive(refd_tr
, template_restriction
);
8261 if (pre_call_check
|| post_call_check
) {
8262 ref
->warning("Inadequate restriction on the referenced %s `%s', "
8263 "this may cause a dynamic test case error at runtime",
8264 ass
->get_assname(), ref
->get_dispname().c_str());
8265 ass
->note("Referenced %s is here", ass
->get_assname());
8268 ret_val_ap
->set_gen_restriction_check(template_restriction
);
8269 if (post_call_check
)
8270 ret_val_ap
->set_gen_post_restriction_check(refd_tr
);
8272 // for out and inout template parameters of external functions
8273 // always check because we do not trust user written C++ code
8274 if (refd_tr
!=TR_NONE
) {
8275 switch (my_parlist
->get_my_def()->get_asstype()) {
8276 case A_EXT_FUNCTION
:
8277 case A_EXT_FUNCTION_RVAL
:
8278 case A_EXT_FUNCTION_RTEMP
:
8279 ret_val_ap
->set_gen_post_restriction_check(refd_tr
);
8288 actual_par
->error("Reference to a %s was expected for an %s",
8289 expected_string
, get_assname());
8290 return new ActualPar();
8294 ActualPar
*FormalPar::chk_actual_par_timer(TemplateInstance
*actual_par
,
8295 Type::expected_value_t exp_val
)
8297 Type
*ap_type
= actual_par
->get_Type();
8299 ap_type
->error("Explicit type specification cannot be used for a "
8301 actual_par
->chk_Type(0);
8303 Ref_base
*derived_ref
= actual_par
->get_DerivedRef();
8305 derived_ref
->error("An in-line modified template cannot be used as "
8307 actual_par
->chk_DerivedRef(0);
8309 Template
*ap_template
= actual_par
->get_Template();
8310 if (ap_template
->is_Ref()) {
8311 Ref_base
*ref
= ap_template
->get_Ref();
8312 Common::Assignment
*ass
= ref
->get_refd_assignment();
8315 return new ActualPar();
8317 switch (ass
->get_asstype()) {
8319 ArrayDimensions
*dims
= ass
->get_Dimensions();
8320 if (dims
) dims
->chk_indices(ref
, "timer", false, exp_val
);
8321 else if (ref
->get_subrefs()) ref
->error("Reference to single %s "
8322 "cannot have field or array sub-references",
8323 ass
->get_description().c_str());
8326 if (ref
->get_subrefs()) ref
->error("Reference to %s cannot have "
8327 "field or array sub-references", ass
->get_description().c_str());
8330 ref
->error("Reference to a timer or timer parameter was expected for "
8331 "a timer parameter instead of %s", ass
->get_description().c_str());
8333 return new ActualPar(ref
);
8335 actual_par
->error("Reference to a timer or timer parameter was "
8336 "expected for a timer parameter");
8337 return new ActualPar();
8341 ActualPar
*FormalPar::chk_actual_par_port(TemplateInstance
*actual_par
,
8342 Type::expected_value_t exp_val
)
8344 Type
*ap_type
= actual_par
->get_Type();
8346 ap_type
->warning("Explicit type specification is useless for a port "
8348 actual_par
->chk_Type(type
);
8350 Ref_base
*derived_ref
= actual_par
->get_DerivedRef();
8352 derived_ref
->error("An in-line modified template cannot be used as "
8354 actual_par
->chk_DerivedRef(type
);
8356 Template
*ap_template
= actual_par
->get_Template();
8357 if (ap_template
->is_Ref()) {
8358 Ref_base
*ref
= ap_template
->get_Ref();
8359 Common::Assignment
*ass
= ref
->get_refd_assignment();
8362 return new ActualPar();
8364 bool asstype_correct
= false;
8365 switch (ass
->get_asstype()) {
8367 ArrayDimensions
*dims
= ass
->get_Dimensions();
8368 if (dims
) dims
->chk_indices(ref
, "port", false, exp_val
);
8369 else if (ref
->get_subrefs()) ref
->error("Reference to single %s "
8370 "cannot have field or array sub-references",
8371 ass
->get_description().c_str());
8372 asstype_correct
= true;
8375 if (ref
->get_subrefs()) ref
->error("Reference to %s cannot have "
8376 "field or array sub-references", ass
->get_description().c_str());
8377 asstype_correct
= true;
8380 ref
->error("Reference to a port or port parameter was expected for a "
8381 "port parameter instead of %s", ass
->get_description().c_str());
8383 if (asstype_correct
) {
8384 Type
*ref_type
= ass
->get_Type();
8385 if (ref_type
&& !type
->is_identical(ref_type
))
8386 ref
->error("Type mismatch: Reference to a port or port parameter "
8387 "of type `%s' was expected instead of `%s'",
8388 type
->get_typename().c_str(), ref_type
->get_typename().c_str());
8390 return new ActualPar(ref
);
8392 actual_par
->error("Reference to a port or port parameter was expected "
8393 "for a port parameter");
8394 return new ActualPar();
8398 void FormalPar::use_as_lvalue(const Location
& p_loc
)
8402 case A_PAR_TEMPL_IN
:
8405 FATAL_ERROR("FormalPar::use_as_lvalue()");
8407 if (!used_as_lvalue
) {
8408 Definition
*my_def
= my_parlist
->get_my_def();
8409 if (!my_def
) FATAL_ERROR("FormalPar::use_as_lvalue()");
8410 if (my_def
->get_asstype() == A_TEMPLATE
)
8411 p_loc
.error("Parameter `%s' of the template cannot be passed further "
8412 "as `out' or `inout' parameter", id
->get_dispname().c_str());
8414 // update the genname so that all references in the generated code
8415 // will point to the shadow object
8417 set_genname(id
->get_name() + "_shadow");
8419 used_as_lvalue
= true;
8424 char* FormalPar::generate_code_defval(char* str
)
8426 if (!defval
.ap
|| defval_generated
) return str
;
8427 defval_generated
= true;
8428 switch (defval
.ap
->get_selection()) {
8429 case ActualPar::AP_VALUE
: {
8430 Value
*val
= defval
.ap
->get_Value();
8431 if (use_runtime_2
&& TypeConv::needs_conv_refd(val
)) {
8432 str
= TypeConv::gen_conv_code_refd(str
, val
->get_lhs_name().c_str(), val
);
8434 str
= val
->generate_code_init(str
, val
->get_lhs_name().c_str());
8437 case ActualPar::AP_TEMPLATE
: {
8438 TemplateInstance
*ti
= defval
.ap
->get_TemplateInstance();
8439 Template
*temp
= ti
->get_Template();
8440 Ref_base
*dref
= ti
->get_DerivedRef();
8442 expression_struct expr
;
8443 Code::init_expr(&expr
);
8444 expr
.expr
= mputprintf(expr
.expr
, "%s = ",
8445 temp
->get_lhs_name().c_str());
8446 dref
->generate_code(&expr
);
8447 str
= Code::merge_free_expr(str
, &expr
, false);
8449 if (use_runtime_2
&& TypeConv::needs_conv_refd(temp
)) {
8450 str
= TypeConv::gen_conv_code_refd(str
, temp
->get_lhs_name().c_str(), temp
);
8452 str
= temp
->generate_code_init(str
, temp
->get_lhs_name().c_str());
8454 if (defval
.ap
->get_gen_restriction_check() != TR_NONE
) {
8455 str
= Template::generate_restriction_check_code(str
,
8456 temp
->get_lhs_name().c_str(), defval
.ap
->get_gen_restriction_check());
8459 case ActualPar::AP_REF
:
8462 FATAL_ERROR("FormalPar::generate_code()");
8467 void FormalPar::generate_code_defval(output_struct
*target
, bool)
8469 if (!defval
.ap
) return;
8470 switch (defval
.ap
->get_selection()) {
8471 case ActualPar::AP_VALUE
: {
8472 Value
*val
= defval
.ap
->get_Value();
8474 Code::init_cdef(&cdef
);
8475 type
->generate_code_object(&cdef
, val
);
8476 Code::merge_cdef(target
, &cdef
);
8477 Code::free_cdef(&cdef
);
8479 case ActualPar::AP_TEMPLATE
: {
8480 TemplateInstance
*ti
= defval
.ap
->get_TemplateInstance();
8481 Template
*temp
= ti
->get_Template();
8483 Code::init_cdef(&cdef
);
8484 type
->generate_code_object(&cdef
, temp
);
8485 Code::merge_cdef(target
, &cdef
);
8486 Code::free_cdef(&cdef
);
8488 case ActualPar::AP_REF
:
8491 FATAL_ERROR("FormalPar::generate_code()");
8493 target
->functions
.post_init
= generate_code_defval(target
->functions
.post_init
);
8496 char *FormalPar::generate_code_fpar(char *str
, bool display_unused
/* = false */)
8498 // the name of the parameter should not be displayed if the parameter is not
8499 // used (to avoid a compiler warning)
8500 bool display_name
= (usage_found
|| display_unused
|| debugger_active
||
8501 (!enable_set_bound_out_param
&& (asstype
== A_PAR_VAL_OUT
|| asstype
== A_PAR_TEMPL_OUT
)));
8502 const char *name_str
= display_name
? id
->get_name().c_str() : "";
8506 str
= mputprintf(str
, "Lazy_Param<%s>& %s", type
->get_genname_value(my_scope
).c_str(), name_str
);
8508 str
= mputprintf(str
, "const %s& %s", type
->get_genname_value(my_scope
).c_str(), name_str
);
8512 case A_PAR_VAL_INOUT
:
8514 str
= mputprintf(str
, "%s& %s", type
->get_genname_value(my_scope
).c_str(),
8517 case A_PAR_TEMPL_IN
:
8519 str
= mputprintf(str
, "Lazy_Param<%s>& %s", type
->get_genname_template(my_scope
).c_str(), name_str
);
8521 str
= mputprintf(str
, "const %s& %s", type
->get_genname_template(my_scope
).c_str(), name_str
);
8524 case A_PAR_TEMPL_OUT
:
8525 case A_PAR_TEMPL_INOUT
:
8526 str
= mputprintf(str
, "%s& %s",
8527 type
->get_genname_template(my_scope
).c_str(), name_str
);
8530 str
= mputprintf(str
, "TIMER& %s", name_str
);
8533 FATAL_ERROR("FormalPar::generate_code()");
8538 string
FormalPar::get_reference_name(Scope
* scope
) const
8544 case A_PAR_TEMPL_IN
:
8545 ret_val
+= type
->get_genname_template(scope
);
8548 ret_val
+= type
->get_genname_value(scope
);
8553 ret_val
+= get_id().get_name();
8560 char *FormalPar::generate_code_object(char *str
, const char *p_prefix
, char refch
)
8562 const char *name_str
= id
->get_name().c_str();
8566 str
= mputprintf(str
, "Lazy_Param<%s> %s%s;\n", type
->get_genname_value(my_scope
).c_str(), p_prefix
, name_str
);
8568 str
= mputprintf(str
, "%s %s%s;\n", type
->get_genname_value(my_scope
).c_str(), p_prefix
, name_str
);
8572 case A_PAR_VAL_INOUT
:
8574 str
= mputprintf(str
, "%s%c %s%s;\n",
8575 type
->get_genname_value(my_scope
).c_str(), refch
, p_prefix
, name_str
);
8577 case A_PAR_TEMPL_IN
:
8579 str
= mputprintf(str
, "Lazy_Param<%s> %s%s;\n", type
->get_genname_template(my_scope
).c_str(), p_prefix
, name_str
);
8581 str
= mputprintf(str
, "%s %s%s;\n", type
->get_genname_template(my_scope
).c_str(), p_prefix
, name_str
);
8584 case A_PAR_TEMPL_OUT
:
8585 case A_PAR_TEMPL_INOUT
:
8586 str
= mputprintf(str
, "%s%c %s%s;\n",
8587 type
->get_genname_template(my_scope
).c_str(), refch
, p_prefix
, name_str
);
8590 str
= mputprintf(str
, "TIMER& %s%s;\n", p_prefix
, name_str
);
8593 FATAL_ERROR("FormalPar::generate_code_object()");
8598 char *FormalPar::generate_shadow_object(char *str
) const
8600 if (used_as_lvalue
&& !lazy_eval
) {
8601 const string
& t_genname
= get_genname();
8602 const char *genname_str
= t_genname
.c_str();
8603 const char *name_str
= id
->get_name().c_str();
8606 str
= mputprintf(str
, "%s %s(%s);\n",
8607 type
->get_genname_value(my_scope
).c_str(), genname_str
, name_str
);
8609 case A_PAR_TEMPL_IN
:
8610 str
= mputprintf(str
, "%s %s(%s);\n",
8611 type
->get_genname_template(my_scope
).c_str(), genname_str
, name_str
);
8614 FATAL_ERROR("FormalPar::generate_shadow_object()");
8620 char *FormalPar::generate_code_set_unbound(char *str
) const
8623 case A_PAR_TEMPL_OUT
:
8625 str
= mputprintf(str
, "%s.clean_up();\n", id
->get_name().c_str());
8633 void FormalPar::dump_internal(unsigned level
) const
8635 DEBUG(level
, "%s: %s", get_assname(), id
->get_dispname().c_str());
8636 if (type
) type
->dump(level
+ 1);
8639 DEBUG(level
+ 1, "default value:");
8640 defval
.ap
->dump(level
+ 2);
8644 DEBUG(level
+ 1, "default value:");
8645 defval
.ti
->dump(level
+ 2);
8650 // =================================
8651 // ===== FormalParList
8652 // =================================
8654 FormalParList::~FormalParList()
8656 size_t nof_pars
= pars_v
.size();
8657 for (size_t i
= 0; i
< nof_pars
; i
++) delete pars_v
[i
];
8662 FormalParList
*FormalParList::clone() const
8664 FATAL_ERROR("FormalParList::clone");
8667 void FormalParList::set_fullname(const string
& p_fullname
)
8669 Node::set_fullname(p_fullname
);
8670 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
8671 FormalPar
*par
= pars_v
[i
];
8672 par
->set_fullname(p_fullname
+ "." + par
->get_id().get_dispname());
8676 void FormalParList::set_my_scope(Scope
*p_scope
)
8678 set_parent_scope(p_scope
);
8679 Node::set_my_scope(p_scope
);
8680 // the scope of parameters is set to the parent scope instead of this
8681 // because they cannot refer to each other
8682 for (size_t i
= 0; i
< pars_v
.size(); i
++) pars_v
[i
]->set_my_scope(p_scope
);
8685 void FormalParList::add_fp(FormalPar
*p_fp
)
8687 if (!p_fp
) FATAL_ERROR("NULL parameter: Ttcn::FormalParList::add_fp()");
8689 p_fp
->set_my_parlist(this);
8693 bool FormalParList::has_notused_defval() const
8695 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
8696 if (pars_v
[i
]->has_notused_defval())
8702 bool FormalParList::has_only_default_values() const
8704 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
8705 if (!pars_v
[i
]->has_defval()) {
8713 bool FormalParList::has_fp_withName(const Identifier
& p_name
)
8715 if (!checked
) chk(Definition::A_UNDEF
);
8716 return pars_m
.has_key(p_name
.get_name());
8719 FormalPar
*FormalParList::get_fp_byName(const Identifier
& p_name
)
8721 if (!checked
) chk(Definition::A_UNDEF
);
8722 return pars_m
[p_name
.get_name()];
8725 bool FormalParList::get_startability()
8727 if(!checked
) FATAL_ERROR("FormalParList::get_startability()");
8728 return is_startable
;
8731 Common::Assignment
*FormalParList::get_ass_bySRef(Common::Ref_simple
*p_ref
)
8733 if (!p_ref
|| !checked
) FATAL_ERROR("FormalParList::get_ass_bySRef()");
8734 if (p_ref
->get_modid()) return parent_scope
->get_ass_bySRef(p_ref
);
8736 const string
& name
= p_ref
->get_id()->get_name();
8737 if (pars_m
.has_key(name
)) return pars_m
[name
];
8738 else return parent_scope
->get_ass_bySRef(p_ref
);
8742 bool FormalParList::has_ass_withId(const Identifier
& p_id
)
8744 if (!checked
) FATAL_ERROR("Ttcn::FormalParList::has_ass_withId()");
8745 return pars_m
.has_key(p_id
.get_name())
8746 || parent_scope
->has_ass_withId(p_id
);
8749 void FormalParList::set_genname(const string
& p_prefix
)
8751 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
8752 FormalPar
*par
= pars_v
[i
];
8753 const string
& par_name
= par
->get_id().get_name();
8754 if (par
->get_asstype() != Definition::A_PAR_TIMER
)
8755 par
->get_Type()->set_genname(p_prefix
, par_name
);
8756 if (par
->has_defval()) {
8757 string
embedded_genname(p_prefix
);
8758 embedded_genname
+= '_';
8759 embedded_genname
+= par_name
;
8760 embedded_genname
+= "_defval";
8761 ActualPar
*defval
= par
->get_defval();
8762 switch (defval
->get_selection()) {
8763 case ActualPar::AP_ERROR
:
8764 case ActualPar::AP_REF
:
8766 case ActualPar::AP_VALUE
: {
8767 Value
*v
= defval
->get_Value();
8768 v
->set_genname_prefix("const_");
8769 v
->set_genname_recursive(embedded_genname
);
8771 case ActualPar::AP_TEMPLATE
: {
8772 Template
*t
= defval
->get_TemplateInstance()->get_Template();
8773 t
->set_genname_prefix("template_");
8774 t
->set_genname_recursive(embedded_genname
);
8777 FATAL_ERROR("FormalParList::set_genname()");
8783 void FormalParList::chk(Definition::asstype_t deftype
)
8785 if (checked
) return;
8788 is_startable
= true;
8789 Error_Context
cntxt(this, "In formal parameter list");
8790 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
8791 FormalPar
*par
= pars_v
[i
];
8792 const Identifier
& id
= par
->get_id();
8793 const string
& name
= id
.get_name();
8794 const char *dispname
= id
.get_dispname().c_str();
8795 if (pars_m
.has_key(name
)) {
8796 par
->error("Duplicate parameter with name `%s'", dispname
);
8797 pars_m
[name
]->note("Previous definition of `%s' is here", dispname
);
8799 pars_m
.add(name
, par
);
8800 if (parent_scope
&& parent_scope
->has_ass_withId(id
)) {
8801 par
->error("Parameter name `%s' is not unique in the scope "
8802 "hierarchy", dispname
);
8803 Reference
ref(0, id
.clone());
8804 Common::Assignment
*ass
= parent_scope
->get_ass_bySRef(&ref
);
8805 if (!ass
) FATAL_ERROR("FormalParList::chk()");
8806 ass
->note("Symbol `%s' is already defined here in a higher scope "
8810 Error_Context
cntxt2(par
, "In parameter `%s'", dispname
);
8812 // check whether the parameter type is allowed
8814 case Definition::A_TEMPLATE
:
8815 switch (par
->get_asstype()) {
8816 case Definition::A_PAR_VAL_IN
:
8817 case Definition::A_PAR_TEMPL_IN
:
8818 // these are allowed
8821 par
->error("A template cannot have %s", par
->get_assname());
8824 case Definition::A_TESTCASE
:
8825 switch (par
->get_asstype()) {
8826 case Definition::A_PAR_TIMER
:
8827 case Definition::A_PAR_PORT
:
8828 // these are forbidden
8829 par
->error("A testcase cannot have %s", par
->get_assname());
8834 // everything is allowed for functions and altsteps
8838 switch(par
->get_asstype()) {
8839 case Common::Assignment::A_PAR_VAL_IN
:
8840 case Common::Assignment::A_PAR_TEMPL_IN
:
8841 case Common::Assignment::A_PAR_VAL_INOUT
:
8842 case Common::Assignment::A_PAR_TEMPL_INOUT
:
8843 if (is_startable
&& par
->get_Type()->is_component_internal())
8844 is_startable
= false;
8847 is_startable
= false;
8850 if (!par
->has_defval()) min_nof_pars
= i
+ 1;
8851 // the last parameter without a default value determines the minimum
8855 // check that @lazy paramterization not used in cases currently unsupported
8856 void FormalParList::chk_noLazyParams() {
8857 Error_Context
cntxt(this, "In formal parameter list");
8858 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
8859 FormalPar
*par
= pars_v
[i
];
8860 if (par
->get_lazy_eval()) {
8861 par
->error("Formal parameter `%s' cannot be @lazy, not supported in this case.",
8862 par
->get_id().get_dispname().c_str());
8867 void FormalParList::chk_startability(const char *p_what
, const char *p_name
)
8869 if(!checked
) FATAL_ERROR("FormalParList::chk_startability()");
8870 if (is_startable
) return;
8871 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
8872 FormalPar
*par
= pars_v
[i
];
8873 switch (par
->get_asstype()) {
8874 case Common::Assignment::A_PAR_VAL_IN
:
8875 case Common::Assignment::A_PAR_TEMPL_IN
:
8876 case Common::Assignment::A_PAR_VAL_INOUT
:
8877 case Common::Assignment::A_PAR_TEMPL_INOUT
:
8878 if (par
->get_Type()->is_component_internal()) {
8879 map
<Type
*,void> type_chain
;
8880 char* err_str
= mprintf("a parameter or embedded in a parameter of "
8881 "a function used in a start operation. "
8882 "%s `%s' cannot be started on a parallel test component "
8883 "because of `%s'", p_what
, p_name
, par
->get_description().c_str());
8884 par
->get_Type()->chk_component_internal(type_chain
, err_str
);
8889 par
->error("%s `%s' cannot be started on a parallel test component "
8890 "because it has %s", p_what
, p_name
, par
->get_description().c_str());
8895 void FormalParList::chk_compatibility(FormalParList
* p_fp_list
,
8898 size_t nof_type_pars
= pars_v
.size();
8899 size_t nof_function_pars
= p_fp_list
->pars_v
.size();
8900 // check for the number of parameters
8901 if (nof_type_pars
!= nof_function_pars
) {
8902 p_fp_list
->error("Too %s parameters: %lu was expected instead of %lu",
8903 nof_type_pars
< nof_function_pars
? "many" : "few",
8904 (unsigned long) nof_type_pars
, (unsigned long) nof_function_pars
);
8906 size_t upper_limit
=
8907 nof_type_pars
< nof_function_pars
? nof_type_pars
: nof_function_pars
;
8908 for (size_t i
= 0; i
< upper_limit
; i
++) {
8909 FormalPar
*type_par
= pars_v
[i
];
8910 FormalPar
*function_par
= p_fp_list
->pars_v
[i
];
8911 Error_Context
cntxt(function_par
, "In parameter #%lu",
8912 (unsigned long) (i
+ 1));
8913 FormalPar::asstype_t type_par_asstype
= type_par
->get_asstype();
8914 FormalPar::asstype_t function_par_asstype
= function_par
->get_asstype();
8915 // check for parameter kind equivalence
8916 // (in, out or inout / value or template)
8917 if (type_par_asstype
!= function_par_asstype
) {
8918 function_par
->error("The kind of the parameter is not the same as in "
8919 "type `%s': %s was expected instead of %s", where
,
8920 type_par
->get_assname(), function_par
->get_assname());
8922 // check for type equivalence
8923 if (type_par_asstype
!= FormalPar::A_PAR_TIMER
&&
8924 function_par_asstype
!= FormalPar::A_PAR_TIMER
) {
8925 Type
*type_par_type
= type_par
->get_Type();
8926 Type
*function_par_type
= function_par
->get_Type();
8927 if (!type_par_type
->is_identical(function_par_type
)) {
8928 function_par_type
->error("The type of the parameter is not the same "
8929 "as in type `%s': `%s' was expected instead of `%s'", where
,
8930 type_par_type
->get_typename().c_str(),
8931 function_par_type
->get_typename().c_str());
8932 } else if (type_par_type
->get_sub_type() && function_par_type
->get_sub_type() &&
8933 (type_par_type
->get_sub_type()->get_subtypetype()==function_par_type
->get_sub_type()->get_subtypetype()) &&
8934 (!type_par_type
->get_sub_type()->is_compatible(function_par_type
->get_sub_type()))) {
8935 // TODO: maybe equivalence should be checked, or maybe that is too strict
8936 function_par_type
->error(
8937 "Subtype mismatch: subtype %s has no common value with subtype %s",
8938 type_par_type
->get_sub_type()->to_string().c_str(),
8939 function_par_type
->get_sub_type()->to_string().c_str());
8942 // check for template restriction equivalence
8943 if (type_par
->get_template_restriction()!=
8944 function_par
->get_template_restriction()) {
8945 function_par
->error("The template restriction of the parameter is "
8946 "not the same as in type `%s': %s restriction was expected instead "
8947 "of %s restriction", where
,
8948 type_par
->get_template_restriction()==TR_NONE
? "no" :
8949 Template::get_restriction_name(type_par
->get_template_restriction()),
8950 function_par
->get_template_restriction()==TR_NONE
? "no" :
8951 Template::get_restriction_name(function_par
->
8952 get_template_restriction()));
8954 // check for @lazy equivalence
8955 if (type_par
->get_lazy_eval()!=function_par
->get_lazy_eval()) {
8956 function_par
->error("Parameter @lazy-ness mismatch");
8958 // check for name equivalence
8959 const Identifier
& type_par_id
= type_par
->get_id();
8960 const Identifier
& function_par_id
= function_par
->get_id();
8961 if (type_par_id
!= function_par_id
) {
8962 function_par
->warning("The name of the parameter is not the same "
8963 "as in type `%s': `%s' was expected instead of `%s'", where
,
8964 type_par_id
.get_dispname().c_str(),
8965 function_par_id
.get_dispname().c_str());
8970 bool FormalParList::fold_named_and_chk(ParsedActualParameters
*p_paps
,
8971 ActualParList
*p_aplist
)
8973 const size_t num_named
= p_paps
->get_nof_nps();
8974 const size_t num_unnamed
= p_paps
->get_nof_tis();
8975 size_t num_actual
= num_unnamed
;
8977 // Construct a map to tell us what index a FormalPar has
8978 typedef map
<FormalPar
*, size_t> formalpar_map_t
;
8979 formalpar_map_t formalpar_map
;
8981 size_t num_fp
= get_nof_fps();
8982 for (size_t fpx
= 0; fpx
< num_fp
; ++fpx
) {
8983 FormalPar
*fp
= get_fp_byIndex(fpx
);
8984 formalpar_map
.add(fp
, new size_t(fpx
));
8987 // Go through the named parameters
8988 for (size_t i
= 0; i
< num_named
; ++i
) {
8989 NamedParam
*np
= p_paps
->extract_np_byIndex(i
);
8990 // We are now responsible for np.
8992 if (has_fp_withName(*np
->get_name())) {
8993 // there is a formal parameter with that name
8994 FormalPar
*fp
= get_fp_byName(*np
->get_name());
8995 const size_t is_at
= *formalpar_map
[fp
]; // the index of the formal par
8996 if (is_at
>= num_actual
) {
8997 // There is no actual par in the unnamed part.
8998 // Create one from the named param.
9000 // First, pad the gap with '-'
9001 for (; num_actual
< is_at
; ++num_actual
) {
9003 if (pars_v
[num_actual
]->has_defval()) {
9004 not_used
= new Template(Template::TEMPLATE_NOTUSED
);
9006 else { // cannot use '-' if no default value
9007 not_used
= new Template(Template::TEMPLATE_ERROR
);
9009 TemplateInstance
*new_ti
= new TemplateInstance(0, 0, not_used
);
9010 // Conjure a location info at the beginning of the unnamed part
9011 // (that is, the beginning of the actual parameter list)
9012 new_ti
->set_location(p_paps
->get_tis()->get_filename(),
9013 p_paps
->get_tis()->get_first_line(),
9014 p_paps
->get_tis()->get_first_column(), 0, 0);
9015 p_paps
->get_tis()->add_ti(new_ti
);
9017 TemplateInstance
* namedti
= np
->extract_ti();
9018 p_paps
->get_tis()->add_ti(namedti
);
9021 // There is already an actual par at that position, fetch it
9022 TemplateInstance
* ti
= p_paps
->get_tis()->get_ti_byIndex(is_at
);
9023 Template::templatetype_t tt
= ti
->get_Template()->get_templatetype();
9025 if (is_at
>= num_unnamed
&& !ti
->get_Type() && !ti
->get_DerivedRef()
9026 && (tt
== Template::TEMPLATE_NOTUSED
|| tt
== Template::TEMPLATE_ERROR
)) {
9027 // NotUsed in the named part => padding
9028 np
->error("Named parameter `%s' out of order",
9029 np
->get_name()->get_dispname().c_str());
9031 // attempt to override an original unnamed param with a named one
9032 np
->error("Formal parameter `%s' assigned more than once",
9033 np
->get_name()->get_dispname().c_str());
9037 else { // no formal parameter with that name
9039 switch (my_def
->get_asstype()) {
9040 case Common::Assignment::A_TYPE
: {
9041 Type
*t
= my_def
->get_Type();
9043 switch (t
? t
->get_typetype() : 0) {
9044 case Type::T_FUNCTION
:
9045 nam
= mcopystr("Function reference");
9047 case Type::T_ALTSTEP
:
9048 nam
= mcopystr("Altstep reference");
9050 case Type::T_TESTCASE
:
9051 nam
= mcopystr("Testcase reference");
9054 FATAL_ERROR("FormalParList::chk_actual_parlist() "
9055 "Unexpected type %s", t
->get_typename().c_str());
9056 } // switch(typetype)
9059 nam
= mcopystr(my_def
->get_assname());
9061 } // switch(asstype)
9063 *nam
&= ~('a'-'A'); // Make the first letter uppercase
9064 p_paps
->get_tis()->error("%s `%s' has no formal parameter `%s'",
9066 my_def
->get_fullname().c_str(),
9067 np
->get_name()->get_dispname().c_str());
9074 for (size_t fpx
= 0; fpx
< num_fp
; ++fpx
) {
9075 delete formalpar_map
.get_nth_elem(fpx
);
9077 formalpar_map
.clear();
9079 return chk_actual_parlist(p_paps
->get_tis(), p_aplist
);
9082 bool FormalParList::chk_actual_parlist(TemplateInstances
*p_tis
,
9083 ActualParList
*p_aplist
)
9085 size_t formal_pars
= pars_v
.size();
9086 size_t actual_pars
= p_tis
->get_nof_tis();
9087 // p_aplist->get_nof_pars() is usually 0 on entry
9088 bool error_flag
= false;
9090 if (min_nof_pars
== formal_pars
) {
9091 // none of the parameters have default value
9092 if (actual_pars
!= formal_pars
) {
9093 p_tis
->error("Too %s parameters: %lu was expected "
9094 "instead of %lu", actual_pars
< formal_pars
? "few" : "many",
9095 (unsigned long) formal_pars
, (unsigned long) actual_pars
);
9099 // some parameters have default value
9100 if (actual_pars
< min_nof_pars
) {
9101 p_tis
->error("Too few parameters: at least %lu "
9102 "was expected instead of %lu",
9103 (unsigned long) min_nof_pars
, (unsigned long) actual_pars
);
9105 } else if (actual_pars
> formal_pars
) {
9106 p_tis
->error("Too many parameters: at most %lu "
9107 "was expected instead of %lu",
9108 (unsigned long) formal_pars
, (unsigned long) actual_pars
);
9113 // Do not check actual parameters in excess of the formal ones
9114 size_t upper_limit
= actual_pars
< formal_pars
? actual_pars
: formal_pars
;
9115 for (size_t i
= 0; i
< upper_limit
; i
++) {
9116 TemplateInstance
*ti
= p_tis
->get_ti_byIndex(i
);
9118 // the formal parameter for the current actual parameter
9119 FormalPar
*fp
= pars_v
[i
];
9120 Error_Context
cntxt(ti
, "In parameter #%lu for `%s'",
9121 (unsigned long) (i
+ 1), fp
->get_id().get_dispname().c_str());
9122 if (!ti
->get_Type() && !ti
->get_DerivedRef() && ti
->get_Template()
9123 ->get_templatetype() == Template::TEMPLATE_NOTUSED
) {
9124 if (fp
->has_defval()) {
9125 ActualPar
*defval
= fp
->get_defval();
9126 p_aplist
->add(new ActualPar(defval
));
9127 if (defval
->is_erroneous()) error_flag
= true;
9129 ti
->error("Not used symbol (`-') cannot be used for parameter "
9130 "that does not have default value");
9131 p_aplist
->add(new ActualPar());
9134 } else if (!ti
->get_Type() && !ti
->get_DerivedRef() && ti
->get_Template()
9135 ->get_templatetype() == Template::TEMPLATE_ERROR
) {
9136 ti
->error("Parameter not specified");
9138 ActualPar
*ap
= fp
->chk_actual_par(ti
, Type::EXPECTED_DYNAMIC_VALUE
);
9140 if (ap
->is_erroneous()) error_flag
= true;
9144 // The rest of formal parameters have no corresponding actual parameters.
9145 // Create actual parameters for them based on their default values
9146 // (which must exist).
9147 for (size_t i
= upper_limit
; i
< formal_pars
; i
++) {
9148 FormalPar
*fp
= pars_v
[i
];
9149 if (fp
->has_defval()) {
9150 ActualPar
*defval
= fp
->get_defval();
9151 p_aplist
->add(new ActualPar(defval
));
9152 if (defval
->is_erroneous()) error_flag
= true;
9154 p_aplist
->add(new ActualPar()); // erroneous
9161 bool FormalParList::chk_activate_argument(ActualParList
*p_aplist
,
9162 const char* p_description
)
9164 bool ret_val
= true;
9165 for(size_t i
= 0; i
< p_aplist
->get_nof_pars(); i
++) {
9166 ActualPar
*t_ap
= p_aplist
->get_par(i
);
9167 if(t_ap
->get_selection() != ActualPar::AP_REF
) continue;
9168 FormalPar
*t_fp
= pars_v
[i
];
9169 switch(t_fp
->get_asstype()) {
9170 case Common::Assignment::A_PAR_VAL_OUT
:
9171 case Common::Assignment::A_PAR_VAL_INOUT
:
9172 case Common::Assignment::A_PAR_TEMPL_OUT
:
9173 case Common::Assignment::A_PAR_TEMPL_INOUT
:
9174 case Common::Assignment::A_PAR_TIMER
:
9175 //the checking shall be performed for these parameter types
9177 case Common::Assignment::A_PAR_PORT
:
9178 // port parameters are always correct because ports can be defined
9179 // only in component types
9182 FATAL_ERROR("FormalParList::chk_activate_argument()");
9184 Ref_base
*t_ref
= t_ap
->get_Ref();
9185 Common::Assignment
*t_par_ass
= t_ref
->get_refd_assignment();
9186 if(!t_par_ass
) FATAL_ERROR("FormalParList::chk_activate_argument()");
9187 switch (t_par_ass
->get_asstype()) {
9188 case Common::Assignment::A_VAR
:
9189 case Common::Assignment::A_VAR_TEMPLATE
:
9190 case Common::Assignment::A_TIMER
:
9191 // it is not allowed to pass references of local variables or timers
9192 if (t_par_ass
->is_local()) {
9193 t_ref
->error("Parameter #%lu of %s refers to %s, which is a local "
9194 "definition within a statement block and may have shorter "
9195 "lifespan than the activated default. Only references to "
9196 "variables and timers defined in the component type can be passed "
9197 "to activated defaults", (unsigned long) (i
+ 1), p_description
,
9198 t_par_ass
->get_description().c_str());
9202 case Common::Assignment::A_PAR_VAL_IN
:
9203 case Common::Assignment::A_PAR_VAL_OUT
:
9204 case Common::Assignment::A_PAR_VAL_INOUT
:
9205 case Common::Assignment::A_PAR_TEMPL_IN
:
9206 case Common::Assignment::A_PAR_TEMPL_OUT
:
9207 case Common::Assignment::A_PAR_TEMPL_INOUT
:
9208 case Common::Assignment::A_PAR_TIMER
: {
9209 // it is not allowed to pass references pointing to formal parameters
9210 // except for activate() statements within testcases
9211 // note: all defaults are deactivated at the end of the testcase
9212 FormalPar
*t_refd_fp
= dynamic_cast<FormalPar
*>(t_par_ass
);
9213 if (!t_refd_fp
) FATAL_ERROR("FormalParList::chk_activate_argument()");
9214 FormalParList
*t_fpl
= t_refd_fp
->get_my_parlist();
9215 if (!t_fpl
|| !t_fpl
->my_def
)
9216 FATAL_ERROR("FormalParList::chk_activate_argument()");
9217 if (t_fpl
->my_def
->get_asstype() != Common::Assignment::A_TESTCASE
) {
9218 t_ref
->error("Parameter #%lu of %s refers to %s, which may have "
9219 "shorter lifespan than the activated default. Only references to "
9220 "variables and timers defined in the component type can be passed "
9221 "to activated defaults", (unsigned long) (i
+ 1), p_description
,
9222 t_par_ass
->get_description().c_str());
9232 char *FormalParList::generate_code(char *str
, size_t display_unused
/* = 0 */)
9234 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
9235 if (i
> 0) str
= mputstr(str
, ", ");
9236 str
= pars_v
[i
]->generate_code_fpar(str
, i
< display_unused
);
9241 char* FormalParList::generate_code_defval(char* str
)
9243 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
9244 str
= pars_v
[i
]->generate_code_defval(str
);
9249 void FormalParList::generate_code_defval(output_struct
*target
)
9251 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
9252 pars_v
[i
]->generate_code_defval(target
);
9256 char *FormalParList::generate_code_actual_parlist(char *str
,
9257 const char *p_prefix
)
9259 for (size_t i
= 0; i
< pars_v
.size(); i
++) {
9260 if (i
> 0) str
= mputstr(str
, ", ");
9261 str
= mputstr(str
, p_prefix
);
9262 str
= mputstr(str
, pars_v
[i
]->get_id().get_name().c_str());
9267 char *FormalParList::generate_code_object(char *str
, const char *p_prefix
, char refch
)
9269 for (size_t i
= 0; i
< pars_v
.size(); i
++)
9270 str
= pars_v
[i
]->generate_code_object(str
, p_prefix
, refch
);
9274 char *FormalParList::generate_shadow_objects(char *str
) const
9276 for (size_t i
= 0; i
< pars_v
.size(); i
++)
9277 str
= pars_v
[i
]->generate_shadow_object(str
);
9281 char *FormalParList::generate_code_set_unbound(char *str
) const
9283 if (enable_set_bound_out_param
) return str
;
9284 for (size_t i
= 0; i
< pars_v
.size(); i
++)
9285 str
= pars_v
[i
]->generate_code_set_unbound(str
);
9290 void FormalParList::dump(unsigned level
) const
9292 size_t nof_pars
= pars_v
.size();
9293 DEBUG(level
, "formal parameters: %lu pcs.", (unsigned long) nof_pars
);
9294 for(size_t i
= 0; i
< nof_pars
; i
++) pars_v
[i
]->dump(level
+ 1);
9297 // =================================
9299 // =================================
9301 ActualPar::ActualPar(Value
*v
)
9302 : Node(), selection(AP_VALUE
), my_scope(0), gen_restriction_check(TR_NONE
),
9303 gen_post_restriction_check(TR_NONE
)
9305 if (!v
) FATAL_ERROR("ActualPar::ActualPar()");
9309 ActualPar::ActualPar(TemplateInstance
*t
)
9310 : Node(), selection(AP_TEMPLATE
), my_scope(0),
9311 gen_restriction_check(TR_NONE
), gen_post_restriction_check(TR_NONE
)
9313 if (!t
) FATAL_ERROR("ActualPar::ActualPar()");
9317 ActualPar::ActualPar(Ref_base
*r
)
9318 : Node(), selection(AP_REF
), my_scope(0), gen_restriction_check(TR_NONE
),
9319 gen_post_restriction_check(TR_NONE
)
9321 if (!r
) FATAL_ERROR("ActualPar::ActualPar()");
9325 ActualPar::ActualPar(ActualPar
*a
)
9326 : Node(), selection(AP_DEFAULT
), my_scope(0),
9327 gen_restriction_check(TR_NONE
), gen_post_restriction_check(TR_NONE
)
9329 if (!a
) FATAL_ERROR("ActualPar::ActualPar()");
9333 ActualPar::~ActualPar()
9348 break; // nothing to do with act
9350 FATAL_ERROR("ActualPar::~ActualPar()");
9354 ActualPar
*ActualPar::clone() const
9356 FATAL_ERROR("ActualPar::clone");
9359 void ActualPar::set_fullname(const string
& p_fullname
)
9361 Node::set_fullname(p_fullname
);
9366 val
->set_fullname(p_fullname
);
9369 temp
->set_fullname(p_fullname
);
9372 ref
->set_fullname(p_fullname
);
9377 FATAL_ERROR("ActualPar::set_fullname()");
9381 void ActualPar::set_my_scope(Scope
*p_scope
)
9388 val
->set_my_scope(p_scope
);
9391 temp
->set_my_scope(p_scope
);
9394 ref
->set_my_scope(p_scope
);
9397 switch (act
->selection
) {
9399 ref
->set_my_scope(p_scope
);
9406 FATAL_ERROR("ActualPar::set_my_scope()");
9410 FATAL_ERROR("ActualPar::set_my_scope()");
9414 Value
*ActualPar::get_Value() const
9416 if (selection
!= AP_VALUE
) FATAL_ERROR("ActualPar::get_Value()");
9420 TemplateInstance
*ActualPar::get_TemplateInstance() const
9422 if (selection
!= AP_TEMPLATE
)
9423 FATAL_ERROR("ActualPar::get_TemplateInstance()");
9427 Ref_base
*ActualPar::get_Ref() const
9429 if (selection
!= AP_REF
) FATAL_ERROR("ActualPar::get_Ref()");
9433 ActualPar
*ActualPar::get_ActualPar() const
9435 if (selection
!= AP_DEFAULT
) FATAL_ERROR("ActualPar::get_ActualPar()");
9439 void ActualPar::chk_recursions(ReferenceChain
& refch
)
9441 switch (selection
) {
9444 val
->chk_recursions(refch
);
9448 Ref_base
*derived_ref
= temp
->get_DerivedRef();
9450 ActualParList
*parlist
= derived_ref
->get_parlist();
9453 parlist
->chk_recursions(refch
);
9458 Ttcn::Def_Template
* defTemp
= temp
->get_Referenced_Base_Template();
9461 refch
.add(defTemp
->get_fullname());
9465 temp
->get_Template()->chk_recursions(refch
);
9473 bool ActualPar::has_single_expr()
9475 switch (selection
) {
9477 return val
->has_single_expr();
9479 if (gen_restriction_check
!=TR_NONE
||
9480 gen_post_restriction_check
!=TR_NONE
) return false;
9481 return temp
->has_single_expr();
9483 if (gen_restriction_check
!=TR_NONE
||
9484 gen_post_restriction_check
!=TR_NONE
) return false;
9485 if (use_runtime_2
&& ref
->get_subrefs() != NULL
) {
9486 FieldOrArrayRefs
* subrefs
= ref
->get_subrefs();
9487 for (size_t i
= 0; i
< subrefs
->get_nof_refs(); ++i
) {
9488 if (FieldOrArrayRef::ARRAY_REF
== subrefs
->get_ref(i
)->get_type()) {
9493 return ref
->has_single_expr();
9497 FATAL_ERROR("ActualPar::has_single_expr()");
9502 void ActualPar::set_code_section(
9503 GovernedSimple::code_section_t p_code_section
)
9505 switch (selection
) {
9507 val
->set_code_section(p_code_section
);
9510 temp
->set_code_section(p_code_section
);
9513 ref
->set_code_section(p_code_section
);
9519 void ActualPar::generate_code(expression_struct
*expr
, bool copy_needed
, bool lazy_param
, bool used_as_lvalue
) const
9521 switch (selection
) {
9523 if (lazy_param
) { // copy_needed doesn't matter in this case
9524 LazyParamData::init(used_as_lvalue
);
9525 LazyParamData::generate_code(expr
, val
, my_scope
);
9526 LazyParamData::clean();
9527 if (val
->get_valuetype() == Value::V_REFD
) {
9528 // check if the reference is a parameter, mark it as used if it is
9529 Reference
* ref
= dynamic_cast<Reference
*>(val
->get_reference());
9531 ref
->ref_usage_found();
9535 if (copy_needed
) expr
->expr
= mputprintf(expr
->expr
, "%s(",
9536 val
->get_my_governor()->get_genname_value(my_scope
).c_str());
9537 if (use_runtime_2
&& TypeConv::needs_conv_refd(val
)) {
9538 // Generate everything to preamble to be able to tackle the wrapper
9539 // constructor call. TODO: Reduce the number of temporaries created.
9540 const string
& tmp_id
= val
->get_temporary_id();
9541 const char *tmp_id_str
= tmp_id
.c_str();
9542 expr
->preamble
= mputprintf(expr
->preamble
, "%s %s;\n",
9543 val
->get_my_governor()->get_genname_value(my_scope
).c_str(),
9545 expr
->preamble
= TypeConv::gen_conv_code_refd(expr
->preamble
,
9547 expr
->expr
= mputstr(expr
->expr
, tmp_id_str
);
9548 } else val
->generate_code_expr(expr
);
9549 if (copy_needed
) expr
->expr
= mputc(expr
->expr
, ')');
9553 if (lazy_param
) { // copy_needed doesn't matter in this case
9554 LazyParamData::init(used_as_lvalue
);
9555 LazyParamData::generate_code(expr
, temp
, gen_restriction_check
, my_scope
);
9556 LazyParamData::clean();
9557 if (temp
->get_DerivedRef() != NULL
||
9558 temp
->get_Template()->get_templatetype() == Template::TEMPLATE_REFD
) {
9559 // check if the reference is a parameter, mark it as used if it is
9560 Reference
* ref
= dynamic_cast<Reference
*>(temp
->get_DerivedRef() != NULL
?
9561 temp
->get_DerivedRef() : temp
->get_Template()->get_reference());
9563 ref
->ref_usage_found();
9568 expr
->expr
= mputprintf(expr
->expr
, "%s(", temp
->get_Template()
9569 ->get_my_governor()->get_genname_template(my_scope
).c_str());
9570 if (use_runtime_2
&& TypeConv::needs_conv_refd(temp
->get_Template())) {
9571 const string
& tmp_id
= temp
->get_Template()->get_temporary_id();
9572 const char *tmp_id_str
= tmp_id
.c_str();
9573 expr
->preamble
= mputprintf(expr
->preamble
, "%s %s;\n",
9574 temp
->get_Template()->get_my_governor()
9575 ->get_genname_template(my_scope
).c_str(), tmp_id_str
);
9576 expr
->preamble
= TypeConv::gen_conv_code_refd(expr
->preamble
,
9577 tmp_id_str
, temp
->get_Template());
9578 // Not incorporated into gen_conv_code() yet.
9579 if (gen_restriction_check
!= TR_NONE
)
9580 expr
->preamble
= Template::generate_restriction_check_code(
9581 expr
->preamble
, tmp_id_str
, gen_restriction_check
);
9582 expr
->expr
= mputstr(expr
->expr
, tmp_id_str
);
9583 } else temp
->generate_code(expr
, gen_restriction_check
);
9584 if (copy_needed
) expr
->expr
= mputc(expr
->expr
, ')');
9588 if (lazy_param
) FATAL_ERROR("ActualPar::generate_code()"); // syntax error should have already happened
9589 if (copy_needed
) FATAL_ERROR("ActualPar::generate_code()");
9590 if (gen_restriction_check
!= TR_NONE
||
9591 gen_post_restriction_check
!= TR_NONE
) {
9592 // generate runtime check for restricted templates
9593 // code for reference + restriction check
9594 Common::Assignment
*ass
= ref
->get_refd_assignment();
9595 const string
& tmp_id
= my_scope
->get_scope_mod_gen()->get_temporary_id();
9596 const char *tmp_id_str
= tmp_id
.c_str();
9597 expression_struct ref_expr
;
9598 Code::init_expr(&ref_expr
);
9599 ref
->generate_code_const_ref(&ref_expr
);
9600 ref_expr
.preamble
= mputprintf(ref_expr
.preamble
, "%s& %s = %s;\n",
9601 ass
->get_Type()->get_genname_template(ref
->get_my_scope()).c_str(),
9602 tmp_id_str
, ref_expr
.expr
);
9603 if (gen_restriction_check
!= TR_NONE
) {
9604 ref_expr
.preamble
= Template::generate_restriction_check_code(
9605 ref_expr
.preamble
, tmp_id_str
, gen_restriction_check
);
9607 if (gen_post_restriction_check
!= TR_NONE
) {
9608 ref_expr
.postamble
= Template::generate_restriction_check_code(
9609 ref_expr
.postamble
, tmp_id_str
, gen_post_restriction_check
);
9611 // copy content of ref_expr to expr
9612 expr
->preamble
= mputstr(expr
->preamble
, ref_expr
.preamble
);
9613 expr
->expr
= mputprintf(expr
->expr
, "%s", tmp_id_str
);
9614 expr
->postamble
= mputstr(expr
->postamble
, ref_expr
.postamble
);
9615 Code::free_expr(&ref_expr
);
9617 ref
->generate_code(expr
);
9621 if (copy_needed
) FATAL_ERROR("ActualPar::generate_code()");
9622 switch (act
->selection
) {
9625 LazyParamData::generate_code_ap_default_ref(expr
, act
->ref
, my_scope
);
9627 act
->ref
->generate_code(expr
);
9632 LazyParamData::generate_code_ap_default_value(expr
, act
->val
, my_scope
);
9634 expr
->expr
= mputstr(expr
->expr
, act
->val
->get_genname_own(my_scope
).c_str());
9639 LazyParamData::generate_code_ap_default_ti(expr
, act
->temp
, my_scope
);
9641 expr
->expr
= mputstr(expr
->expr
, act
->temp
->get_Template()->get_genname_own(my_scope
).c_str());
9645 FATAL_ERROR("ActualPar::generate_code()");
9649 FATAL_ERROR("ActualPar::generate_code()");
9653 char *ActualPar::rearrange_init_code(char *str
, Common::Module
* usage_mod
)
9655 switch (selection
) {
9657 str
= val
->rearrange_init_code(str
, usage_mod
);
9660 str
= temp
->rearrange_init_code(str
, usage_mod
);
9664 str
= act
->rearrange_init_code_defval(str
, usage_mod
);
9667 FATAL_ERROR("ActualPar::rearrange_init_code()");
9672 char *ActualPar::rearrange_init_code_defval(char *str
, Common::Module
* usage_mod
)
9674 switch (selection
) {
9676 if (val
->get_my_scope()->get_scope_mod_gen() == usage_mod
) {
9677 str
= val
->generate_code_init(str
, val
->get_lhs_name().c_str());
9681 str
= temp
->rearrange_init_code(str
, usage_mod
);
9682 Template
*t
= temp
->get_Template();
9683 if (t
->get_my_scope()->get_scope_mod_gen() == usage_mod
) {
9684 Ref_base
*dref
= temp
->get_DerivedRef();
9686 expression_struct expr
;
9687 Code::init_expr(&expr
);
9688 expr
.expr
= mputprintf(expr
.expr
, "%s = ", t
->get_lhs_name().c_str());
9689 dref
->generate_code(&expr
);
9690 str
= Code::merge_free_expr(str
, &expr
, false);
9692 str
= t
->generate_code_init(str
, t
->get_lhs_name().c_str());
9696 FATAL_ERROR("ActualPar::rearrange_init_code_defval()");
9701 void ActualPar::append_stringRepr(string
& str
) const
9703 switch (selection
) {
9705 str
+= val
->get_stringRepr();
9708 temp
->append_stringRepr(str
);
9711 str
+= ref
->get_dispname();
9717 str
+= "<erroneous actual parameter>";
9721 void ActualPar::dump(unsigned level
) const
9723 switch (selection
) {
9725 DEBUG(level
, "actual parameter: value");
9726 val
->dump(level
+ 1);
9729 DEBUG(level
, "actual parameter: template");
9730 temp
->dump(level
+ 1);
9733 DEBUG(level
, "actual parameter: reference");
9734 ref
->dump(level
+ 1);
9737 DEBUG(level
, "actual parameter: default");
9740 DEBUG(level
, "actual parameter: erroneous");
9744 // =================================
9745 // ===== ActualParList
9746 // =================================
9748 ActualParList::ActualParList(const ActualParList
& p
)
9751 size_t nof_pars
= p
.params
.size();
9752 for (size_t i
= 0; i
< nof_pars
; i
++) params
.add(p
.params
[i
]->clone());
9755 ActualParList::~ActualParList()
9757 size_t nof_pars
= params
.size();
9758 for (size_t i
= 0; i
< nof_pars
; i
++) delete params
[i
];
9762 ActualParList
*ActualParList::clone() const
9764 return new ActualParList(*this);
9767 void ActualParList::set_fullname(const string
& p_fullname
)
9769 Node::set_fullname(p_fullname
);
9770 size_t nof_pars
= params
.size();
9771 for(size_t i
= 0; i
< nof_pars
; i
++)
9772 params
[i
]->set_fullname(p_fullname
+
9773 ".<parameter" + Int2string(i
+ 1) + ">");
9776 void ActualParList::set_my_scope(Scope
*p_scope
)
9778 size_t nof_pars
= params
.size();
9779 for (size_t i
= 0; i
< nof_pars
; i
++) params
[i
]->set_my_scope(p_scope
);
9782 void ActualParList::chk_recursions(ReferenceChain
& refch
)
9784 size_t nof_pars
= params
.size();
9785 for (size_t i
= 0; i
< nof_pars
; i
++)
9786 params
[i
]->chk_recursions(refch
);
9789 void ActualParList::generate_code_noalias(expression_struct
*expr
, FormalParList
*p_fpl
)
9791 size_t nof_pars
= params
.size();
9792 for (size_t i
= 0; i
< nof_pars
; i
++) {
9793 if (i
> 0) expr
->expr
= mputstr(expr
->expr
, ", ");
9794 params
[i
]->generate_code(expr
, false, p_fpl
&& p_fpl
->get_fp_byIndex(i
)->get_lazy_eval(), p_fpl
&& p_fpl
->get_fp_byIndex(i
)->get_used_as_lvalue());
9798 void ActualParList::generate_code_alias(expression_struct
*expr
,
9799 FormalParList
*p_fpl
, Type
*p_comptype
, bool p_compself
)
9801 size_t nof_pars
= params
.size();
9802 // collect all value and template definitions that are passed by reference
9803 map
<Common::Assignment
*, void> value_refs
, template_refs
;
9804 for (size_t i
= 0; i
< nof_pars
; i
++) {
9805 ActualPar
*par
= params
[i
];
9806 if (par
->get_selection() == ActualPar::AP_DEFAULT
)
9807 par
= par
->get_ActualPar();
9808 if (par
->get_selection() == ActualPar::AP_REF
) {
9809 Common::Assignment
*ass
= par
->get_Ref()->get_refd_assignment();
9810 switch (ass
->get_asstype()) {
9811 case Common::Assignment::A_VAR
:
9812 case Common::Assignment::A_PAR_VAL_IN
:
9813 case Common::Assignment::A_PAR_VAL_OUT
:
9814 case Common::Assignment::A_PAR_VAL_INOUT
:
9815 if (!value_refs
.has_key(ass
)) value_refs
.add(ass
, 0);
9817 case Common::Assignment::A_VAR_TEMPLATE
:
9818 case Common::Assignment::A_PAR_TEMPL_IN
:
9819 case Common::Assignment::A_PAR_TEMPL_OUT
:
9820 case Common::Assignment::A_PAR_TEMPL_INOUT
:
9821 if (!template_refs
.has_key(ass
)) template_refs
.add(ass
, 0);
9827 // walk through the parameter list and generate the code
9828 // add an extra copy constructor call to the referenced value and template
9829 // parameters if the referred definition is also passed by reference to
9830 // another parameter
9831 for (size_t i
= 0; i
< nof_pars
; i
++) {
9832 if (i
> 0) expr
->expr
= mputstr(expr
->expr
, ", ");
9833 ActualPar
*par
= params
[i
];
9834 bool copy_needed
= false;
9835 // the copy constructor call is not needed if the parameter is copied
9836 // into a shadow object in the body of the called function
9837 if (!p_fpl
|| !p_fpl
->get_fp_byIndex(i
)->get_used_as_lvalue()) {
9838 switch (par
->get_selection()) {
9839 case ActualPar::AP_VALUE
: {
9840 Value
*v
= par
->get_Value();
9841 if (v
->get_valuetype() == Value::V_REFD
) {
9842 Common::Assignment
*t_ass
=
9843 v
->get_reference()->get_refd_assignment();
9844 if (value_refs
.has_key(t_ass
)) {
9845 // a reference to the same variable is also passed to the called
9848 } else if (p_comptype
|| p_compself
) {
9849 // the called definition has a 'runs on' clause so it can access
9850 // component variables
9851 switch (t_ass
->get_asstype()) {
9852 case Common::Assignment::A_PAR_VAL_OUT
:
9853 case Common::Assignment::A_PAR_VAL_INOUT
:
9854 // the parameter may be an alias of a component variable
9857 case Common::Assignment::A_VAR
:
9858 // copy is needed if t_ass is a component variable that is
9859 // visible by the called definition
9860 if (!t_ass
->is_local()) copy_needed
= true;
9861 /** \todo component type compatibility: check whether t_ass is
9862 * visible from p_comptype (otherwise copy is not needed) */
9869 case ActualPar::AP_TEMPLATE
: {
9870 TemplateInstance
*ti
= par
->get_TemplateInstance();
9871 if (!ti
->get_DerivedRef()) {
9872 Template
*t
= ti
->get_Template();
9873 if (t
->get_templatetype() == Template::TEMPLATE_REFD
) {
9874 Common::Assignment
*t_ass
=
9875 t
->get_reference()->get_refd_assignment();
9876 if (template_refs
.has_key(t_ass
)) {
9877 // a reference to the same variable is also passed to the called
9880 } else if (p_comptype
|| p_compself
) {
9881 // the called definition has a 'runs on' clause so it can access
9882 // component variables
9883 switch (t_ass
->get_asstype()) {
9884 case Common::Assignment::A_PAR_TEMPL_OUT
:
9885 case Common::Assignment::A_PAR_TEMPL_INOUT
:
9886 // the parameter may be an alias of a component variable
9889 case Common::Assignment::A_VAR_TEMPLATE
:
9890 // copy is needed if t_ass is a component variable that is
9891 // visible by the called definition
9892 if (!t_ass
->is_local()) copy_needed
= true;
9893 /** \todo component type compatibility: check whether t_ass is
9894 * visible from p_comptype (otherwise copy is not needed) */
9906 if (use_runtime_2
&& ActualPar::AP_REF
== par
->get_selection()) {
9907 // if the parameter references an element of a record of/set of, then
9908 // the record of object needs to know, so it doesn't delete the referenced
9910 Ref_base
* ref
= par
->get_Ref();
9911 FieldOrArrayRefs
* subrefs
= ref
->get_subrefs();
9912 if (subrefs
!= NULL
) {
9913 Common::Assignment
* ass
= ref
->get_refd_assignment();
9915 for (ref_i
= 0; ref_i
< subrefs
->get_nof_refs(); ++ref_i
) {
9916 FieldOrArrayRef
* subref
= subrefs
->get_ref(ref_i
);
9917 if (FieldOrArrayRef::ARRAY_REF
== subref
->get_type()) {
9918 // set the referenced index in each array in the subrefs
9919 expression_struct array_expr
;
9920 Code::init_expr(&array_expr
);
9921 // the array object's name contains the reference, followed by
9922 // the subrefs before the current array ref
9923 array_expr
.expr
= mcopystr(LazyParamData::in_lazy() ?
9924 LazyParamData::add_ref_genname(ass
, ref
->get_my_scope()).c_str() :
9925 ass
->get_genname_from_scope(ref
->get_my_scope()).c_str());
9927 subrefs
->generate_code(&array_expr
, ass
, ref_i
);
9929 expression_struct index_expr
;
9930 Code::init_expr(&index_expr
);
9931 subrefs
->get_ref(ref_i
)->get_val()->generate_code_expr(&index_expr
);
9932 // insert any preambles the array object or the index might have
9933 if (array_expr
.preamble
!= NULL
) {
9934 expr
->preamble
= mputstr(expr
->preamble
, array_expr
.preamble
);
9935 expr
->postamble
= mputstr(expr
->postamble
, array_expr
.preamble
);
9937 if (index_expr
.preamble
!= NULL
) {
9938 expr
->preamble
= mputstr(expr
->preamble
, index_expr
.preamble
);
9939 expr
->postamble
= mputstr(expr
->postamble
, index_expr
.preamble
);
9941 // let the array object know that the index is referenced before
9942 // calling the function, and let it know that it's now longer
9943 // referenced after the function call (this is done with the help
9944 // of the RefdIndexHandler's constructor and destructor)
9945 string tmp_id
= ref
->get_my_scope()->get_scope_mod_gen()->get_temporary_id();
9946 expr
->preamble
= mputprintf(expr
->preamble
,
9947 "RefdIndexHandler %s(&%s, %s);\n",
9948 tmp_id
.c_str(), array_expr
.expr
, index_expr
.expr
);
9949 // insert any postambles the array object or the index might have
9950 if (array_expr
.postamble
!= NULL
) {
9951 expr
->preamble
= mputstr(expr
->preamble
, array_expr
.postamble
);
9952 expr
->postamble
= mputstr(expr
->postamble
, array_expr
.postamble
);
9954 if (index_expr
.postamble
!= NULL
) {
9955 expr
->preamble
= mputstr(expr
->preamble
, index_expr
.postamble
);
9956 expr
->postamble
= mputstr(expr
->postamble
, index_expr
.postamble
);
9958 Code::free_expr(&array_expr
);
9959 Code::free_expr(&index_expr
);
9960 } // if (FieldOrArrayRef::ARRAY_REF == subref->get_type())
9962 } // if (subrefs != NULL)
9963 } // if (ActualPar::AP_REF == par->get_selection())
9965 par
->generate_code(expr
, copy_needed
, p_fpl
&& p_fpl
->get_fp_byIndex(i
)->get_lazy_eval(), p_fpl
&& p_fpl
->get_fp_byIndex(i
)->get_used_as_lvalue());
9968 template_refs
.clear();
9971 char *ActualParList::rearrange_init_code(char *str
, Common::Module
* usage_mod
)
9973 for (size_t i
= 0; i
< params
.size(); i
++)
9974 str
= params
[i
]->rearrange_init_code(str
, usage_mod
);
9978 void ActualParList::dump(unsigned level
) const
9980 DEBUG(level
, "actual parameter list: %lu parameters",
9981 (unsigned long) params
.size());
9982 for (size_t i
= 0; i
< params
.size(); i
++)
9983 params
[i
]->dump(level
+ 1);