6859e36cc11c7b90c97f61bc1d2e7e2f15091a43
[deliverable/titan.core.git] / compiler2 / ttcn3 / AST_ttcn3.cc
1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Baji, Laszlo
10 * Balasko, Jeno
11 * Baranyi, Botond
12 * Beres, Szabolcs
13 * Delic, Adam
14 * Kovacs, Ferenc
15 * Raduly, Csaba
16 * Szabados, Kristof
17 * Szalai, Gabor
18 * Zalanyi, Balazs Andor
19 * Pandi, Krisztian
20 *
21 ******************************************************************************/
22 #include "../../common/dbgnew.hh"
23 #include "AST_ttcn3.hh"
24 #include "../Identifier.hh"
25 #include "../CompilerError.hh"
26 #include "../Setting.hh"
27 #include "../Type.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"
37 #include "compiler.h"
38 #include "../main.hh"
39 #include "Statement.hh"
40 #include "ILT.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"
47 #include <limits.h>
48
49 // implemented in coding_attrib_p.y
50 extern Ttcn::ExtensionAttributes * parse_extattributes(
51 Ttcn::WithAttribPath *w_attrib_path);
52
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);
56
57
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;
62
63 /** Create a field name in the anytype
64 *
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
68 * type.
69 *
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.
74 *
75 * Note: Prefixing with "AT_" is not done here, but in defUnionClass().
76 *
77 * @param type_name string
78 * @return string to be used as the identifier.
79 */
80 string anytype_field(const string& type_name)
81 {
82 string retval(type_name);
83
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;
88 else ++dot;
89 retval.replace(0, dot, "");
90
91 return retval;
92 }
93
94 extern Common::Modules *modules; // in main.cc
95
96 namespace {
97 static const string _T_("_T_");
98 }
99
100 namespace Ttcn {
101
102 using namespace Common;
103
104 // =================================
105 // ===== FieldOrArrayRef
106 // =================================
107
108 FieldOrArrayRef::FieldOrArrayRef(const FieldOrArrayRef& p)
109 : Node(p), Location(p), ref_type(p.ref_type)
110 {
111 switch (p.ref_type) {
112 case FIELD_REF:
113 u.id = p.u.id->clone();
114 break;
115 case ARRAY_REF:
116 u.arp = p.u.arp->clone();
117 break;
118 default:
119 FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
120 }
121 }
122
123 FieldOrArrayRef::FieldOrArrayRef(Identifier *p_id)
124 : Node(), Location(), ref_type(FIELD_REF)
125 {
126 if (!p_id) FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
127 u.id = p_id;
128 }
129
130 FieldOrArrayRef::FieldOrArrayRef(Value *p_arp)
131 : Node(), Location(), ref_type(ARRAY_REF)
132 {
133 if (!p_arp) FATAL_ERROR("FieldOrArrayRef::FieldOrArrayRef()");
134 u.arp = p_arp;
135 }
136
137 FieldOrArrayRef::~FieldOrArrayRef()
138 {
139 switch (ref_type) {
140 case FIELD_REF:
141 delete u.id;
142 break;
143 case ARRAY_REF:
144 delete u.arp;
145 break;
146 default:
147 FATAL_ERROR("FieldOrArrayRef::~FieldOrArrayRef()");
148 }
149 }
150
151 FieldOrArrayRef *FieldOrArrayRef::clone() const
152 {
153 return new FieldOrArrayRef(*this);
154 }
155
156 void FieldOrArrayRef::set_fullname(const string& p_fullname)
157 {
158 Node::set_fullname(p_fullname);
159 if (ref_type == ARRAY_REF)
160 u.arp->set_fullname(p_fullname + ".<array_index>");
161 }
162
163 void FieldOrArrayRef::set_my_scope(Scope *p_scope)
164 {
165 if (ref_type == ARRAY_REF) u.arp->set_my_scope(p_scope);
166 }
167
168 const Identifier* FieldOrArrayRef::get_id() const
169 {
170 if (ref_type != FIELD_REF) FATAL_ERROR("FieldOrArrayRef::get_id()");
171 return u.id;
172 }
173
174 Value *FieldOrArrayRef::get_val() const
175 {
176 if (ref_type != ARRAY_REF) FATAL_ERROR("FieldOrArrayRef::get_val()");
177 return u.arp;
178 }
179
180 void FieldOrArrayRef::append_stringRepr(string& str) const
181 {
182 switch (ref_type) {
183 case FIELD_REF:
184 str += '.';
185 str += u.id->get_dispname();
186 break;
187 case ARRAY_REF:
188 str += '[';
189 str += u.arp->get_stringRepr();
190 str += ']';
191 break;
192 default:
193 str += "<unknown sub-reference>";
194 }
195 }
196
197 void FieldOrArrayRef::set_field_name_to_lowercase()
198 {
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
206 // for that type)
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, "");
210 }
211 delete u.id;
212 u.id = new Identifier(Identifier::ID_NAME, new_name);
213 }
214 }
215
216 // =================================
217 // ===== FieldOrArrayRefs
218 // =================================
219
220 FieldOrArrayRefs::FieldOrArrayRefs(const FieldOrArrayRefs& p)
221 : Node(p), refs_str_element(false)
222 {
223 for (size_t i = 0; i < p.refs.size(); i++) refs.add(p.refs[i]->clone());
224 }
225
226 FieldOrArrayRefs::~FieldOrArrayRefs()
227 {
228 for (size_t i = 0; i < refs.size(); i++) delete refs[i];
229 refs.clear();
230 }
231
232 FieldOrArrayRefs *FieldOrArrayRefs::clone() const
233 {
234 return new FieldOrArrayRefs(*this);
235 }
236
237 void FieldOrArrayRefs::set_fullname(const string& p_fullname)
238 {
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) + ">");
243 }
244
245 void FieldOrArrayRefs::set_my_scope(Scope *p_scope)
246 {
247 for (size_t i = 0; i < refs.size(); i++) refs[i]->set_my_scope(p_scope);
248 }
249
250 bool FieldOrArrayRefs::has_unfoldable_index() const
251 {
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;
258 }
259 }
260 return false;
261 }
262
263 void FieldOrArrayRefs::remove_refs(size_t n)
264 {
265 for (size_t i = 0; i < n; i++) delete refs[i];
266 refs.replace(0, n, NULL);
267 set_fullname(get_fullname());
268 }
269
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 */
277
278 Identifier* FieldOrArrayRefs::remove_last_field()
279 {
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();
285 delete last_elem;
286 refs.replace(last_elem_ind, 1, NULL);
287 return ret_val;
288 } else return 0;
289 }
290
291 void FieldOrArrayRefs::generate_code(expression_struct *expr,
292 Common::Assignment *ass, size_t nof_subrefs /* = UINT_MAX*/)
293 {
294 Type *type = 0;
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();
308 break;
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
316 // need the prefix.
317 type = ass->get_Type();
318 is_template = true;
319 break;
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).
328 type = 0;
329 break;
330 default:
331 // Reference to other definitions cannot occur during code generation.
332 FATAL_ERROR("FieldOrArrayRefs::generate_code()");
333 type = 0;
334 }
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());
349 if (type) {
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();
361 }
362 } else {
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, ']');
367 if (type) {
368 // Follow the embedded type.
369 switch (type->get_typetype()) {
370 case Type::T_SEQOF:
371 case Type::T_SETOF:
372 case Type::T_ARRAY:
373 type = type->get_ofType();
374 break;
375 default:
376 // The index points to a string element.
377 // There are no further sub-references.
378 type = 0;
379 } // switch
380 } // if (type)
381 } // if (ref->get_type)
382 } // next reference
383 }
384
385 void FieldOrArrayRefs::append_stringRepr(string& str) const
386 {
387 for (size_t i = 0; i < refs.size(); i++) refs[i]->append_stringRepr(str);
388 }
389
390 // =================================
391 // ===== Ref_base
392 // =================================
393
394 Ref_base::Ref_base(const Ref_base& p)
395 : Ref_simple(p), subrefs(p.subrefs)
396 {
397 modid = p.modid ? p.modid->clone() : 0;
398 id = p.id ? p.id->clone() : 0;
399 params_checked = p.is_erroneous;
400 }
401
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)
405 {
406 if (!p_id)
407 FATAL_ERROR("NULL parameter: Ttcn::Ref_base::Ref_base()");
408 }
409
410 Ref_base::~Ref_base()
411 {
412 delete modid;
413 delete id;
414 }
415
416 void Ref_base::set_fullname(const string& p_fullname)
417 {
418 Ref_simple::set_fullname(p_fullname);
419 subrefs.set_fullname(p_fullname);
420 }
421
422 void Ref_base::set_my_scope(Scope *p_scope)
423 {
424 Ref_simple::set_my_scope(p_scope);
425 subrefs.set_my_scope(p_scope);
426 }
427
428 /* returns the referenced variable's base type or value */
429 Setting* Ref_base::get_refd_setting()
430 {
431 Common::Assignment *ass = get_refd_assignment();
432 if (ass) return ass->get_Setting();
433 else return 0;
434 }
435
436 FieldOrArrayRefs *Ref_base::get_subrefs()
437 {
438 if (!id) get_modid();
439 if (subrefs.get_nof_refs() == 0) return 0;
440 else return &subrefs;
441 }
442
443 bool Ref_base::has_single_expr()
444 {
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;
451 }
452 return true;
453 }
454
455 void Ref_base::set_code_section(
456 GovernedSimple::code_section_t p_code_section)
457 {
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);
462 }
463 }
464
465 void Ref_base::generate_code_const_ref(expression_struct_t */*expr*/)
466 {
467 FATAL_ERROR("Ref_base::generate_code_const_ref()");
468 }
469
470 // =================================
471 // ===== Reference
472 // =================================
473
474 Reference::Reference(Identifier *p_id)
475 : Ref_base(), parlist(0)
476 {
477 subrefs.add(new FieldOrArrayRef(p_id));
478 }
479
480 Reference::~Reference()
481 {
482 if (parlist) {
483 delete parlist;
484 }
485 }
486
487 /* Called by:
488 * Common::PortTypeBody::PortTypeBody
489 * Common::Type::Type
490 * Common::TypeMappingTarget::TypeMappingTarget
491 * Common::PatternString::ps_elem_t::chk_ref */
492 Reference *Reference::clone() const
493 {
494 return new Reference(*this);
495 }
496
497 string Reference::get_dispname()
498 {
499 string ret_val;
500 if (id) {
501 if (modid) {
502 ret_val += modid->get_dispname();
503 ret_val += '.';
504 }
505 ret_val += id->get_dispname();
506 subrefs.append_stringRepr(ret_val);
507 } else {
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, "");
512 }
513 return ret_val;
514 }
515
516 Common::Assignment* Reference::get_refd_assignment(bool check_parlist)
517 {
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();
523 if (fplist) {
524 if (fplist->has_only_default_values()
525 && Common::Assignment::A_TEMPLATE == ass->get_asstype()) {
526 Ttcn::ParsedActualParameters params;
527 Error_Context cntxt(&params, "In actual parameter list of %s",
528 ass->get_description().c_str());
529 parlist = new ActualParList();
530 is_erroneous = fplist->fold_named_and_chk(&params, parlist);
531 parlist->set_fullname(get_fullname());
532 parlist->set_my_scope(my_scope);
533 } else {
534 error("Reference to parameterized definition `%s' without "
535 "actual parameter list", ass->get_id().get_dispname().c_str());
536 }
537 }
538 }
539 return ass;
540 }
541
542 const Identifier* Reference::get_modid()
543 {
544 if (!id) detect_modid();
545 return modid;
546 }
547
548 const Identifier* Reference::get_id()
549 {
550 if (!id) detect_modid();
551 return id;
552 }
553
554 Type *Reference::chk_variable_ref()
555 {
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);
561 // no break
562 case Common::Assignment::A_VAR:
563 case Common::Assignment::A_PAR_VAL_OUT:
564 case Common::Assignment::A_PAR_VAL_INOUT:
565 break;
566 default:
567 error("Reference to a variable or value parameter was "
568 "expected instead of %s", t_ass->get_description().c_str());
569 return 0;
570 }
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());
577 }
578 return ret_val;
579 }
580
581 Type *Reference::chk_comptype_ref()
582 {
583 Common::Assignment *ass = get_refd_assignment();
584 if (ass) {
585 if (ass->get_asstype() == Common::Assignment::A_TYPE) {
586 Type *t = ass->get_Type()->get_type_refd_last();
587 switch (t->get_typetype()) {
588 case Type::T_ERROR:
589 // remain silent
590 break;
591 case Type::T_COMPONENT:
592 return t;
593 default:
594 error("Reference `%s' does not refer to a component type",
595 get_dispname().c_str());
596 }
597 } else {
598 error("Reference `%s' does not refer to a type",
599 get_dispname().c_str());
600 }
601 }
602 return 0;
603 }
604
605 bool Reference::has_single_expr()
606 {
607 if (!Ref_base::has_single_expr()) {
608 return false;
609 }
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()) {
613 return false;
614 }
615 }
616 }
617 return true;
618 }
619
620 void Reference::ref_usage_found()
621 {
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);
635 if (fpar == NULL) {
636 FATAL_ERROR("Reference::ref_usage_found()");
637 }
638 fpar->set_usage_found();
639 break; }
640 case Common::Assignment::A_EXT_CONST: {
641 Def_ExtConst* def = dynamic_cast<Def_ExtConst*>(ass);
642 if (def == NULL) {
643 FATAL_ERROR("Reference::ref_usage_found()");
644 }
645 def->set_usage_found();
646 break; }
647 default:
648 break;
649 }
650 }
651
652 void Reference::generate_code(expression_struct_t *expr)
653 {
654 ref_usage_found();
655 Common::Assignment *ass = get_refd_assignment();
656 if (!ass) FATAL_ERROR("Reference::generate_code()");
657 if (parlist) {
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, ')');
665 } else {
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());
670 }
671 if (subrefs.get_nof_refs() > 0) subrefs.generate_code(expr, ass);
672 }
673
674 void Reference::generate_code_const_ref(expression_struct_t *expr)
675 {
676 FieldOrArrayRefs *t_subrefs = get_subrefs();
677 if (!t_subrefs || t_subrefs->get_nof_refs() == 0) {
678 generate_code(expr);
679 return;
680 }
681
682 ref_usage_found();
683 Common::Assignment *ass = get_refd_assignment();
684 if (!ass) FATAL_ERROR("Reference::generate_code_const_ref()");
685
686 bool is_template;
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: {
695 is_template = false;
696 break; }
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: {
703 is_template = true;
704 break; }
705 case Common::Assignment::A_CONST:
706 case Common::Assignment::A_EXT_CONST:
707 default:
708 generate_code(expr);
709 return;
710 }
711
712 Type *refd_gov = ass->get_Type();
713 if (is_template) {
714 expr->expr = mputprintf(expr->expr, "const_cast< const %s&>(",
715 refd_gov->get_genname_template(get_my_scope()).c_str() );
716 } else {
717 expr->expr = mputprintf(expr->expr, "const_cast< const %s&>(",
718 refd_gov->get_genname_value(get_my_scope()).c_str());
719 }
720 if (parlist) {
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, ')');
728 } else {
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());
733 }
734 expr->expr = mputstr(expr->expr, ")");
735
736 if (t_subrefs && t_subrefs->get_nof_refs() > 0)
737 t_subrefs->generate_code(expr, ass);
738 }
739
740 void Reference::generate_code_portref(expression_struct_t *expr,
741 Scope *p_scope)
742 {
743 ref_usage_found();
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);
749 }
750
751 //FIXME quick hack
752 void Reference::generate_code_ispresentbound(expression_struct_t *expr,
753 bool is_template, const bool isbound)
754 {
755 ref_usage_found();
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();
759
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();
764
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,
769 ass_id_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);
772
773 expr->preamble = mputstr(expr->preamble, isbound_expr.preamble);
774 expr->preamble = mputstr(expr->preamble, isbound_expr.expr);
775 Code::free_expr(&isbound_expr);
776
777 expr->expr = mputprintf(expr->expr, "%s", tmp_generalid_str);
778 } else {
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" : "");
782 }
783 }
784
785 void Reference::detect_modid()
786 {
787 // do nothing if detection is already performed
788 if (id) return;
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();
809 }
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
812 if (second_id) {
813 modid = first_id->clone();
814 id = second_id->clone();
815 subrefs.remove_refs(2);
816 } else {
817 modid = 0;
818 id = first_id->clone();
819 subrefs.remove_refs(1);
820 }
821 }
822
823 // =================================
824 // ===== Ref_pard
825 // =================================
826
827 Ref_pard::Ref_pard(const Ref_pard& p)
828 : Ref_base(p), parlist(p.parlist), expr_cache(0)
829 {
830 params = p.params ? p.params->clone() : 0;
831 }
832
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)
836 {
837 if (!p_params)
838 FATAL_ERROR("Ttcn::Ref_pard::Ref_pard(): NULL parameter");
839 }
840
841 Ref_pard::~Ref_pard()
842 {
843 delete params;
844 Free(expr_cache);
845 }
846
847 Ref_pard *Ref_pard::clone() const
848 {
849 return new Ref_pard(*this);
850 }
851
852 void Ref_pard::set_fullname(const string& p_fullname)
853 {
854 Ref_base::set_fullname(p_fullname);
855 parlist.set_fullname(p_fullname);
856 if (params) params->set_fullname(p_fullname);
857 }
858
859 void Ref_pard::set_my_scope(Scope *p_scope)
860 {
861 Ref_base::set_my_scope(p_scope);
862 parlist.set_my_scope(p_scope);
863 if (params) params->set_my_scope(p_scope);
864 }
865
866 string Ref_pard::get_dispname()
867 {
868 if (is_erroneous) return string("erroneous");
869 string ret_val;
870 if (modid) {
871 ret_val += modid->get_dispname();
872 ret_val += '.';
873 }
874 ret_val += id->get_dispname();
875 ret_val += '(';
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);
881 }
882 } else {
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);
887 }
888 }
889 ret_val += ')';
890 subrefs.append_stringRepr(ret_val);
891 return ret_val;
892 }
893
894 Common::Assignment* Ref_pard::get_refd_assignment(bool check_parlist)
895 {
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();
900 if (fplist) {
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
907 delete params;
908 params = 0;
909 } else {
910 params->error("The referenced %s cannot have actual parameters",
911 ass->get_description().c_str());
912 }
913 }
914 return ass;
915 }
916
917 const Identifier* Ref_pard::get_modid()
918 {
919 return modid;
920 }
921
922 const Identifier* Ref_pard::get_id()
923 {
924 return id;
925 }
926
927 ActualParList *Ref_pard::get_parlist()
928 {
929 if (!params_checked) FATAL_ERROR("Ref_pard::get_parlist()");
930 return &parlist;
931 }
932
933 bool Ref_pard::chk_activate_argument()
934 {
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());
940 return false;
941 }
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());
950 }
951
952 bool Ref_pard::has_single_expr()
953 {
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();
959 if (ass) {
960 const FormalParList *fplist = ass->get_FormalParList();
961 if (fplist) {
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;
966 }
967 }
968 }
969 return true;
970 }
971
972 void Ref_pard::set_code_section(
973 GovernedSimple::code_section_t p_code_section)
974 {
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);
978 }
979
980 void Ref_pard::generate_code(expression_struct_t *expr)
981 {
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, ')');
989 // subreferences
990 if (subrefs.get_nof_refs() > 0) subrefs.generate_code(expr, ass);
991 }
992
993 void Ref_pard::generate_code_cached(expression_struct_t *expr)
994 {
995 if (expr_cache) {
996 expr->expr = mputstr(expr->expr, expr_cache);
997 }
998 else {
999 generate_code(expr);
1000 expr_cache = mputstr(expr_cache, expr->expr);
1001 }
1002 }
1003
1004 void Ref_pard::generate_code_const_ref(expression_struct_t *expr)
1005 {
1006 FieldOrArrayRefs *t_subrefs = get_subrefs();
1007 if (!t_subrefs || t_subrefs->get_nof_refs() == 0) {
1008 generate_code(expr);
1009 return;
1010 }
1011
1012 Common::Assignment *ass = get_refd_assignment();
1013 if (!ass) FATAL_ERROR("Ref_pard::generate_code_const_ref()");
1014
1015 bool is_template;
1016 switch (ass->get_asstype()) {
1017 case Common::Assignment::A_TEMPLATE:
1018 if (NULL == ass->get_FormalParList()) {
1019 // not a parameterized template
1020 is_template = true;
1021 break;
1022 }
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);
1035 return;
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;
1042 break; }
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: {
1048 is_template = true;
1049 break; }
1050 default:
1051 is_template = false;
1052 break;
1053 }
1054
1055 Type *refd_gov = ass->get_Type();
1056 if (!refd_gov) {
1057 generate_code(expr);
1058 return;
1059 }
1060
1061 if (is_template) {
1062 expr->expr = mputprintf(expr->expr, "const_cast< const %s&>(",
1063 refd_gov->get_genname_template(get_my_scope()).c_str() );
1064 } else {
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":"");
1068 }
1069
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, "))");
1075
1076 t_subrefs->generate_code(expr, ass);
1077 }
1078
1079 // =================================
1080 // ===== NameBridgingScope
1081 // =================================
1082 string NameBridgingScope::get_scopeMacro_name() const
1083 {
1084 if (scopeMacro_name.empty()) FATAL_ERROR("NameBridgingScope::get_scopeMacro_name()");
1085 return scopeMacro_name;
1086 }
1087
1088 NameBridgingScope *NameBridgingScope::clone() const
1089 {
1090 FATAL_ERROR("NameBridgingScope::clone");
1091 }
1092
1093 Common::Assignment* NameBridgingScope::get_ass_bySRef(Ref_simple *p_ref)
1094 {
1095 return get_parent_scope()->get_ass_bySRef(p_ref);
1096 }
1097
1098 // =================================
1099 // ===== RunsOnScope
1100 // =================================
1101
1102 RunsOnScope::RunsOnScope(Type *p_comptype)
1103 : Scope(), component_type(p_comptype)
1104 {
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() + "'");
1110 }
1111
1112 RunsOnScope *RunsOnScope::clone() const
1113 {
1114 FATAL_ERROR("RunsOnScope::clone()");
1115 }
1116
1117 void RunsOnScope::chk_uniq()
1118 {
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())
1122 return;
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());
1134 }
1135
1136 }
1137 }
1138
1139 RunsOnScope *RunsOnScope::get_scope_runs_on()
1140 {
1141 return this;
1142 }
1143
1144 Common::Assignment *RunsOnScope::get_ass_bySRef(Ref_simple *p_ref)
1145 {
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);
1148 else {
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()");
1153
1154 if (component_defs->is_own_assignment(ass)) return ass;
1155 else if (ass->get_visibility() == PUBLIC) {
1156 return ass;
1157 }
1158
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());
1162 return 0;
1163 } else return parent_scope->get_ass_bySRef(p_ref);
1164 }
1165 }
1166
1167 bool RunsOnScope::has_ass_withId(const Identifier& p_id)
1168 {
1169 return component_defs->has_ass_withId(p_id)
1170 || parent_scope->has_ass_withId(p_id);
1171 }
1172
1173 // =================================
1174 // ===== FriendMod
1175 // =================================
1176
1177 FriendMod::FriendMod(Identifier *p_modid)
1178 : Node(), modid(p_modid), w_attrib_path(0), parentgroup(0), checked(false)
1179 {
1180 if (!p_modid) FATAL_ERROR("NULL parameter: Ttcn::FriendMod::FriendMod()");
1181 set_fullname("<friends>."+modid->get_dispname());
1182 }
1183
1184 FriendMod::~FriendMod()
1185 {
1186 delete modid;
1187
1188 delete w_attrib_path;
1189 }
1190
1191 FriendMod *FriendMod::clone() const
1192 {
1193 FATAL_ERROR("Ttcn::FriendMod::clone()");
1194 }
1195
1196 void FriendMod::set_fullname(const string& p_fullname)
1197 {
1198 if(w_attrib_path) w_attrib_path->set_fullname(p_fullname + ".<attribpath>");
1199 }
1200
1201 void FriendMod::chk()
1202 {
1203 if (checked) return;
1204
1205 Error_Context cntxt(this, "In friend module declaration");
1206
1207 if (w_attrib_path) {
1208 w_attrib_path->chk_global_attrib();
1209 w_attrib_path->chk_no_qualif();
1210 }
1211
1212 checked = true;
1213 }
1214
1215 void FriendMod::set_with_attr(MultiWithAttrib* p_attrib)
1216 {
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);
1221 }
1222 }
1223
1224 WithAttribPath* FriendMod::get_attrib_path()
1225 {
1226 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
1227 return w_attrib_path;
1228 }
1229
1230 void FriendMod::set_parent_path(WithAttribPath* p_path)
1231 {
1232 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
1233 w_attrib_path->set_parent(p_path);
1234 }
1235
1236 void FriendMod::set_parent_group(Group* p_group)
1237 {
1238 if(parentgroup) FATAL_ERROR("FriendMod::set_parent_group");
1239 parentgroup = p_group;
1240 }
1241
1242 // =================================
1243 // ===== ImpMod
1244 // =================================
1245
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)
1250 {
1251 if (!p_modid) FATAL_ERROR("NULL parameter: Ttcn::ImpMod::ImpMod()");
1252 set_fullname("<imports>." + modid->get_dispname());
1253 }
1254
1255 ImpMod::~ImpMod()
1256 {
1257 delete modid;
1258 delete language_spec;
1259
1260 delete w_attrib_path;
1261 }
1262
1263 ImpMod *ImpMod::clone() const
1264 {
1265 FATAL_ERROR("No clone for you!");
1266 }
1267
1268 void ImpMod::set_fullname(const string& p_fullname)
1269 {
1270 if(w_attrib_path) w_attrib_path->set_fullname(p_fullname + ".<attribpath>");
1271 }
1272
1273 void ImpMod::chk()
1274 {
1275 if (w_attrib_path) {
1276 w_attrib_path->chk_global_attrib();
1277 w_attrib_path->chk_no_qualif();
1278 }
1279 }
1280
1281 void ImpMod::chk_imp(ReferenceChain& refch, vector<Common::Module>& moduleStack)
1282 {
1283 Error_Context cntxt(this, "In import definition");
1284
1285 if (!modules->has_mod_withId(*modid)) {
1286 error("There is no module with identifier `%s'",
1287 modid->get_dispname().c_str());
1288 return;
1289 }
1290
1291 Common::Module *m = modules->get_mod_byId(*modid);
1292 if (m == NULL)
1293 return;
1294
1295 set_mod(m);
1296 if (m == my_mod) {
1297 error("A module cannot import from itself");
1298 return;
1299 }
1300 chk();
1301
1302 moduleStack.add(m);
1303
1304 refch.mark_state();
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());
1309 }
1310 refch.prev_state();
1311 return;
1312 } else {
1313 refch.add(my_mod->get_fullname());
1314
1315 if (ImpMod::I_IMPORTIMPORT == imptype){
1316 Ttcn::Module* ttcnmodule =static_cast<Ttcn::Module*>(m);
1317 const Imports& imp = ttcnmodule->get_imports();
1318
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
1323
1324 refch.mark_state();
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());
1330 }
1331 refch.prev_state();
1332 continue;
1333 } else {
1334 refch.add(m->get_fullname());
1335 cm->chk_imp(refch, moduleStack);
1336 }
1337 }
1338 refch.prev_state();
1339 }
1340 } else {
1341 //refch.mark_state();
1342 m->chk_imp(refch, moduleStack);
1343 //refch.prev_state();
1344 }
1345 }
1346 refch.prev_state();
1347
1348 size_t state=moduleStack.size();
1349 moduleStack.replace(state, moduleStack.size() - state);
1350 }
1351
1352 bool ImpMod::has_imported_def(const Identifier& p_source_modid,
1353 const Identifier& p_id, const Location *loc) const
1354 {
1355 if (!mod) return false;
1356 else {
1357 switch (imptype) {
1358 case I_ALL:
1359 case I_IMPORTSPEC:
1360 {
1361 Common::Assignment* return_assignment = mod->importAssignment(p_source_modid, p_id);
1362 if (return_assignment != NULL) {
1363 return true;
1364 } else {
1365 return false;
1366 }
1367 break;
1368 }
1369 case I_IMPORTIMPORT:
1370 {
1371 Ttcn::Module *tm = static_cast<Ttcn::Module*>(mod); // B
1372
1373 const Imports & imps = tm->get_imports();
1374
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;
1382
1383 if (return_assignment != NULL) {
1384 return true;
1385 } else {
1386 return false;
1387 }
1388 }
1389 //satisfy destructor
1390 tempusedImpMods.clear();
1391
1392 break;
1393 }
1394 default:
1395 FATAL_ERROR("ImpMod::get_imported_def");
1396 }
1397 }
1398 return false;
1399 }
1400
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
1405 {
1406 if (!mod) return 0;
1407 else {
1408
1409 Common::Assignment* result = NULL;
1410
1411 switch (imptype) {
1412 case I_ALL:
1413 case I_IMPORTSPEC:
1414 result = mod->importAssignment(p_source_modid, p_id);
1415 if (result != NULL) {
1416 usedImpMods.add(const_cast<Ttcn::ImpMod*>(this));
1417 }
1418 return result;
1419 break;
1420 case I_IMPORTIMPORT:
1421 {
1422 Ttcn::Module *tm = static_cast<Ttcn::Module*>(mod);
1423
1424 const Imports & imps = tm->get_imports();
1425 Common::Assignment* t_ass = NULL;
1426
1427 for (size_t i = 0, num = imps.impmods_v.size(); i < num; ++i) {
1428 vector<ImpMod> tempusedImpMods;
1429
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 );
1433 }
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 );
1436 if (t_ass != NULL){
1437 tempusedImpMods.add(imps.impmods_v[i]);
1438 }
1439 }
1440 refch->prev_state();
1441
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())) {
1451 visible= false;
1452 }
1453 }
1454 }
1455 if (visible) {
1456 for (size_t t = 0; i< tempusedImpMods.size(); i++) {
1457 usedImpMods.add(tempusedImpMods[t]);
1458 }
1459
1460 if (!result) {
1461 result = t_ass;
1462 } else if(result != t_ass) {
1463 if (loc == NULL) {
1464 result = NULL;
1465 } else {
1466 loc->error(
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());
1470 }
1471 }
1472 }
1473 t_ass = NULL;
1474 }
1475 tempusedImpMods.clear();
1476 }
1477
1478 if (result != NULL) {
1479 usedImpMods.add(const_cast<Ttcn::ImpMod*>(this));
1480 }
1481 return result;
1482 break;
1483 }
1484 default:
1485 FATAL_ERROR("ImpMod::get_imported_def");
1486 }
1487 }
1488 }
1489
1490 void ImpMod::set_language_spec(const char *p_language_spec)
1491 {
1492 if (language_spec) FATAL_ERROR("ImpMod::set_language_spec()");
1493 if (p_language_spec) language_spec = new string(p_language_spec);
1494 }
1495
1496 void ImpMod::generate_code(output_struct *target)
1497 {
1498 const char *module_name = modid->get_name().c_str();
1499
1500 target->header.includes = mputprintf(target->header.includes,
1501 "#include \"%s.hh\"\n",
1502 duplicate_underscores ? module_name : modid->get_ttcnname().c_str());
1503
1504 target->functions.pre_init = mputprintf(target->functions.pre_init,
1505 "%s%s.pre_init_module();\n", module_name,
1506 "::module_object");
1507
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,
1511 "::module_object");
1512
1513 }
1514 }
1515
1516 void ImpMod::dump(unsigned level) const
1517 {
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();
1521 if (attrib) {
1522 DEBUG(level + 1, "Attributes:");
1523 attrib->dump(level + 2);
1524 }
1525 }
1526 }
1527
1528 void ImpMod::set_with_attr(MultiWithAttrib* p_attrib)
1529 {
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);
1534 }
1535 }
1536
1537 WithAttribPath* ImpMod::get_attrib_path()
1538 {
1539 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
1540 return w_attrib_path;
1541 }
1542
1543 void ImpMod::set_parent_path(WithAttribPath* p_path)
1544 {
1545 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
1546 w_attrib_path->set_parent(p_path);
1547 }
1548
1549 void ImpMod::set_parent_group(Group* p_group)
1550 {
1551 if(parentgroup) FATAL_ERROR("ImpMod::set_parent_group");
1552 parentgroup = p_group;
1553 }
1554
1555 // =================================
1556 // ===== Imports
1557 // =================================
1558
1559 Imports::~Imports()
1560 {
1561 for (size_t i = 0; i< impmods_v.size(); i++)
1562 delete impmods_v[i];
1563 impmods_v.clear();
1564 }
1565
1566 Imports *Imports::clone() const
1567 {
1568 FATAL_ERROR("Ttcn::Imports::clone()");
1569 }
1570
1571 void Imports::add_impmod(ImpMod *p_impmod)
1572 {
1573 if (!p_impmod) FATAL_ERROR("Ttcn::Imports::add_impmod()");
1574 impmods_v.add(p_impmod);
1575 p_impmod->set_my_mod(my_mod);
1576 }
1577
1578 void Imports::set_my_mod(Module *p_mod)
1579 {
1580 my_mod = p_mod;
1581 for(size_t i = 0; i < impmods_v.size(); i++)
1582 impmods_v[i]->set_my_mod(my_mod);
1583 }
1584
1585 bool Imports::has_impmod_withId(const Identifier& p_id) const
1586 {
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
1593 return true;
1594 }
1595 }
1596 return false;
1597 }
1598
1599 void Imports::chk_imp(ReferenceChain& refch, vector<Common::Module>& moduleStack)
1600 {
1601 if (impmods_v.size() <= 0) return;
1602
1603 if (!my_mod) FATAL_ERROR("Ttcn::Imports::chk_imp()");
1604
1605 checked = true;
1606
1607 //Do some checks
1608 for(size_t n = 0; n < impmods_v.size(); n++)
1609 {
1610 impmods_v[n]->chk();
1611 }
1612
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);
1618
1619 const Identifier& im_id = im->get_modid();
1620 Common::Module *m = modules->get_mod_byId(im_id);
1621 if (m == NULL)
1622 continue;
1623 if (m->get_gen_code()) my_mod->set_gen_code();
1624 } // next assignment
1625 }
1626
1627 bool Imports::has_imported_def(const Identifier& p_id, const Location *loc) const
1628 {
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;
1633 }
1634 return false;
1635 }
1636
1637 Common::Assignment *Imports::get_imported_def(
1638 const Identifier& p_source_modid, const Identifier& p_id,
1639 const Location *loc, ReferenceChain* refch)
1640 {
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();
1650
1651 if(ass) {
1652 if (!result) {
1653 result = ass;
1654 } else if(result != ass && loc) {
1655 if(loc == NULL) {
1656 result = NULL;
1657 } else {
1658 loc->error(
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());
1662 }
1663 }
1664 }
1665 }
1666 return result;
1667 }
1668
1669 void Imports::get_imported_mods(Module::module_set_t& p_imported_mods) const
1670 {
1671 for (size_t i = 0; i < impmods_v.size(); i++) {
1672 ImpMod *im = impmods_v[i];
1673 Common::Module *m = im->get_mod();
1674 if (!m) continue;
1675 if (!p_imported_mods.has_key(m)) {
1676 p_imported_mods.add(m, 0);
1677 m->get_visible_mods(p_imported_mods);
1678 }
1679 }
1680 }
1681
1682 void Imports::generate_code(output_struct *target)
1683 {
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
1703 covered = true;
1704 break;
1705 }
1706 }
1707 // do not generate the #include if a covering module is found
1708 if (!covered) im->generate_code(target);
1709 }
1710 }
1711
1712 void Imports::generate_code(CodeGenHelper& cgh) {
1713 generate_code(cgh.get_current_outputstruct());
1714 }
1715
1716 void Imports::dump(unsigned level) const
1717 {
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);
1721 }
1722
1723 // =================================
1724 // ===== Definitions
1725 // =================================
1726
1727 Definitions::~Definitions()
1728 {
1729 for(size_t i = 0; i < ass_v.size(); i++) delete ass_v[i];
1730 ass_v.clear();
1731 ass_m.clear();
1732 }
1733
1734 Definitions *Definitions::clone() const
1735 {
1736 FATAL_ERROR("Definitions::clone");
1737 }
1738
1739 void Definitions::add_ass(Definition *p_ass)
1740 {
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()");
1743 ass_v.add(p_ass);
1744 p_ass->set_my_scope(this);
1745 }
1746
1747 void Definitions::set_fullname(const string& p_fullname)
1748 {
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());
1753 }
1754 }
1755
1756 bool Definitions::has_local_ass_withId(const Identifier& p_id)
1757 {
1758 if (!checked) chk_uniq();
1759 return ass_m.has_key(p_id.get_name());
1760 }
1761
1762 Common::Assignment* Definitions::get_local_ass_byId(const Identifier& p_id)
1763 {
1764 if (!checked) chk_uniq();
1765 return ass_m[p_id.get_name()];
1766 }
1767
1768 size_t Definitions::get_nof_asss()
1769 {
1770 if (!checked) chk_uniq();
1771 return ass_m.size();
1772 }
1773
1774 size_t Definitions::get_nof_raw_asss()
1775 {
1776 return ass_v.size();
1777 }
1778
1779 Common::Assignment* Definitions::get_ass_byIndex(size_t p_i)
1780 {
1781 if (!checked) chk_uniq();
1782 return ass_m.get_nth_elem(p_i);
1783 }
1784
1785 Ttcn::Definition* Definitions::get_raw_ass_byIndex(size_t p_i) {
1786 return ass_v[p_i];
1787 }
1788
1789 void Definitions::chk_uniq()
1790 {
1791 if (checked) return;
1792 ass_m.clear();
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);
1801 } else {
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());
1806 }
1807 }
1808 }
1809 checked = true;
1810 }
1811
1812 void Definitions::chk()
1813 {
1814 for (size_t i = 0; i < ass_v.size(); i++) ass_v[i]->chk();
1815 }
1816
1817 void Definitions::chk_for()
1818 {
1819 if (checked) return;
1820 checked = true;
1821 ass_m.clear();
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);
1832 } else {
1833 ass_m.add(name, def);
1834 if (parent_scope) {
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());
1847 }
1848 }
1849 }
1850 def->chk();
1851 }
1852 }
1853
1854 void Definitions::set_genname(const string& prefix)
1855 {
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());
1859 }
1860 }
1861
1862 void Definitions::generate_code(output_struct* target)
1863 {
1864 for(size_t i = 0; i < ass_v.size(); i++) ass_v[i]->generate_code(target);
1865 }
1866
1867 void Definitions::generate_code(CodeGenHelper& cgh) {
1868 // FIXME: implement
1869 for(size_t i = 0; i < ass_v.size(); i++) ass_v[i]->generate_code(cgh);
1870 }
1871
1872 char* Definitions::generate_code_str(char *str)
1873 {
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);
1877 }
1878 return str;
1879 }
1880
1881 void Definitions::ilt_generate_code(ILT *ilt)
1882 {
1883 for(size_t i=0; i<ass_v.size(); i++)
1884 ass_v[i]->ilt_generate_code(ilt);
1885 }
1886
1887
1888 void Definitions::dump(unsigned level) const
1889 {
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);
1892 }
1893
1894 // =================================
1895 // ===== Group
1896 // =================================
1897
1898 Group::Group(Identifier *p_id)
1899 : Node(), Location(), parent_group(0), w_attrib_path(0), id(p_id),
1900 checked(false)
1901 {
1902 if (!p_id) FATAL_ERROR("Group::Group()");
1903 }
1904
1905 Group::~Group()
1906 {
1907 delete w_attrib_path;
1908 ass_v.clear();
1909 ass_m.clear();
1910 for (size_t i = 0; i < group_v.size(); i++) delete group_v[i];
1911 group_v.clear();
1912 group_m.clear();
1913 impmods_v.clear();
1914 friendmods_v.clear();
1915 delete id;
1916 }
1917
1918 Group* Group::clone() const
1919 {
1920 FATAL_ERROR("Ttcn::Group::clone()");
1921 }
1922
1923 void Group::set_fullname(const string& p_fullname)
1924 {
1925 Node::set_fullname(p_fullname);
1926 for(size_t i = 0; i < group_v.size() ; i++)
1927 {
1928 group_v[i]->set_fullname(p_fullname + ".<group " + Int2string(i) + ">");
1929 }
1930 if (w_attrib_path) w_attrib_path->set_fullname( p_fullname
1931 + ".<attribpath>");
1932 }
1933
1934 void Group::add_ass(Definition* p_ass)
1935 {
1936 ass_v.add(p_ass);
1937 p_ass->set_parent_group(this);
1938 }
1939
1940 void Group::add_group(Group* p_group)
1941 {
1942 group_v.add(p_group);
1943 }
1944
1945 void Group::set_parent_group(Group* p_parent_group)
1946 {
1947 if (parent_group) FATAL_ERROR("Group::set_parent_group()");
1948 parent_group = p_parent_group;
1949 }
1950
1951 void Group::set_with_attr(MultiWithAttrib* p_attrib)
1952 {
1953 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
1954 w_attrib_path->set_with_attr(p_attrib);
1955 }
1956
1957 WithAttribPath* Group::get_attrib_path()
1958 {
1959 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
1960 return w_attrib_path;
1961 }
1962
1963 void Group::set_parent_path(WithAttribPath* p_path)
1964 {
1965 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
1966 w_attrib_path->set_parent(p_path);
1967 }
1968
1969 void Group::chk_uniq()
1970 {
1971 if (checked) return;
1972 ass_m.clear();
1973 group_m.clear();
1974
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);
1979 }
1980
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());
1990 }
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);
1997 }
1998 checked = true;
1999 }
2000
2001 void Group::chk()
2002 {
2003 Error_Context cntxt(this, "In group `%s'", id->get_dispname().c_str());
2004
2005 chk_uniq();
2006
2007 if (w_attrib_path) {
2008 w_attrib_path->chk_global_attrib();
2009 w_attrib_path->chk_no_qualif();
2010 }
2011
2012 for(size_t i = 0; i < group_v.size(); i++) group_v[i]->chk();
2013 }
2014
2015 void Group::add_impmod(ImpMod *p_impmod)
2016 {
2017 impmods_v.add(p_impmod);
2018 p_impmod->set_parent_group(this);
2019 }
2020
2021 void Group::add_friendmod(FriendMod *p_friendmod)
2022 {
2023 friendmods_v.add(p_friendmod);
2024 p_friendmod->set_parent_group(this);
2025 }
2026
2027 void Group::dump(unsigned level) const
2028 {
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();
2041 if (attrib) {
2042 DEBUG(level + 1, "Group Attributes:");
2043 attrib->dump(level + 2);
2044 }
2045 }
2046 }
2047
2048 // =================================
2049 // ===== ControlPart
2050 // =================================
2051
2052 ControlPart::ControlPart(StatementBlock* p_block)
2053 : Node(), Location(), block(p_block), w_attrib_path(0)
2054 {
2055 if (!p_block) FATAL_ERROR("ControlPart::ControlPart()");
2056 }
2057
2058 ControlPart::~ControlPart()
2059 {
2060 delete block;
2061 delete w_attrib_path;
2062 }
2063
2064 ControlPart* ControlPart::clone() const
2065 {
2066 FATAL_ERROR("ControlPart::clone");
2067 }
2068
2069 void ControlPart::set_fullname(const string& p_fullname)
2070 {
2071 block->set_fullname(p_fullname);
2072 if(w_attrib_path) w_attrib_path->set_fullname(p_fullname + ".<attribpath>");
2073 }
2074
2075 void ControlPart::set_my_scope(Scope *p_scope)
2076 {
2077 bridgeScope.set_parent_scope(p_scope);
2078 string temp("control");
2079 bridgeScope.set_scopeMacro_name(temp);
2080
2081 block->set_my_scope(&bridgeScope);
2082 }
2083
2084 void ControlPart::chk()
2085 {
2086 Error_Context cntxt(this, "In control part");
2087 block->chk();
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();
2093 }
2094 }
2095
2096 void ControlPart::generate_code(output_struct *target, Module *my_module)
2097 {
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",
2101 module_dispname);
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);
2109 }
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");
2114 }
2115
2116 void ControlPart::set_with_attr(MultiWithAttrib* p_attrib)
2117 {
2118 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
2119 w_attrib_path->set_with_attr(p_attrib);
2120 }
2121
2122 WithAttribPath* ControlPart::get_attrib_path()
2123 {
2124 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
2125 return w_attrib_path;
2126 }
2127
2128 void ControlPart::set_parent_path(WithAttribPath* p_path)
2129 {
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);
2133 }
2134
2135 void ControlPart::dump(unsigned level) const
2136 {
2137 DEBUG(level, "Control part");
2138 block->dump(level + 1);
2139 if (w_attrib_path) {
2140 MultiWithAttrib *attrib = w_attrib_path->get_with_attr();
2141 if (attrib) {
2142 DEBUG(level + 1, "Attributes:");
2143 attrib->dump(level + 2);
2144 }
2145 }
2146 }
2147
2148 // =================================
2149 // ===== Module
2150 // =================================
2151
2152 Module::Module(Identifier *p_modid)
2153 : Common::Module(MOD_TTCN, p_modid), language_spec(0), w_attrib_path(0),
2154 controlpart(0)
2155 {
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
2161 }
2162
2163 Module::~Module()
2164 {
2165 delete language_spec;
2166 delete asss;
2167 for (size_t i = 0; i < group_v.size(); i++) delete group_v[i];
2168 group_v.clear();
2169 group_m.clear();
2170 delete imp;
2171 for (size_t i = 0; i < friendmods_v.size(); i++) delete friendmods_v[i];
2172 friendmods_v.clear();
2173 delete controlpart;
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;
2178 }
2179
2180 void Module::add_group(Group* p_group)
2181 {
2182 group_v.add(p_group);
2183 }
2184
2185 void Module::add_friendmod(FriendMod *p_friendmod)
2186 {
2187 friendmods_v.add(p_friendmod);
2188 p_friendmod->set_my_mod(this);
2189 }
2190
2191 Module *Module::clone() const
2192 {
2193 FATAL_ERROR("Ttcn::Module::clone()");
2194 }
2195
2196 Common::Assignment *Module::importAssignment(const Identifier& p_modid,
2197 const Identifier& p_id) const
2198 {
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()");
2202
2203 switch(ass->get_visibility()) {
2204 case FRIEND:
2205 for (size_t i = 0; i < friendmods_v.size(); i++) {
2206 if (friendmods_v[i]->get_modid() == p_modid) return ass;
2207 }
2208 return 0;
2209 case PUBLIC:
2210 return ass;
2211 case PRIVATE:
2212 return 0;
2213 default:
2214 FATAL_ERROR("Ttcn::Module::importAssignment()");
2215 return 0;
2216 }
2217 } else return 0;
2218 }
2219
2220 void Module::set_fullname(const string& p_fullname)
2221 {
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++)
2226 {
2227 group_v[i]->set_fullname(p_fullname + ".<group " + Int2string(i) + ">");
2228 }
2229 if (w_attrib_path) w_attrib_path->set_fullname(p_fullname
2230 + ".<attribpath>");
2231 }
2232
2233 Common::Assignments *Module::get_scope_asss()
2234 {
2235 return asss;
2236 }
2237
2238 bool Module::has_imported_ass_withId(const Identifier& p_id)
2239 {
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;
2245 }
2246 return false;
2247 }
2248
2249 Common::Assignment* Module::get_ass_bySRef(Ref_simple *p_ref)
2250 {
2251 const Identifier *r_modid = p_ref->get_modid();
2252 const Identifier *r_id = p_ref->get_id();
2253 if (r_modid) {
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;
2270
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())) {
2274 t_ass = NULL;
2275 }
2276
2277 if (t_ass != NULL) {
2278 if (result_ass == NULL) {
2279 result_ass = t_ass;
2280 } else if(result_ass != t_ass) {
2281 p_ref->error(
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());
2285 }
2286 }
2287 }
2288 tempusedImpMods.clear();
2289 }
2290 }
2291 if (result_ass) return result_ass;
2292
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());
2297 } else {
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());
2304 } else {
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());
2308 }
2309 } else {
2310 p_ref->error("There is no module with name `%s'",
2311 r_modid->get_dispname().c_str());
2312 }
2313 return 0;
2314 }
2315 } else {
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);
2319 } else {
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());
2323 }
2324 }
2325 } else {
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);
2329 } else {
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);
2334
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();
2339
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())) {
2344 t_ass = NULL;
2345 }
2346
2347 if (t_ass != NULL) {
2348 if (t_result == NULL) {
2349 t_result = t_ass;
2350 } else if(t_result != t_ass) {
2351 p_ref->error(
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());
2355 }
2356 }
2357 }
2358 tempusedImpMods.clear();
2359 }
2360
2361 if (t_result) return t_result;
2362
2363 p_ref->error("There is no local or imported definition with name `%s'"
2364 ,r_id->get_dispname().c_str());
2365 }
2366 }
2367 return 0;
2368 }
2369
2370 bool Module::is_valid_moduleid(const Identifier& p_id)
2371 {
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;
2376 return false;
2377 }
2378
2379 Common::Assignments *Module::get_asss()
2380 {
2381 return asss;
2382 }
2383
2384 bool Module::exports_sym(const Identifier&)
2385 {
2386 FATAL_ERROR("Ttcn::Module::exports_sym()");
2387 return true;
2388 }
2389
2390 Type *Module::get_address_type()
2391 {
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();
2399 }
2400
2401 void Module::chk_imp(ReferenceChain& refch, vector<Common::Module>& moduleStack)
2402 {
2403 if (imp_checked) return;
2404 const string& module_name = modid->get_dispname();
2405
2406 Error_Context backup;
2407 Error_Context cntxt(this, "In TTCN-3 module `%s'", module_name.c_str());
2408 imp->chk_imp(refch, moduleStack);
2409 imp_checked = true;
2410 asss->chk_uniq();
2411 collect_visible_mods();
2412 }
2413
2414 void Module::chk()
2415 {
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();
2422
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.
2432 extatrs = 0;
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();
2436 if (result != 0) {
2437 delete extatrs;
2438 extatrs = 0;
2439 continue;
2440 }
2441
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);
2445
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;
2451 char* extra_junk;
2452 (void)ex.get_id(act_product_number, act_suffix, act_rel, act_patch, act_build, extra_junk);
2453
2454 if (release != UINT_MAX) {
2455 ex.error("Duplicate 'version' attribute");
2456 }
2457 else {
2458 product_number = mcopystr(act_product_number);
2459 suffix = act_suffix;
2460 release = act_rel;
2461 patch = act_patch;
2462 build = act_build;
2463 extra = mcopystr(extra_junk);
2464 }
2465 // Avoid propagating the attribute needlessly
2466 multi->delete_element(i--);
2467 single = 0;
2468 break; }
2469
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;
2474 char* exp_extra;
2475 Common::Identifier *req_id = ex.get_id(exp_product_number,
2476 exp_suffix, exp_rel, exp_patch, exp_build, exp_extra);
2477 // We own req_id
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--);
2486 single = 0;
2487 break;
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);
2502
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--);
2510 single = 0;
2511 break;
2512 }
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);
2521
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--);
2529 single = 0;
2530 break;
2531 }
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);
2545 Free(exp_bld_str);
2546 Free(mod_bld_str);
2547 }
2548 } else {
2549 single->error("No imported module named '%s'",
2550 req_id->get_dispname().c_str());
2551 }
2552 multi->delete_element(i--);
2553 single = 0;
2554 break; }
2555
2556 case Ttcn::ExtensionAttribute::REQ_TITAN: {
2557 char* exp_product_number;
2558 unsigned int exp_suffix, exp_minor, exp_patch, exp_build;
2559 char* exp_extra;
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);
2565 }
2566 if (0 == exp_suffix) {
2567 exp_suffix = 1; // previous version number format did not list the suffix part
2568 }
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);
2579 }
2580 multi->delete_element(i--);
2581 single = 0;
2582 break; }
2583 case Ttcn::ExtensionAttribute::PRINTING: {
2584 ex.error("Attribute 'printing' not allowed at module level");
2585 multi->delete_element(i--);
2586 single = 0;
2587 break;
2588 }
2589
2590 default:
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.
2594 break;
2595 } // switch
2596 } // next a
2597 delete extatrs;
2598 } // next i
2599 }
2600 chk_friends();
2601 chk_groups();
2602 asss->chk_uniq();
2603 asss->chk();
2604 if (controlpart) controlpart->chk();
2605 if (control_ns && !*control_ns) { // set but empty
2606 error("Invalid URI value for control namespace");
2607 }
2608 if (control_ns_prefix && !*control_ns_prefix) { // set but empty
2609 error("Empty NCName for the control namespace prefix is not allowed");
2610 }
2611 // TODO proper URI and NCName validation
2612 }
2613
2614 void Module::chk_friends()
2615 {
2616 map<string, FriendMod> friends_m;
2617
2618 for(size_t i = 0; i < friendmods_v.size(); i++)
2619 {
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))
2624 {
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());
2629 } else {
2630 friends_m.add(friend_name, temp_friend);
2631 }
2632
2633 friendmods_v[i]->chk();
2634 }
2635
2636 friends_m.clear();
2637 }
2638
2639 /** \todo revise */
2640 void Module::chk_groups()
2641 {
2642 map<string,Common::Assignment> ass_m;
2643
2644 for(size_t i = 0; i < asss->get_nof_asss(); i++)
2645 {
2646 Common::Assignment *temp_ass = asss->get_ass_byIndex(i);
2647 if(!temp_ass->get_parent_group())
2648 {
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);
2651 }
2652 }
2653
2654 for(size_t i = 0; i < group_v.size(); i++)
2655 {
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))
2660 {
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());
2665 }
2666 if(group_m.has_key(group_name))
2667 {
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());
2672 }else{
2673 group_m.add(group_name,group_v[i]);
2674 }
2675 }
2676
2677 ass_m.clear();
2678
2679 for(size_t i = 0; i < group_v.size(); i++)
2680 {
2681 group_v[i]->chk();
2682 }
2683 }
2684
2685 void Module::get_imported_mods(module_set_t& p_imported_mods)
2686 {
2687 imp->get_imported_mods(p_imported_mods);
2688 }
2689
2690 void Module::generate_code_internal(CodeGenHelper& cgh) {
2691 imp->generate_code(cgh);
2692 asss->generate_code(cgh);
2693 if (controlpart)
2694 controlpart->generate_code(cgh.get_outputstruct(modid->get_ttcnname()), this);
2695 }
2696
2697 RunsOnScope *Module::get_runs_on_scope(Type *comptype)
2698 {
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();
2703 return ret_val;
2704 }
2705
2706
2707 void Module::dump(unsigned level) const
2708 {
2709 DEBUG(level, "TTCN-3 module: %s", modid->get_dispname().c_str());
2710 level++;
2711 if(imp) imp->dump(level);
2712 if(asss) asss->dump(level);
2713
2714 for(size_t i = 0; i < group_v.size(); i++)
2715 {
2716 group_v[i]->dump(level);
2717 }
2718
2719 if(controlpart) controlpart->dump(level);
2720
2721 if (w_attrib_path) {
2722 MultiWithAttrib *attrib = w_attrib_path->get_with_attr();
2723 if (attrib) {
2724 DEBUG(level, "Module Attributes:");
2725 attrib->dump(level + 1);
2726 }
2727 }
2728 }
2729
2730 void Module::set_language_spec(const char *p_language_spec)
2731 {
2732 if (language_spec) FATAL_ERROR("Module::set_language_spec()");
2733 if (p_language_spec) language_spec = new string(p_language_spec);
2734 }
2735
2736 void Module::add_ass(Definition* p_ass)
2737 {
2738 asss->add_ass(p_ass);
2739 }
2740
2741 void Module::add_impmod(ImpMod *p_impmod)
2742 {
2743 imp->add_impmod(p_impmod);
2744 }
2745
2746 void Module::add_controlpart(ControlPart* p_controlpart)
2747 {
2748 if (!p_controlpart || controlpart) FATAL_ERROR("Module::add_controlpart()");
2749 controlpart = p_controlpart;
2750 controlpart->set_my_scope(asss);
2751 }
2752
2753 void Module::set_with_attr(MultiWithAttrib* p_attrib)
2754 {
2755 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
2756 w_attrib_path->set_with_attr(p_attrib);
2757 }
2758
2759 WithAttribPath* Module::get_attrib_path()
2760 {
2761 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
2762 return w_attrib_path;
2763 }
2764
2765 void Module::set_parent_path(WithAttribPath* p_path)
2766 {
2767 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
2768 w_attrib_path->set_parent(p_path);
2769 }
2770
2771 bool Module::is_visible(const Identifier& id, visibility_t visibility){
2772
2773 if (visibility== PUBLIC) {
2774 return true;
2775 }
2776 if (visibility== FRIEND) {
2777 for (size_t i = 0; i < friendmods_v.size(); i++) {
2778 if (friendmods_v[i]->get_modid() == id) {
2779 return true;
2780 }
2781 }
2782 }
2783 return false;
2784 }
2785
2786 void Module::generate_json_schema(JSON_Tokenizer& json, map<Type*, JSON_Tokenizer>& json_refs)
2787 {
2788 // add a new property for this module
2789 json.put_next_token(JSON_TOKEN_NAME, modid->get_ttcnname().c_str());
2790
2791 // add type definitions into an object
2792 json.put_next_token(JSON_TOKEN_OBJECT_START);
2793
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));
2797 if (def != NULL) {
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);
2802
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);
2808 }
2809 }
2810 }
2811 }
2812
2813 // end of type definitions
2814 json.put_next_token(JSON_TOKEN_OBJECT_END);
2815
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));
2819 if (def != NULL) {
2820 def->generate_json_schema_ref(json_refs);
2821 }
2822 }
2823 }
2824
2825 void Module::generate_debugger_init(output_struct* output)
2826 {
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"
2832 "{\n"
2833 "%s", first ? " ttcn3_debugger.activate();\n" : "");
2834 first = FALSE;
2835
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);
2840 }
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"
2846 "%s",
2847 get_modid().get_dispname().c_str(), str_glob);
2848 Free(str_glob);
2849 }
2850
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));
2854 if (def != NULL) {
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());
2862 }
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"
2868 "%s"
2869 , comp_type->get_dispname().c_str(), comp_type->get_dispname().c_str()
2870 , comp_type->get_dispname().c_str(), str_comp);
2871 Free(str_comp);
2872 }
2873 }
2874 }
2875 }
2876
2877 // close the initializer function
2878 output->source.global_vars = mputstr(output->source.global_vars, "}\n");
2879 }
2880
2881 char* Module::generate_debugger_global_vars(char* str, Common::Module* current_mod)
2882 {
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++
2889 break;
2890 }
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");
2896 break;
2897 case Common::Assignment::A_EXT_CONST: {
2898 Def_ExtConst* def = dynamic_cast<Def_ExtConst*>(ass);
2899 if (def == NULL) {
2900 FATAL_ERROR("Module::generate_debugger_global_vars");
2901 }
2902 if (def->is_used()) {
2903 str = generate_code_debugger_add_var(str, ass, current_mod, "global");
2904 }
2905 break; }
2906 default:
2907 break;
2908 }
2909 }
2910 return str;
2911 }
2912
2913 void Module::generate_debugger_functions(output_struct *output)
2914 {
2915 char* print_str = NULL;
2916 char* overwrite_str = NULL;
2917 for (size_t i = 0; i < asss->get_nof_asss(); ++i) {
2918 Def_Type* def = dynamic_cast<Def_Type*>(asss->get_ass_byIndex(i));
2919 if (def != NULL) {
2920 Type* t = def->get_Type();
2921 if (!t->is_ref() && t->get_typetype() != Type::T_COMPONENT) {
2922 // don't generate code for subtypes
2923 if (t->get_typetype() != Type::T_SIGNATURE) {
2924 print_str = mputprintf(print_str,
2925 " %sif (!strcmp(p_var.type_name, \"%s\")) {\n"
2926 " ((const %s*)ptr)->log();\n"
2927 " }\n"
2928 , (print_str != NULL) ? "else " : ""
2929 , t->get_dispname().c_str(), t->get_genname_value(this).c_str());
2930 if (t->get_typetype() != Type::T_PORT) {
2931 overwrite_str = mputprintf(overwrite_str,
2932 " %sif (!strcmp(p_var.type_name, \"%s\")) {\n"
2933 " ((%s*)p_var.value)->set_param(p_new_value);\n"
2934 " }\n"
2935 , (overwrite_str != NULL) ? "else " : ""
2936 , t->get_dispname().c_str(), t->get_genname_value(this).c_str());
2937 }
2938 }
2939 if (t->get_typetype() != Type::T_PORT) {
2940 print_str = mputprintf(print_str,
2941 " %sif (!strcmp(p_var.type_name, \"%s template\")) {\n"
2942 " ((const %s_template*)ptr)->log();\n"
2943 " }\n"
2944 , (print_str != NULL) ? "else " : ""
2945 , t->get_dispname().c_str(), t->get_genname_value(this).c_str());
2946 if (t->get_typetype() != Type::T_SIGNATURE) {
2947 overwrite_str = mputprintf(overwrite_str,
2948 " %sif (!strcmp(p_var.type_name, \"%s template\")) {\n"
2949 " ((%s_template*)p_var.value)->set_param(p_new_value);\n"
2950 " }\n"
2951 , (overwrite_str != NULL) ? "else " : ""
2952 , t->get_dispname().c_str(), t->get_genname_value(this).c_str());
2953 }
2954 }
2955 }
2956 }
2957 }
2958 if (print_str != NULL) {
2959 // don't generate an empty printing function
2960 output->header.class_defs = mputprintf(output->header.class_defs,
2961 "/* Debugger printing and overwriting functions for types declared in this module */\n\n"
2962 "extern CHARSTRING print_var_%s(const TTCN3_Debugger::variable_t& p_var);\n",
2963 get_modid().get_ttcnname().c_str());
2964 output->source.global_vars = mputprintf(output->source.global_vars,
2965 "\n/* Debugger printing function for types declared in this module */\n"
2966 "CHARSTRING print_var_%s(const TTCN3_Debugger::variable_t& p_var)\n"
2967 "{\n"
2968 " const void* ptr = p_var.set_function != NULL ? p_var.value : p_var.cvalue;\n"
2969 " TTCN_Logger::begin_event_log2str();\n"
2970 "%s"
2971 " else {\n"
2972 " TTCN_Logger::log_event_str(\"<unrecognized value or template>\");\n"
2973 " }\n"
2974 " return TTCN_Logger::end_event_log2str();\n"
2975 "}\n", get_modid().get_ttcnname().c_str(), print_str);
2976 }
2977 if (overwrite_str != NULL) {
2978 // don't generate an empty overwriting function
2979 output->header.class_defs = mputprintf(output->header.class_defs,
2980 "extern boolean set_var_%s(TTCN3_Debugger::variable_t& p_var, Module_Param& p_new_value);\n",
2981 get_modid().get_ttcnname().c_str());
2982 output->source.global_vars = mputprintf(output->source.global_vars,
2983 "\n/* Debugger overwriting function for types declared in this module */\n"
2984 "boolean set_var_%s(TTCN3_Debugger::variable_t& p_var, Module_Param& p_new_value)\n"
2985 "{\n"
2986 "%s"
2987 " else {\n"
2988 " return FALSE;\n"
2989 " }\n"
2990 " return TRUE;\n"
2991 "}\n", get_modid().get_ttcnname().c_str(), overwrite_str);
2992 }
2993 }
2994
2995 // =================================
2996 // ===== Definition
2997 // =================================
2998
2999 string Definition::get_genname() const
3000 {
3001 if (!genname.empty()) return genname;
3002 else return id->get_name();
3003 }
3004
3005 namedbool Definition::has_implicit_omit_attr() const {
3006 if (w_attrib_path) {
3007 const vector<SingleWithAttrib>& real_attribs =
3008 w_attrib_path->get_real_attrib();
3009 for (size_t in = real_attribs.size(); in > 0; in--) {
3010 if (SingleWithAttrib::AT_OPTIONAL ==
3011 real_attribs[in-1]->get_attribKeyword()) {
3012 if ("implicit omit" ==
3013 real_attribs[in-1]->get_attribSpec().get_spec()) {
3014 return IMPLICIT_OMIT;
3015 } else if ("explicit omit" ==
3016 real_attribs[in-1]->get_attribSpec().get_spec()) {
3017 return NOT_IMPLICIT_OMIT;
3018 } // error reporting for other values is in chk_global_attrib
3019 }
3020 }
3021 }
3022 return NOT_IMPLICIT_OMIT;
3023 }
3024
3025 Definition::~Definition()
3026 {
3027 delete w_attrib_path;
3028 delete erroneous_attrs;
3029 }
3030
3031 void Definition::set_fullname(const string& p_fullname)
3032 {
3033 Common::Assignment::set_fullname(p_fullname);
3034 if (w_attrib_path) w_attrib_path->set_fullname(p_fullname + ".<attribpath>");
3035 if (erroneous_attrs) erroneous_attrs->set_fullname(p_fullname+".<erroneous_attributes>");
3036 }
3037
3038 bool Definition::is_local() const
3039 {
3040 if (!my_scope) FATAL_ERROR("Definition::is_local()");
3041 for (Scope *scope = my_scope; scope; scope = scope->get_parent_scope()) {
3042 if (dynamic_cast<StatementBlock*>(scope)) return true;
3043 }
3044 return false;
3045 }
3046
3047 bool Definition::chk_identical(Definition *)
3048 {
3049 FATAL_ERROR("Definition::chk_identical()");
3050 return false;
3051 }
3052
3053 void Definition::chk_erroneous_attr()
3054 {
3055 if (!w_attrib_path) return;
3056 const Ttcn::MultiWithAttrib* attribs = w_attrib_path->get_local_attrib();
3057 if (!attribs) return;
3058 for (size_t i = 0; i < attribs->get_nof_elements(); i++) {
3059 const Ttcn::SingleWithAttrib* act_attr = attribs->get_element(i);
3060 if (act_attr->get_attribKeyword()==Ttcn::SingleWithAttrib::AT_ERRONEOUS) {
3061 if (!use_runtime_2) {
3062 error("`erroneous' attributes can be used only with the Function Test Runtime");
3063 note("If you need negative testing use the -R flag when generating the makefile");
3064 return;
3065 }
3066 size_t nof_qualifiers = act_attr->get_attribQualifiers() ? act_attr->get_attribQualifiers()->get_nof_qualifiers() : 0;
3067 dynamic_array<Type*> refd_type_array(nof_qualifiers); // only the qualifiers pointing to existing fields will be added to erroneous_attrs objects
3068 if (nof_qualifiers==0) {
3069 act_attr->error("At least one qualifier must be specified for the `erroneous' attribute");
3070 } else {
3071 // check if qualifiers point to existing fields
3072 for (size_t qi=0; qi<nof_qualifiers; qi++) {
3073 Qualifier* act_qual = const_cast<Qualifier*>(act_attr->get_attribQualifiers()->get_qualifier(qi));
3074 act_qual->set_my_scope(get_my_scope());
3075 Type* field_type = get_Type()->get_field_type(act_qual, Type::EXPECTED_CONSTANT);
3076 if (field_type) {
3077 dynamic_array<size_t> subrefs_array;
3078 dynamic_array<Type*> type_array;
3079 bool valid_indexes = get_Type()->get_subrefs_as_array(act_qual, subrefs_array, type_array);
3080 if (!valid_indexes) field_type = NULL;
3081 if (act_qual->refers_to_string_element()) {
3082 act_qual->error("Reference to a string element cannot be used in this context");
3083 field_type = NULL;
3084 }
3085 }
3086 refd_type_array.add(field_type);
3087 }
3088 }
3089 // parse the attr. spec.
3090 ErroneousAttributeSpec* err_attr_spec = ttcn3_parse_erroneous_attr_spec_string(
3091 act_attr->get_attribSpec().get_spec().c_str(), act_attr->get_attribSpec());
3092 if (err_attr_spec) {
3093 if (!erroneous_attrs) erroneous_attrs = new ErroneousAttributes(get_Type());
3094 // attr.spec will be owned by erroneous_attrs object
3095 erroneous_attrs->add_spec(err_attr_spec);
3096 err_attr_spec->set_fullname(get_fullname());
3097 err_attr_spec->set_my_scope(get_my_scope());
3098 err_attr_spec->chk();
3099 // create qualifier - err.attr.spec. pairs
3100 for (size_t qi=0; qi<nof_qualifiers; qi++) {
3101 if (refd_type_array[qi] && (err_attr_spec->get_indicator()!=ErroneousAttributeSpec::I_INVALID)) {
3102 erroneous_attrs->add_pair(act_attr->get_attribQualifiers()->get_qualifier(qi), err_attr_spec);
3103 }
3104 }
3105 }
3106 }
3107 }
3108 if (erroneous_attrs) erroneous_attrs->chk();
3109 }
3110
3111 char* Definition::generate_code_str(char *str)
3112 {
3113 FATAL_ERROR("Definition::generate_code_str()");
3114 return str;
3115 }
3116
3117 void Definition::ilt_generate_code(ILT *)
3118 {
3119 FATAL_ERROR("Definition::ilt_generate_code()");
3120 }
3121
3122 char *Definition::generate_code_init_comp(char *str, Definition *)
3123 {
3124 FATAL_ERROR("Definition::generate_code_init_comp()");
3125 return str;
3126 }
3127
3128 void Definition::set_with_attr(MultiWithAttrib* p_attrib)
3129 {
3130 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
3131 w_attrib_path->set_with_attr(p_attrib);
3132 }
3133
3134 WithAttribPath* Definition::get_attrib_path()
3135 {
3136 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
3137 return w_attrib_path;
3138 }
3139
3140 void Definition::set_parent_path(WithAttribPath* p_path)
3141 {
3142 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
3143 w_attrib_path->set_parent(p_path);
3144 }
3145
3146 void Definition::set_parent_group(Group* p_group)
3147 {
3148 if(parentgroup) // there would be a leak!
3149 FATAL_ERROR("Definition::set_parent_group()");
3150 parentgroup = p_group;
3151 }
3152
3153 Group* Definition::get_parent_group()
3154 {
3155 return parentgroup;
3156 }
3157
3158 void Definition::dump_internal(unsigned level) const
3159 {
3160 DEBUG(level, "Move along, nothing to see here");
3161 }
3162
3163 void Definition::dump(unsigned level) const
3164 {
3165 dump_internal(level);
3166 if (w_attrib_path) {
3167 MultiWithAttrib *attrib = w_attrib_path->get_with_attr();
3168 if (attrib) {
3169 DEBUG(level + 1, "Definition Attributes:");
3170 attrib->dump(level + 2);
3171 }
3172 }
3173 if (erroneous_attrs) erroneous_attrs->dump(level+1);
3174 }
3175
3176 // =================================
3177 // ===== Def_Type
3178 // =================================
3179
3180 Def_Type::Def_Type(Identifier *p_id, Type *p_type)
3181 : Definition(A_TYPE, p_id), type(p_type)
3182 {
3183 if(!p_type) FATAL_ERROR("Ttcn::Def_Type::Def_Type()");
3184 type->set_ownertype(Type::OT_TYPE_DEF, this);
3185 }
3186
3187 Def_Type::~Def_Type()
3188 {
3189 delete type;
3190 }
3191
3192 Def_Type *Def_Type::clone() const
3193 {
3194 FATAL_ERROR("Def_Type::clone");
3195 }
3196
3197 void Def_Type::set_fullname(const string& p_fullname)
3198 {
3199 Definition::set_fullname(p_fullname);
3200 type->set_fullname(p_fullname);
3201 }
3202
3203 void Def_Type::set_my_scope(Scope *p_scope)
3204 {
3205 bridgeScope.set_parent_scope(p_scope);
3206 bridgeScope.set_scopeMacro_name(id->get_dispname());
3207
3208 Definition::set_my_scope(&bridgeScope);
3209 type->set_my_scope(&bridgeScope);
3210
3211 }
3212
3213 Setting *Def_Type::get_Setting()
3214 {
3215 return get_Type();
3216 }
3217
3218 Type *Def_Type::get_Type()
3219 {
3220 chk();
3221 return type;
3222 }
3223
3224 void Def_Type::chk()
3225 {
3226 if (checked) return;
3227 checked = true;
3228 Error_Context cntxt(this, "In %s definition `%s'",
3229 type->get_typetype() == Type::T_SIGNATURE ? "signature" : "type",
3230 id->get_dispname().c_str());
3231 type->set_genname(get_genname());
3232 if (!semantic_check_only && type->get_typetype() == Type::T_COMPONENT) {
3233 // the prefix of embedded definitions must be set before the checking
3234 type->get_CompBody()->set_genname(get_genname() + "_component_");
3235 }
3236
3237 while (w_attrib_path) { // not a loop, but we can _break_ out of it
3238 w_attrib_path->chk_global_attrib();
3239 w_attrib_path->chk_no_qualif();
3240 if (type->get_typetype() != Type::T_ANYTYPE) break;
3241 // This is the anytype; it must be empty (we're about to add the fields)
3242 if (type->get_nof_comps() > 0) FATAL_ERROR("Def_Type::chk");
3243
3244 Ttcn::ExtensionAttributes *extattrs = parse_extattributes(w_attrib_path);
3245 if (extattrs == 0) break; // NULL means parsing error
3246
3247 size_t num_atrs = extattrs->size();
3248 for (size_t k = 0; k < num_atrs; ++k) {
3249 ExtensionAttribute &ea = extattrs->get(k);
3250 switch (ea.get_type()) {
3251 case ExtensionAttribute::ANYTYPELIST: {
3252 Types *anytypes = ea.get_types();
3253 // List of types to be converted into fields for the anytype.
3254 // Make sure scope is set on all types in the list.
3255 anytypes->set_my_scope(get_my_scope());
3256
3257 // Convert the list of types into field names for the anytype
3258 for (size_t i=0; i < anytypes->get_nof_types(); ++i) {
3259 Type *t = anytypes->extract_type_byIndex(i);
3260 // we are now the owner of the Type.
3261 if (t->get_typetype()==Type::T_ERROR) { // should we give up?
3262 delete t;
3263 continue;
3264 }
3265
3266 string field_name;
3267 const char* btn = Type::get_typename_builtin(t->get_typetype());
3268 if (btn) {
3269 field_name = btn;
3270 }
3271 else if (t->get_typetype() == Type::T_REFD) {
3272 // Extract the identifier
3273 Common::Reference *ref = t->get_Reference();
3274 Ttcn::Reference *tref = dynamic_cast<Ttcn::Reference*>(ref);
3275 if (!tref) FATAL_ERROR("Def_Type::chk, wrong kind of reference");
3276 const Common::Identifier *modid = tref->get_modid();
3277 if (modid) {
3278 ea.error("Qualified name '%s' cannot be added to the anytype",
3279 tref->get_dispname().c_str());
3280 delete t;
3281 continue;
3282 }
3283 field_name = tref->get_id()->get_ttcnname();
3284 }
3285 else {
3286 // Can't happen here
3287 FATAL_ERROR("Unexpected type %d", t->get_typetype());
3288 }
3289
3290 const string& at_field = anytype_field(field_name);
3291 Identifier *field_id = new Identifier(Identifier::ID_TTCN, at_field);
3292 CompField *cf = new CompField(field_id, t, false, 0);
3293 cf->set_location(ea);
3294 cf->set_fullname(get_fullname());
3295 type->add_comp(cf);
3296 } // next i
3297 delete anytypes;
3298 break; }
3299 default:
3300 w_attrib_path->get_with_attr()->error("Type def can only have anytype");
3301 break;
3302 } // switch
3303 } // next attribute
3304
3305 delete extattrs;
3306 break; // do not loop
3307 }
3308
3309 // Now we can check the type
3310 type->chk();
3311 type->chk_constructor_name(*id);
3312 if (id->get_ttcnname() == "address") type->chk_address();
3313 ReferenceChain refch(type, "While checking embedded recursions");
3314 type->chk_recursions(refch);
3315
3316 if (type->get_typetype()==Type::T_FUNCTION
3317 ||type->get_typetype()==Type::T_ALTSTEP
3318 ||type->get_typetype()==Type::T_TESTCASE) {
3319 // TR 922. This is a function/altstep/testcase reference.
3320 // Set this definition as the definition for the formal parameters.
3321 type->get_fat_parameters()->set_my_def(this);
3322 }
3323 }
3324
3325 void Def_Type::generate_code(output_struct *target, bool)
3326 {
3327 type->generate_code(target);
3328 if (type->get_typetype() == Type::T_COMPONENT) {
3329 // the C++ equivalents of embedded component element definitions must be
3330 // generated from outside Type::generate_code() because the function can
3331 // call itself recursively and create invalid (overlapped) initializer
3332 // sequences
3333 type->get_CompBody()->generate_code(target);
3334 }
3335 }
3336
3337 void Def_Type::generate_code(CodeGenHelper& cgh) {
3338 type->generate_code(cgh.get_outputstruct(get_Type()));
3339 if (type->get_typetype() == Type::T_COMPONENT) {
3340 // the C++ equivalents of embedded component element definitions must be
3341 // generated from outside Type::generate_code() because the function can
3342 // call itself recursively and create invalid (overlapped) initializer
3343 // sequences
3344 type->get_CompBody()->generate_code(cgh.get_current_outputstruct());
3345 }
3346 cgh.finalize_generation(get_Type());
3347 }
3348
3349
3350 void Def_Type::dump_internal(unsigned level) const
3351 {
3352 DEBUG(level, "Type def: %s @ %p", id->get_dispname().c_str(), (const void*)this);
3353 type->dump(level + 1);
3354 }
3355
3356 void Def_Type::set_with_attr(MultiWithAttrib* p_attrib)
3357 {
3358 if (!w_attrib_path) {
3359 w_attrib_path = new WithAttribPath();
3360 type->set_parent_path(w_attrib_path);
3361 }
3362 type->set_with_attr(p_attrib);
3363 }
3364
3365 WithAttribPath* Def_Type::get_attrib_path()
3366 {
3367 if (!w_attrib_path) {
3368 w_attrib_path = new WithAttribPath();
3369 type->set_parent_path(w_attrib_path);
3370 }
3371 return w_attrib_path;
3372 }
3373
3374 void Def_Type::set_parent_path(WithAttribPath* p_path)
3375 {
3376 if (!w_attrib_path) {
3377 w_attrib_path = new WithAttribPath();
3378 type->set_parent_path(w_attrib_path);
3379 }
3380 w_attrib_path->set_parent(p_path);
3381 }
3382
3383 // =================================
3384 // ===== Def_Const
3385 // =================================
3386
3387 Def_Const::Def_Const(Identifier *p_id, Type *p_type, Value *p_value)
3388 : Definition(A_CONST, p_id)
3389 {
3390 if (!p_type || !p_value) FATAL_ERROR("Ttcn::Def_Const::Def_Const()");
3391 type=p_type;
3392 type->set_ownertype(Type::OT_CONST_DEF, this);
3393 value=p_value;
3394 value_under_check=false;
3395 }
3396
3397 Def_Const::~Def_Const()
3398 {
3399 delete type;
3400 delete value;
3401 }
3402
3403 Def_Const *Def_Const::clone() const
3404 {
3405 FATAL_ERROR("Def_Const::clone");
3406 }
3407
3408 void Def_Const::set_fullname(const string& p_fullname)
3409 {
3410 Definition::set_fullname(p_fullname);
3411 type->set_fullname(p_fullname + ".<type>");
3412 value->set_fullname(p_fullname);
3413 }
3414
3415 void Def_Const::set_my_scope(Scope *p_scope)
3416 {
3417 Definition::set_my_scope(p_scope);
3418 type->set_my_scope(p_scope);
3419 value->set_my_scope(p_scope);
3420 }
3421
3422 Setting *Def_Const::get_Setting()
3423 {
3424 return get_Value();
3425 }
3426
3427 Type *Def_Const::get_Type()
3428 {
3429 chk();
3430 return type;
3431 }
3432
3433
3434 Value *Def_Const::get_Value()
3435 {
3436 chk();
3437 return value;
3438 }
3439
3440 void Def_Const::chk()
3441 {
3442 if(checked) {
3443 if (value_under_check) {
3444 error("Circular reference in constant definition `%s'",
3445 id->get_dispname().c_str());
3446 value_under_check = false; // only report the error once for this definition
3447 }
3448 return;
3449 }
3450 Error_Context cntxt(this, "In constant definition `%s'",
3451 id->get_dispname().c_str());
3452 type->set_genname(_T_, get_genname());
3453 type->chk();
3454 value->set_my_governor(type);
3455 type->chk_this_value_ref(value);
3456 checked=true;
3457 if (w_attrib_path) {
3458 w_attrib_path->chk_global_attrib(true);
3459 switch (type->get_type_refd_last()->get_typetype_ttcn3()) {
3460 case Type::T_SEQ_T:
3461 case Type::T_SET_T:
3462 case Type::T_CHOICE_T:
3463 // These types may have qualified attributes
3464 break;
3465 case Type::T_SEQOF: case Type::T_SETOF:
3466 break;
3467 default:
3468 w_attrib_path->chk_no_qualif();
3469 break;
3470 }
3471 }
3472 Type *t = type->get_type_refd_last();
3473 switch (t->get_typetype()) {
3474 case Type::T_PORT:
3475 error("Constant cannot be defined for port type `%s'",
3476 t->get_fullname().c_str());
3477 break;
3478 case Type::T_SIGNATURE:
3479 error("Constant cannot be defined for signature `%s'",
3480 t->get_fullname().c_str());
3481 break;
3482 default:
3483 value_under_check = true;
3484 type->chk_this_value(value, 0, Type::EXPECTED_CONSTANT, INCOMPLETE_ALLOWED,
3485 OMIT_NOT_ALLOWED, SUB_CHK, has_implicit_omit_attr());
3486 value_under_check = false;
3487 chk_erroneous_attr();
3488 if (erroneous_attrs) value->set_err_descr(erroneous_attrs->get_err_descr());
3489 {
3490 ReferenceChain refch(type, "While checking embedded recursions");
3491 value->chk_recursions(refch);
3492 }
3493 break;
3494 }
3495 if (!semantic_check_only) {
3496 value->set_genname_prefix("const_");
3497 value->set_genname_recursive(get_genname());
3498 value->set_code_section(GovernedSimple::CS_PRE_INIT);
3499 }
3500 }
3501
3502 bool Def_Const::chk_identical(Definition *p_def)
3503 {
3504 chk();
3505 p_def->chk();
3506 if (p_def->get_asstype() != A_CONST) {
3507 const char *dispname_str = id->get_dispname().c_str();
3508 error("Local definition `%s' is a constant, but the definition "
3509 "inherited from component type `%s' is a %s", dispname_str,
3510 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
3511 p_def->note("The inherited definition of `%s' is here", dispname_str);
3512 return false;
3513 }
3514 Def_Const *p_def_const = dynamic_cast<Def_Const*>(p_def);
3515 if (!p_def_const) FATAL_ERROR("Def_Const::chk_identical()");
3516 if (!type->is_identical(p_def_const->type)) {
3517 const char *dispname_str = id->get_dispname().c_str();
3518 type->error("Local constant `%s' has type `%s', but the constant "
3519 "inherited from component type `%s' has type `%s'", dispname_str,
3520 type->get_typename().c_str(),
3521 p_def_const->get_my_scope()->get_fullname().c_str(),
3522 p_def_const->type->get_typename().c_str());
3523 p_def_const->note("The inherited constant `%s' is here", dispname_str);
3524 return false;
3525 } else if (!(*value == *p_def_const->value)) {
3526 const char *dispname_str = id->get_dispname().c_str();
3527 value->error("Local constant `%s' and the constant inherited from "
3528 "component type `%s' have different values", dispname_str,
3529 p_def_const->get_my_scope()->get_fullname().c_str());
3530 p_def_const->note("The inherited constant `%s' is here", dispname_str);
3531 return false;
3532 } else return true;
3533 }
3534
3535 void Def_Const::generate_code(output_struct *target, bool)
3536 {
3537 type->generate_code(target);
3538 const_def cdef;
3539 Code::init_cdef(&cdef);
3540 type->generate_code_object(&cdef, value);
3541 cdef.init = update_location_object(cdef.init);
3542 cdef.init = value->generate_code_init(cdef.init,
3543 value->get_lhs_name().c_str());
3544 Code::merge_cdef(target, &cdef);
3545 Code::free_cdef(&cdef);
3546 }
3547
3548 void Def_Const::generate_code(Common::CodeGenHelper& cgh) {
3549 // constant definitions always go to its containing module
3550 generate_code(cgh.get_current_outputstruct());
3551 }
3552
3553 char *Def_Const::generate_code_str(char *str)
3554 {
3555 const string& t_genname = get_genname();
3556 const char *genname_str = t_genname.c_str();
3557 if (value->has_single_expr()) {
3558 // the value can be represented by a single C++ expression
3559 // the object is initialized by the constructor
3560 str = mputprintf(str, "%s %s(%s);\n",
3561 type->get_genname_value(my_scope).c_str(), genname_str,
3562 value->get_single_expr().c_str());
3563 } else {
3564 // use the default constructor
3565 str = mputprintf(str, "%s %s;\n",
3566 type->get_genname_value(my_scope).c_str(), genname_str);
3567 // the value is assigned using subsequent statements
3568 str = value->generate_code_init(str, genname_str);
3569 }
3570 if (debugger_active) {
3571 str = generate_code_debugger_add_var(str, this);
3572 }
3573 return str;
3574 }
3575
3576 void Def_Const::ilt_generate_code(ILT *ilt)
3577 {
3578 const string& t_genname = get_genname();
3579 const char *genname_str = t_genname.c_str();
3580 char*& def=ilt->get_out_def();
3581 char*& init=ilt->get_out_branches();
3582 def = mputprintf(def, "%s %s;\n", type->get_genname_value(my_scope).c_str(),
3583 genname_str);
3584 init = value->generate_code_init(init, genname_str);
3585 }
3586
3587 char *Def_Const::generate_code_init_comp(char *str, Definition *)
3588 {
3589 /* This function actually does nothing as \a this and \a base_defn are
3590 * exactly the same. */
3591 return str;
3592 }
3593
3594 void Def_Const::dump_internal(unsigned level) const
3595 {
3596 DEBUG(level, "Constant: %s @%p", id->get_dispname().c_str(), (const void*)this);
3597 type->dump(level + 1);
3598 value->dump(level + 1);
3599 }
3600
3601 // =================================
3602 // ===== Def_ExtConst
3603 // =================================
3604
3605 Def_ExtConst::Def_ExtConst(Identifier *p_id, Type *p_type)
3606 : Definition(A_EXT_CONST, p_id)
3607 {
3608 if (!p_type) FATAL_ERROR("Ttcn::Def_ExtConst::Def_ExtConst()");
3609 type = p_type;
3610 type->set_ownertype(Type::OT_CONST_DEF, this);
3611 usage_found = false;
3612 }
3613
3614 Def_ExtConst::~Def_ExtConst()
3615 {
3616 delete type;
3617 }
3618
3619 Def_ExtConst *Def_ExtConst::clone() const
3620 {
3621 FATAL_ERROR("Def_ExtConst::clone");
3622 }
3623
3624 void Def_ExtConst::set_fullname(const string& p_fullname)
3625 {
3626 Definition::set_fullname(p_fullname);
3627 type->set_fullname(p_fullname + ".<type>");
3628 }
3629
3630 void Def_ExtConst::set_my_scope(Scope *p_scope)
3631 {
3632 Definition::set_my_scope(p_scope);
3633 type->set_my_scope(p_scope);
3634 }
3635
3636 Type *Def_ExtConst::get_Type()
3637 {
3638 chk();
3639 return type;
3640 }
3641
3642 void Def_ExtConst::chk()
3643 {
3644 if(checked) return;
3645 Error_Context cntxt(this, "In external constant definition `%s'",
3646 id->get_dispname().c_str());
3647 type->set_genname(_T_, get_genname());
3648 type->chk();
3649 checked=true;
3650 Type *t = type->get_type_refd_last();
3651 switch (t->get_typetype()) {
3652 case Type::T_PORT:
3653 error("External constant cannot be defined for port type `%s'",
3654 t->get_fullname().c_str());
3655 break;
3656 case Type::T_SIGNATURE:
3657 error("External constant cannot be defined for signature `%s'",
3658 t->get_fullname().c_str());
3659 break;
3660 default:
3661 break;
3662 }
3663 if (w_attrib_path) {
3664 w_attrib_path->chk_global_attrib();
3665 switch (type->get_type_refd_last()->get_typetype()) {
3666 case Type::T_SEQ_T:
3667 case Type::T_SET_T:
3668 case Type::T_CHOICE_T:
3669 // These types may have qualified attributes
3670 break;
3671 case Type::T_SEQOF: case Type::T_SETOF:
3672 break;
3673 default:
3674 w_attrib_path->chk_no_qualif();
3675 break;
3676 }
3677 }
3678 }
3679
3680 void Def_ExtConst::generate_code(output_struct *target, bool)
3681 {
3682 type->generate_code(target);
3683 target->header.global_vars = mputprintf(target->header.global_vars,
3684 "extern const %s& %s;\n", type->get_genname_value(my_scope).c_str(),
3685 get_genname().c_str());
3686 }
3687
3688 void Def_ExtConst::generate_code(Common::CodeGenHelper& cgh) {
3689 // constant definitions always go to its containing module
3690 generate_code(cgh.get_current_outputstruct());
3691 }
3692
3693 void Def_ExtConst::dump_internal(unsigned level) const
3694 {
3695 DEBUG(level, "External constant: %s @ %p", id->get_dispname().c_str(), (const void*)this);
3696 type->dump(level + 1);
3697 }
3698
3699 // =================================
3700 // ===== Def_Modulepar
3701 // =================================
3702
3703 Def_Modulepar::Def_Modulepar(Identifier *p_id, Type *p_type, Value *p_defval)
3704 : Definition(A_MODULEPAR, p_id)
3705 {
3706 if (!p_type) FATAL_ERROR("Ttcn::Def_Modulepar::Def_Modulepar()");
3707 type = p_type;
3708 type->set_ownertype(Type::OT_MODPAR_DEF, this);
3709 def_value = p_defval;
3710 }
3711
3712 Def_Modulepar::~Def_Modulepar()
3713 {
3714 delete type;
3715 delete def_value;
3716 }
3717
3718 Def_Modulepar* Def_Modulepar::clone() const
3719 {
3720 FATAL_ERROR("Def_Modulepar::clone");
3721 }
3722
3723 void Def_Modulepar::set_fullname(const string& p_fullname)
3724 {
3725 Definition::set_fullname(p_fullname);
3726 type->set_fullname(p_fullname + ".<type>");
3727 if (def_value) def_value->set_fullname(p_fullname + ".<default_value>");
3728 }
3729
3730 void Def_Modulepar::set_my_scope(Scope *p_scope)
3731 {
3732 Definition::set_my_scope(p_scope);
3733 type->set_my_scope(p_scope);
3734 if (def_value) def_value->set_my_scope(p_scope);
3735 }
3736
3737 Type *Def_Modulepar::get_Type()
3738 {
3739 chk();
3740 return type;
3741 }
3742
3743 void Def_Modulepar::chk()
3744 {
3745 if(checked) return;
3746 Error_Context cntxt(this, "In module parameter definition `%s'",
3747 id->get_dispname().c_str());
3748 type->set_genname(_T_, get_genname());
3749 type->chk();
3750 if (w_attrib_path) {
3751 w_attrib_path->chk_global_attrib();
3752 switch (type->get_type_refd_last()->get_typetype()) {
3753 case Type::T_SEQ_T:
3754 case Type::T_SET_T:
3755 case Type::T_CHOICE_T:
3756 // These types may have qualified attributes
3757 break;
3758 case Type::T_SEQOF: case Type::T_SETOF:
3759 break;
3760 default:
3761 w_attrib_path->chk_no_qualif();
3762 break;
3763 }
3764 }
3765 map<Type*,void> type_chain;
3766 map<Type::typetype_t, void> not_allowed;
3767 not_allowed.add(Type::T_PORT, 0);
3768 Type *t = type->get_type_refd_last();
3769 // if the type is valid the original will be returned
3770 Type::typetype_t tt = t->search_for_not_allowed_type(type_chain, not_allowed);
3771 type_chain.clear();
3772 not_allowed.clear();
3773 switch (tt) {
3774 case Type::T_PORT:
3775 error("Type of module parameter cannot be or embed port type `%s'",
3776 t->get_fullname().c_str());
3777 break;
3778 case Type::T_SIGNATURE:
3779 error("Type of module parameter cannot be signature `%s'",
3780 t->get_fullname().c_str());
3781 break;
3782 case Type::T_FUNCTION:
3783 case Type::T_ALTSTEP:
3784 case Type::T_TESTCASE:
3785 if (t->get_fat_runs_on_self()) {
3786 error("Type of module parameter cannot be of function reference type"
3787 " `%s' which has runs on self clause", t->get_fullname().c_str());
3788 }
3789 break;
3790 default:
3791 #if defined(MINGW)
3792 checked = true;
3793 #else
3794 if (def_value) {
3795 Error_Context cntxt2(def_value, "In default value");
3796 def_value->set_my_governor(type);
3797 type->chk_this_value_ref(def_value);
3798 checked = true;
3799 type->chk_this_value(def_value, 0, Type::EXPECTED_CONSTANT, INCOMPLETE_ALLOWED,
3800 OMIT_NOT_ALLOWED, SUB_CHK, has_implicit_omit_attr());
3801 if (!semantic_check_only) {
3802 def_value->set_genname_prefix("modulepar_");
3803 def_value->set_genname_recursive(get_genname());
3804 def_value->set_code_section(GovernedSimple::CS_PRE_INIT);
3805 }
3806 } else checked = true;
3807 #endif
3808 break;
3809 }
3810 }
3811
3812 void Def_Modulepar::generate_code(output_struct *target, bool)
3813 {
3814 type->generate_code(target);
3815 const_def cdef;
3816 Code::init_cdef(&cdef);
3817 const string& t_genname = get_genname();
3818 const char *name = t_genname.c_str();
3819 type->generate_code_object(&cdef, my_scope, t_genname, "modulepar_", false);
3820 if (def_value) {
3821 cdef.init = update_location_object(cdef.init);
3822 cdef.init = def_value->generate_code_init(cdef.init, def_value->get_lhs_name().c_str());
3823 }
3824 Code::merge_cdef(target, &cdef);
3825 Code::free_cdef(&cdef);
3826
3827 if (has_implicit_omit_attr()) {
3828 target->functions.post_init = mputprintf(target->functions.post_init,
3829 "modulepar_%s.set_implicit_omit();\n", name);
3830 }
3831
3832 const char *dispname = id->get_dispname().c_str();
3833 target->functions.set_param = mputprintf(target->functions.set_param,
3834 "if (!strcmp(par_name, \"%s\")) {\n"
3835 "modulepar_%s.set_param(param);\n"
3836 "return TRUE;\n"
3837 "} else ", dispname, name);
3838 target->functions.get_param = mputprintf(target->functions.get_param,
3839 "if (!strcmp(par_name, \"%s\")) {\n"
3840 "return modulepar_%s.get_param(param_name);\n"
3841 "} else ", dispname, name);
3842
3843 if (target->functions.log_param) {
3844 // this is not the first modulepar
3845 target->functions.log_param = mputprintf(target->functions.log_param,
3846 "TTCN_Logger::log_event_str(\", %s := \");\n", dispname);
3847 } else {
3848 // this is the first modulepar
3849 target->functions.log_param = mputprintf(target->functions.log_param,
3850 "TTCN_Logger::log_event_str(\"%s := \");\n", dispname);
3851 }
3852 target->functions.log_param = mputprintf(target->functions.log_param,
3853 "%s.log();\n", name);
3854 }
3855
3856 void Def_Modulepar::generate_code(Common::CodeGenHelper& cgh) {
3857 // module parameter definitions always go to its containing module
3858 generate_code(cgh.get_current_outputstruct());
3859 }
3860
3861 void Def_Modulepar::dump_internal(unsigned level) const
3862 {
3863 DEBUG(level, "Module parameter: %s @ %p", id->get_dispname().c_str(), (const void*)this);
3864 type->dump(level + 1);
3865 if (def_value) def_value->dump(level + 1);
3866 else DEBUG(level + 1, "No default value");
3867 }
3868
3869 // =================================
3870 // ===== Def_Modulepar_Template
3871 // =================================
3872
3873 Def_Modulepar_Template::Def_Modulepar_Template(Identifier *p_id, Type *p_type, Template *p_deftmpl)
3874 : Definition(A_MODULEPAR_TEMP, p_id)
3875 {
3876 if (!p_type) FATAL_ERROR("Ttcn::Def_Modulepar_Template::Def_Modulepar_Template()");
3877 type = p_type;
3878 type->set_ownertype(Type::OT_MODPAR_DEF, this);
3879 def_template = p_deftmpl;
3880 }
3881
3882 Def_Modulepar_Template::~Def_Modulepar_Template()
3883 {
3884 delete type;
3885 delete def_template;
3886 }
3887
3888 Def_Modulepar_Template* Def_Modulepar_Template::clone() const
3889 {
3890 FATAL_ERROR("Def_Modulepar_Template::clone");
3891 }
3892
3893 void Def_Modulepar_Template::set_fullname(const string& p_fullname)
3894 {
3895 Definition::set_fullname(p_fullname);
3896 type->set_fullname(p_fullname + ".<type>");
3897 if (def_template) def_template->set_fullname(p_fullname + ".<default_template>");
3898 }
3899
3900 void Def_Modulepar_Template::set_my_scope(Scope *p_scope)
3901 {
3902 Definition::set_my_scope(p_scope);
3903 type->set_my_scope(p_scope);
3904 if (def_template) def_template->set_my_scope(p_scope);
3905 }
3906
3907 Type *Def_Modulepar_Template::get_Type()
3908 {
3909 chk();
3910 return type;
3911 }
3912
3913 void Def_Modulepar_Template::chk()
3914 {
3915 if(checked) return;
3916 Error_Context cntxt(this, "In template module parameter definition `%s'",
3917 id->get_dispname().c_str());
3918 if (w_attrib_path) {
3919 w_attrib_path->chk_global_attrib();
3920 switch (type->get_type_refd_last()->get_typetype()) {
3921 case Type::T_SEQ_T:
3922 case Type::T_SET_T:
3923 case Type::T_CHOICE_T:
3924 // These types may have qualified attributes
3925 break;
3926 case Type::T_SEQOF: case Type::T_SETOF:
3927 break;
3928 default:
3929 w_attrib_path->chk_no_qualif();
3930 break;
3931 }
3932 }
3933 type->set_genname(_T_, get_genname());
3934 type->chk();
3935 Type *t = type->get_type_refd_last();
3936 switch (t->get_typetype()) {
3937 case Type::T_PORT:
3938 error("Type of template module parameter cannot be port type `%s'",
3939 t->get_fullname().c_str());
3940 break;
3941 case Type::T_SIGNATURE:
3942 error("Type of template module parameter cannot be signature `%s'",
3943 t->get_fullname().c_str());
3944 break;
3945 case Type::T_FUNCTION:
3946 case Type::T_ALTSTEP:
3947 case Type::T_TESTCASE:
3948 if (t->get_fat_runs_on_self()) {
3949 error("Type of template module parameter cannot be of function reference type"
3950 " `%s' which has runs on self clause", t->get_fullname().c_str());
3951 }
3952 break;
3953 default:
3954 if (has_implicit_omit_attr()) {
3955 error("Implicit omit not supported for template module parameters");
3956 }
3957 #if defined(MINGW)
3958 checked = true;
3959 #else
3960 if (def_template) {
3961 Error_Context cntxt2(def_template, "In default template");
3962 def_template->set_my_governor(type);
3963 def_template->flatten(false);
3964 if (def_template->get_templatetype() == Template::CSTR_PATTERN &&
3965 type->get_type_refd_last()->get_typetype() == Type::T_USTR) {
3966 def_template->set_templatetype(Template::USTR_PATTERN);
3967 def_template->get_ustr_pattern()->set_pattern_type(
3968 PatternString::USTR_PATTERN);
3969 }
3970 type->chk_this_template_ref(def_template);
3971 checked = true;
3972 type->chk_this_template_generic(def_template, INCOMPLETE_ALLOWED,
3973 OMIT_ALLOWED, ANY_OR_OMIT_ALLOWED, SUB_CHK, has_implicit_omit_attr() ? IMPLICIT_OMIT : NOT_IMPLICIT_OMIT, 0);
3974 if (!semantic_check_only) {
3975 def_template->set_genname_prefix("modulepar_");
3976 def_template->set_genname_recursive(get_genname());
3977 def_template->set_code_section(GovernedSimple::CS_PRE_INIT);
3978 }
3979 } else checked = true;
3980 #endif
3981 break;
3982 }
3983 }
3984
3985 void Def_Modulepar_Template::generate_code(output_struct *target, bool)
3986 {
3987 type->generate_code(target);
3988 const_def cdef;
3989 Code::init_cdef(&cdef);
3990 const string& t_genname = get_genname();
3991 const char *name = t_genname.c_str();
3992 type->generate_code_object(&cdef, my_scope, t_genname, "modulepar_", true);
3993 if (def_template) {
3994 cdef.init = update_location_object(cdef.init);
3995 cdef.init = def_template->generate_code_init(cdef.init, def_template->get_lhs_name().c_str());
3996 }
3997 Code::merge_cdef(target, &cdef);
3998 Code::free_cdef(&cdef);
3999
4000 if (has_implicit_omit_attr()) {
4001 FATAL_ERROR("Def_Modulepar_Template::generate_code()");
4002 }
4003
4004 const char *dispname = id->get_dispname().c_str();
4005 target->functions.set_param = mputprintf(target->functions.set_param,
4006 "if (!strcmp(par_name, \"%s\")) {\n"
4007 "modulepar_%s.set_param(param);\n"
4008 "return TRUE;\n"
4009 "} else ", dispname, name);
4010 target->functions.get_param = mputprintf(target->functions.get_param,
4011 "if (!strcmp(par_name, \"%s\")) {\n"
4012 "return modulepar_%s.get_param(param_name);\n"
4013 "} else ", dispname, name);
4014
4015 if (target->functions.log_param) {
4016 // this is not the first modulepar
4017 target->functions.log_param = mputprintf(target->functions.log_param,
4018 "TTCN_Logger::log_event_str(\", %s := \");\n", dispname);
4019 } else {
4020 // this is the first modulepar
4021 target->functions.log_param = mputprintf(target->functions.log_param,
4022 "TTCN_Logger::log_event_str(\"%s := \");\n", dispname);
4023 }
4024 target->functions.log_param = mputprintf(target->functions.log_param,
4025 "%s.log();\n", name);
4026 }
4027
4028 void Def_Modulepar_Template::generate_code(Common::CodeGenHelper& cgh) {
4029 // module parameter definitions always go to its containing module
4030 generate_code(cgh.get_current_outputstruct());
4031 }
4032
4033 void Def_Modulepar_Template::dump_internal(unsigned level) const
4034 {
4035 DEBUG(level, "Module parameter: %s @ %p", id->get_dispname().c_str(), (const void*)this);
4036 type->dump(level + 1);
4037 if (def_template) def_template->dump(level + 1);
4038 else DEBUG(level + 1, "No default template");
4039 }
4040
4041 // =================================
4042 // ===== Def_Template
4043 // =================================
4044
4045 Def_Template::Def_Template(template_restriction_t p_template_restriction,
4046 Identifier *p_id, Type *p_type, FormalParList *p_fpl,
4047 Reference *p_derived_ref, Template *p_body)
4048 : Definition(A_TEMPLATE, p_id), type(p_type), fp_list(p_fpl),
4049 derived_ref(p_derived_ref), base_template(0), recurs_deriv_checked(false),
4050 body(p_body), template_restriction(p_template_restriction),
4051 gen_restriction_check(false)
4052 {
4053 if (!p_type || !p_body) FATAL_ERROR("Ttcn::Def_Template::Def_Template()");
4054 type->set_ownertype(Type::OT_TEMPLATE_DEF, this);
4055 if (fp_list) fp_list->set_my_def(this);
4056 }
4057
4058 Def_Template::~Def_Template()
4059 {
4060 delete type;
4061 delete fp_list;
4062 delete derived_ref;
4063 delete body;
4064 }
4065
4066 Def_Template *Def_Template::clone() const
4067 {
4068 FATAL_ERROR("Def_Template::clone");
4069 }
4070
4071 void Def_Template::set_fullname(const string& p_fullname)
4072 {
4073 Definition::set_fullname(p_fullname);
4074 type->set_fullname(p_fullname + ".<type>");
4075 if (fp_list) fp_list->set_fullname(p_fullname + ".<formal_par_list>");
4076 if (derived_ref)
4077 derived_ref->set_fullname(p_fullname + ".<derived_reference>");
4078 body->set_fullname(p_fullname);
4079 }
4080
4081 void Def_Template::set_my_scope(Scope *p_scope)
4082 {
4083 bridgeScope.set_parent_scope(p_scope);
4084 bridgeScope.set_scopeMacro_name(id->get_dispname());
4085
4086 Definition::set_my_scope(&bridgeScope);
4087 type->set_my_scope(&bridgeScope);
4088 if (derived_ref) derived_ref->set_my_scope(&bridgeScope);
4089 if (fp_list) {
4090 fp_list->set_my_scope(&bridgeScope);
4091 body->set_my_scope(fp_list);
4092 } else body->set_my_scope(&bridgeScope);
4093 }
4094
4095 Setting *Def_Template::get_Setting()
4096 {
4097 return get_Template();
4098 }
4099
4100 Type *Def_Template::get_Type()
4101 {
4102 if (!checked) chk();
4103 return type;
4104 }
4105
4106 Template *Def_Template::get_Template()
4107 {
4108 if (!checked) chk();
4109 return body;
4110 }
4111
4112 FormalParList *Def_Template::get_FormalParList()
4113 {
4114 if (!checked) chk();
4115 return fp_list;
4116 }
4117
4118 void Def_Template::chk()
4119 {
4120 if (checked) return;
4121 Error_Context cntxt(this, "In template definition `%s'",
4122 id->get_dispname().c_str());
4123 const string& t_genname = get_genname();
4124 type->set_genname(_T_, t_genname);
4125 type->chk();
4126 if (w_attrib_path) {
4127 w_attrib_path->chk_global_attrib(true);
4128 switch (type->get_type_refd_last()->get_typetype_ttcn3()) {
4129 case Type::T_SEQ_T:
4130 case Type::T_SET_T:
4131 case Type::T_CHOICE_T:
4132 // These types may have qualified attributes
4133 break;
4134 case Type::T_SEQOF: case Type::T_SETOF:
4135 break;
4136 default:
4137 w_attrib_path->chk_no_qualif();
4138 break;
4139 }
4140 }
4141 if (fp_list) {
4142 chk_default();
4143 fp_list->chk(asstype);
4144 if (local_scope) error("Parameterized local template `%s' not supported",
4145 id->get_dispname().c_str());
4146 }
4147
4148 // Merge the elements of "all from" into the list
4149 body->flatten(false);
4150
4151 body->set_my_governor(type);
4152
4153 if (body->get_templatetype() == Template::CSTR_PATTERN &&
4154 type->get_type_refd_last()->get_typetype() == Type::T_USTR) {
4155 body->set_templatetype(Template::USTR_PATTERN);
4156 body->get_ustr_pattern()->set_pattern_type(PatternString::USTR_PATTERN);
4157 }
4158
4159 type->chk_this_template_ref(body);
4160 checked = true;
4161 Type *t = type->get_type_refd_last();
4162 if (t->get_typetype() == Type::T_PORT) {
4163 error("Template cannot be defined for port type `%s'",
4164 t->get_fullname().c_str());
4165 }
4166 chk_modified();
4167 chk_recursive_derivation();
4168 type->chk_this_template_generic(body, INCOMPLETE_ALLOWED, OMIT_ALLOWED,
4169 ANY_OR_OMIT_ALLOWED, SUB_CHK,
4170 has_implicit_omit_attr() ? IMPLICIT_OMIT : NOT_IMPLICIT_OMIT, 0);
4171
4172 chk_erroneous_attr();
4173 if (erroneous_attrs) body->set_err_descr(erroneous_attrs->get_err_descr());
4174
4175 {
4176 ReferenceChain refch(type, "While checking embedded recursions");
4177 body->chk_recursions(refch);
4178 }
4179 if (template_restriction!=TR_NONE) {
4180 Error_Context ec(this, "While checking template restriction `%s'",
4181 Template::get_restriction_name(template_restriction));
4182 gen_restriction_check =
4183 body->chk_restriction("template definition", template_restriction, body);
4184 if (fp_list && template_restriction!=TR_PRESENT) {
4185 size_t nof_fps = fp_list->get_nof_fps();
4186 for (size_t i=0; i<nof_fps; i++) {
4187 FormalPar* fp = fp_list->get_fp_byIndex(i);
4188 // if formal par is not template then skip restriction checking,
4189 // templates can have only `in' parameters
4190 if (fp->get_asstype()!=A_PAR_TEMPL_IN) continue;
4191 template_restriction_t fp_tr = fp->get_template_restriction();
4192 switch (template_restriction) {
4193 case TR_VALUE:
4194 case TR_OMIT:
4195 switch (fp_tr) {
4196 case TR_VALUE:
4197 case TR_OMIT:
4198 // allowed
4199 break;
4200 case TR_PRESENT:
4201 fp->error("Formal parameter with template restriction `%s' "
4202 "not allowed here", Template::get_restriction_name(fp_tr));
4203 break;
4204 case TR_NONE:
4205 fp->error("Formal parameter without template restriction "
4206 "not allowed here");
4207 break;
4208 default:
4209 FATAL_ERROR("Ttcn::Def_Template::chk()");
4210 }
4211 break;
4212 default:
4213 FATAL_ERROR("Ttcn::Def_Template::chk()");
4214 }
4215 }
4216 }
4217 }
4218 if (!semantic_check_only) {
4219 if (fp_list) fp_list->set_genname(t_genname);
4220 body->set_genname_prefix("template_");
4221 body->set_genname_recursive(t_genname);
4222 body->set_code_section(fp_list ? GovernedSimple::CS_INLINE :
4223 GovernedSimple::CS_POST_INIT);
4224 }
4225
4226 }
4227
4228 void Def_Template::chk_default() const
4229 {
4230 if (!fp_list) FATAL_ERROR("Def_Template::chk_default()");
4231 if (!derived_ref) {
4232 if (fp_list->has_notused_defval())
4233 fp_list->error("Only modified templates are allowed to use the not "
4234 "used symbol (`-') as the default parameter");
4235 return;
4236 }
4237 Common::Assignment *ass = derived_ref->get_refd_assignment(false);
4238 if (!ass || ass->get_asstype() != A_TEMPLATE) return; // Work locally.
4239 Def_Template *base = dynamic_cast<Def_Template *>(ass);
4240 if (!base) FATAL_ERROR("Def_Template::chk_default()");
4241 FormalParList *base_fpl = base->get_FormalParList();
4242 size_t nof_base_fps = base_fpl ? base_fpl->get_nof_fps() : 0;
4243 size_t nof_local_fps = fp_list ? fp_list->get_nof_fps() : 0;
4244 size_t min_fps = nof_base_fps;
4245 if (nof_local_fps < nof_base_fps) min_fps = nof_local_fps;
4246 for (size_t i = 0; i < min_fps; i++) {
4247 FormalPar *base_fp = base_fpl->get_fp_byIndex(i);
4248 FormalPar *local_fp = fp_list->get_fp_byIndex(i);
4249 if (local_fp->has_notused_defval()) {
4250 if (base_fp->has_defval()) {
4251 local_fp->set_defval(base_fp->get_defval());
4252 } else {
4253 local_fp->error("Not used symbol (`-') doesn't have the "
4254 "corresponding default parameter in the "
4255 "base template");
4256 }
4257 }
4258 }
4259 // Additional parameters in the derived template with using the not used
4260 // symbol. TODO: Merge the loops.
4261 for (size_t i = nof_base_fps; i < nof_local_fps; i++) {
4262 FormalPar *local_fp = fp_list->get_fp_byIndex(i);
4263 if (local_fp->has_notused_defval())
4264 local_fp->error("Not used symbol (`-') doesn't have the "
4265 "corresponding default parameter in the "
4266 "base template");
4267 }
4268 }
4269
4270 void Def_Template::chk_modified()
4271 {
4272 if (!derived_ref) return;
4273 // Do not check the (non-existent) actual parameter list of the derived
4274 // reference against the formal parameter list of the base template.
4275 // According to TTCN-3 syntax the derived reference cannot have parameters
4276 // even if the base template is parameterized.
4277 Common::Assignment *ass = derived_ref->get_refd_assignment(false);
4278 // Checking the existence and type compatibility of the base template.
4279 if (!ass) return;
4280 if (ass->get_asstype() != A_TEMPLATE) {
4281 derived_ref->error("Reference to a template was expected in the "
4282 "`modifies' definition instead of %s",
4283 ass->get_description().c_str());
4284 return;
4285 }
4286 base_template = dynamic_cast<Def_Template*>(ass);
4287 if (!base_template) FATAL_ERROR("Def_Template::chk_modified()");
4288 Type *base_type = base_template->get_Type();
4289 TypeCompatInfo info_base(my_scope->get_scope_mod(), type, base_type, true,
4290 false, true);
4291 TypeChain l_chain_base;
4292 TypeChain r_chain_base;
4293 if (!type->is_compatible(base_type, &info_base, &l_chain_base,
4294 &r_chain_base)) {
4295 if (info_base.is_subtype_error()) {
4296 type->error("%s", info_base.get_subtype_error().c_str());
4297 } else
4298 if (!info_base.is_erroneous()) {
4299 type->error("The modified template has different type than base "
4300 "template `%s': `%s' was expected instead of `%s'",
4301 ass->get_fullname().c_str(),
4302 base_type->get_typename().c_str(),
4303 type->get_typename().c_str());
4304 } else {
4305 // Always use the format string.
4306 type->error("%s", info_base.get_error_str_str().c_str());
4307 }
4308 } else {
4309 if (info_base.needs_conversion())
4310 body->set_needs_conversion();
4311 }
4312 // Check for restriction.
4313 if (Template::is_less_restrictive(base_template->get_template_restriction(),
4314 template_restriction)) {
4315 error("The template restriction is not the same or more "
4316 "restrictive as of base template `%s'", ass->get_fullname().c_str());
4317 }
4318 // Checking formal parameter lists.
4319 FormalParList *base_fpl = base_template->get_FormalParList();
4320 size_t nof_base_fps = base_fpl ? base_fpl->get_nof_fps() : 0;
4321 size_t nof_local_fps = fp_list ? fp_list->get_nof_fps() : 0;
4322 size_t min_fps;
4323 if (nof_local_fps < nof_base_fps) {
4324 error("The modified template has fewer formal parameters than base "
4325 "template `%s': at least %lu parameter%s expected instead of %lu",
4326 ass->get_fullname().c_str(), (unsigned long)nof_base_fps,
4327 nof_base_fps > 1 ? "s were" : " was", (unsigned long)nof_local_fps);
4328 min_fps = nof_local_fps;
4329 } else min_fps = nof_base_fps;
4330
4331 for (size_t i = 0; i < min_fps; i++) {
4332 FormalPar *base_fp = base_fpl->get_fp_byIndex(i);
4333 FormalPar *local_fp = fp_list->get_fp_byIndex(i);
4334 Error_Context cntxt(local_fp, "In formal parameter #%lu",
4335 (unsigned long)(i + 1));
4336 // Check for parameter kind equivalence (value or template).
4337 if (base_fp->get_asstype() != local_fp->get_asstype())
4338 local_fp->error("The kind of parameter is not the same as in base "
4339 "template `%s': %s was expected instead of %s",
4340 ass->get_fullname().c_str(), base_fp->get_assname(),
4341 local_fp->get_assname());
4342 // Check for type compatibility.
4343 Type *base_fp_type = base_fp->get_Type();
4344 Type *local_fp_type = local_fp->get_Type();
4345 TypeCompatInfo info_par(my_scope->get_scope_mod(), base_fp_type,
4346 local_fp_type, true, false);
4347 TypeChain l_chain_par;
4348 TypeChain r_chain_par;
4349 if (!base_fp_type->is_compatible(local_fp_type, &info_par, &l_chain_par,
4350 &r_chain_par)) {
4351 if (info_par.is_subtype_error()) {
4352 local_fp_type->error("%s", info_par.get_subtype_error().c_str());
4353 } else
4354 if (!info_par.is_erroneous()) {
4355 local_fp_type->error("The type of parameter is not the same as in "
4356 "base template `%s': `%s' was expected instead "
4357 "of `%s'",
4358 ass->get_fullname().c_str(),
4359 base_fp_type->get_typename().c_str(),
4360 local_fp_type->get_typename().c_str());
4361 } else {
4362 local_fp_type->error("%s", info_par.get_error_str_str().c_str());
4363 }
4364 } else {
4365 if (info_par.needs_conversion())
4366 body->set_needs_conversion();
4367 }
4368 // Check for name equivalence.
4369 const Identifier& base_fp_id = base_fp->get_id();
4370 const Identifier& local_fp_id = local_fp->get_id();
4371 if (!(base_fp_id == local_fp_id))
4372 local_fp->error("The name of parameter is not the same as in base "
4373 "template `%s': `%s' was expected instead of `%s'",
4374 ass->get_fullname().c_str(),
4375 base_fp_id.get_dispname().c_str(),
4376 local_fp_id.get_dispname().c_str());
4377 // Check for restrictions: the derived must be same or more restrictive.
4378 if (base_fp->get_asstype()==local_fp->get_asstype() &&
4379 Template::is_less_restrictive(base_fp->get_template_restriction(),
4380 local_fp->get_template_restriction())) {
4381 local_fp->error("The restriction of parameter is not the same or more "
4382 "restrictive as in base template `%s'", ass->get_fullname().c_str());
4383 }
4384 }
4385 // Set the pointer to the body of base template.
4386 body->set_base_template(base_template->get_Template());
4387 }
4388
4389 void Def_Template::chk_recursive_derivation()
4390 {
4391 if (recurs_deriv_checked) return;
4392 if (base_template) {
4393 ReferenceChain refch(this, "While checking the chain of base templates");
4394 refch.add(get_fullname());
4395 for (Def_Template *iter = base_template; iter; iter = iter->base_template)
4396 {
4397 if (iter->recurs_deriv_checked) break;
4398 else if (refch.add(iter->get_fullname()))
4399 iter->recurs_deriv_checked = true;
4400 else break;
4401 }
4402 }
4403 recurs_deriv_checked = true;
4404 }
4405
4406 void Def_Template::generate_code(output_struct *target, bool)
4407 {
4408 type->generate_code(target);
4409 if (fp_list) {
4410 // Parameterized template. Generate code for a function which returns
4411 // a $(genname)_template and has the appropriate parameters.
4412 const string& t_genname = get_genname();
4413 const char *template_name = t_genname.c_str();
4414 const char *template_dispname = id->get_dispname().c_str();
4415 const string& type_genname = type->get_genname_template(my_scope);
4416 const char *type_genname_str = type_genname.c_str();
4417
4418 // assemble the function body first (this also determines which parameters
4419 // are never used)
4420 size_t nof_base_pars = 0;
4421 char* function_body = create_location_object(memptystr(), "TEMPLATE",
4422 template_dispname);
4423 if (debugger_active) {
4424 function_body = generate_code_debugger_function_init(function_body, this);
4425 }
4426 if (base_template) {
4427 // modified template
4428 function_body = mputprintf(function_body, "%s ret_val(%s",
4429 type_genname_str,
4430 base_template->get_genname_from_scope(my_scope).c_str());
4431 if (base_template->fp_list) {
4432 // the base template is also parameterized
4433 function_body = mputc(function_body, '(');
4434 nof_base_pars = base_template->fp_list->get_nof_fps();
4435 for (size_t i = 0; i < nof_base_pars; i++) {
4436 if (i > 0) function_body = mputstr(function_body, ", ");
4437 function_body = mputstr(function_body,
4438 fp_list->get_fp_byIndex(i)->get_id().get_name().c_str());
4439 }
4440 function_body = mputc(function_body, ')');
4441 }
4442 function_body = mputstr(function_body, ");\n");
4443 } else {
4444 // simple template
4445 function_body = mputprintf(function_body, "%s ret_val;\n",
4446 type_genname_str);
4447 }
4448 if (erroneous_attrs && erroneous_attrs->get_err_descr()) {
4449 function_body = erroneous_attrs->get_err_descr()->
4450 generate_code_str(function_body, string("ret_val"));
4451 }
4452 function_body = body->generate_code_init(function_body, "ret_val");
4453 if (template_restriction!=TR_NONE && gen_restriction_check)
4454 function_body = Template::generate_restriction_check_code(function_body,
4455 "ret_val", template_restriction);
4456 if (debugger_active) {
4457 function_body = mputstr(function_body,
4458 "ttcn3_debugger.set_return_value((TTCN_Logger::begin_event_log2str(), "
4459 "ret_val.log(), TTCN_Logger::end_event_log2str()));\n");
4460 }
4461 function_body = mputstr(function_body, "return ret_val;\n");
4462 // if the template modifies a parameterized template, then the inherited
4463 // formal parameters must always be displayed, otherwise generate a smart
4464 // formal parameter list (where the names of unused parameters are omitted)
4465 char *formal_par_list = fp_list->generate_code(memptystr(), nof_base_pars);
4466 fp_list->generate_code_defval(target);
4467
4468 target->header.function_prototypes =
4469 mputprintf(target->header.function_prototypes,
4470 "extern %s %s(%s);\n",
4471 type_genname_str, template_name, formal_par_list);
4472 target->source.function_bodies = mputprintf(target->source.function_bodies,
4473 "%s %s(%s)\n"
4474 "{\n"
4475 "%s"
4476 "}\n\n", type_genname_str, template_name, formal_par_list, function_body);
4477 Free(formal_par_list);
4478 Free(function_body);
4479 } else {
4480 // non-parameterized template
4481 const_def cdef;
4482 Code::init_cdef(&cdef);
4483 type->generate_code_object(&cdef, body);
4484 cdef.init = update_location_object(cdef.init);
4485 if (base_template) {
4486 // modified template
4487 if (base_template->my_scope->get_scope_mod_gen() ==
4488 my_scope->get_scope_mod_gen()) {
4489 // if the base template is in the same module its body has to be
4490 // initialized first
4491 cdef.init = base_template->body->generate_code_init(cdef.init,
4492 base_template->body->get_lhs_name().c_str());
4493 }
4494 if (use_runtime_2 && body->get_needs_conversion()) {
4495 Type *body_type = body->get_my_governor()->get_type_refd_last();
4496 Type *base_type = base_template->body->get_my_governor()
4497 ->get_type_refd_last();
4498 if (!body_type || !base_type)
4499 FATAL_ERROR("Def_Template::generate_code()");
4500 const string& tmp_id = body->get_temporary_id();
4501 const char *tmp_id_str = tmp_id.c_str();
4502 // base template initialization
4503 cdef.init = mputprintf(cdef.init,
4504 "%s %s;\n"
4505 "if (!%s(%s, %s)) TTCN_error(\"Values or templates of types `%s' "
4506 "and `%s' are not compatible at run-time\");\n"
4507 "%s = %s;\n",
4508 body_type->get_genname_template(my_scope).c_str(), tmp_id_str,
4509 TypeConv::get_conv_func(base_type, body_type, my_scope
4510 ->get_scope_mod()).c_str(), tmp_id_str, base_template
4511 ->get_genname_from_scope(my_scope).c_str(), base_type
4512 ->get_typename().c_str(), body_type->get_typename().c_str(),
4513 body->get_lhs_name().c_str(), tmp_id_str);
4514 } else {
4515 cdef.init = mputprintf(cdef.init, "%s = %s;\n",
4516 body->get_lhs_name().c_str(),
4517 base_template->get_genname_from_scope(my_scope).c_str());
4518 }
4519 }
4520 if (use_runtime_2 && TypeConv::needs_conv_refd(body))
4521 cdef.init = TypeConv::gen_conv_code_refd(cdef.init,
4522 body->get_lhs_name().c_str(), body);
4523 else
4524 cdef.init = body->generate_code_init(cdef.init,
4525 body->get_lhs_name().c_str());
4526 if (template_restriction != TR_NONE && gen_restriction_check)
4527 cdef.init = Template::generate_restriction_check_code(cdef.init,
4528 body->get_lhs_name().c_str(), template_restriction);
4529 target->header.global_vars = mputstr(target->header.global_vars,
4530 cdef.decl);
4531 target->source.global_vars = mputstr(target->source.global_vars,
4532 cdef.def);
4533 target->functions.post_init = mputstr(target->functions.post_init,
4534 cdef.init);
4535 Code::free_cdef(&cdef);
4536 }
4537 }
4538
4539 void Def_Template::generate_code(Common::CodeGenHelper& cgh) {
4540 generate_code(cgh.get_outputstruct(this));
4541 }
4542
4543 char *Def_Template::generate_code_str(char *str)
4544 {
4545 const string& t_genname = get_genname();
4546 const char *genname_str = t_genname.c_str();
4547 const string& type_genname = type->get_genname_template(my_scope);
4548 const char *type_genname_str = type_genname.c_str();
4549 if (fp_list) {
4550 const char *dispname_str = id->get_dispname().c_str();
4551 NOTSUPP("Code generation for parameterized local template `%s'",
4552 dispname_str);
4553 str = mputprintf(str, "/* NOT SUPPORTED: template %s */\n",
4554 dispname_str);
4555 } else {
4556 if (base_template) {
4557 // non-parameterized modified template
4558 if (use_runtime_2 && body->get_needs_conversion()) {
4559 Type *body_type = body->get_my_governor()->get_type_refd_last();
4560 Type *base_type = base_template->body->get_my_governor()
4561 ->get_type_refd_last();
4562 if (!body_type || !base_type)
4563 FATAL_ERROR("Def_Template::generate_code_str()");
4564 const string& tmp_id = body->get_temporary_id();
4565 const char *tmp_id_str = tmp_id.c_str();
4566 str = mputprintf(str,
4567 "%s %s;\n"
4568 "if (!%s(%s, %s)) TTCN_error(\"Values or templates of types `%s' "
4569 "and `%s' are not compatible at run-time\");\n"
4570 "%s %s(%s);\n",
4571 body_type->get_genname_template(my_scope).c_str(), tmp_id_str,
4572 TypeConv::get_conv_func(base_type, body_type, my_scope
4573 ->get_scope_mod()).c_str(), tmp_id_str, base_template
4574 ->get_genname_from_scope(my_scope).c_str(), base_type
4575 ->get_typename().c_str(), body_type->get_typename().c_str(),
4576 type_genname_str, genname_str, tmp_id_str);
4577 } else {
4578 // the object is initialized from the base template by the
4579 // constructor
4580 str = mputprintf(str, "%s %s(%s);\n", type_genname_str, genname_str,
4581 base_template->get_genname_from_scope(my_scope).c_str());
4582 }
4583 // the modified body is assigned in the subsequent statements
4584 str = body->generate_code_init(str, genname_str);
4585 } else {
4586 // non-parameterized non-modified template
4587 if (body->has_single_expr()) {
4588 // the object is initialized by the constructor
4589 str = mputprintf(str, "%s %s(%s);\n", type_genname_str,
4590 genname_str, body->get_single_expr(false).c_str());
4591 // make sure the template's code is not generated twice (TR: HU56425)
4592 body->set_code_generated();
4593 } else {
4594 // the default constructor is used
4595 str = mputprintf(str, "%s %s;\n", type_genname_str, genname_str);
4596 // the body is assigned in the subsequent statements
4597 str = body->generate_code_init(str, genname_str);
4598 }
4599 }
4600 if (template_restriction != TR_NONE && gen_restriction_check)
4601 str = Template::generate_restriction_check_code(str, genname_str,
4602 template_restriction);
4603 }
4604 if (debugger_active) {
4605 str = generate_code_debugger_add_var(str, this);
4606 }
4607 return str;
4608 }
4609
4610 void Def_Template::ilt_generate_code(ILT *ilt)
4611 {
4612 const string& t_genname = get_genname();
4613 const char *genname_str = t_genname.c_str();
4614 char*& def=ilt->get_out_def();
4615 char*& init=ilt->get_out_branches();
4616 if (fp_list) {
4617 const char *dispname_str = id->get_dispname().c_str();
4618 NOTSUPP("Code generation for parameterized local template `%s'",
4619 dispname_str);
4620 def = mputprintf(def, "/* NOT SUPPORTED: template %s */\n", dispname_str);
4621 init = mputprintf(init, "/* NOT SUPPORTED: template %s */\n",
4622 dispname_str);
4623 } else {
4624 // non-parameterized template
4625 // use the default constructor for initialization
4626 def = mputprintf(def, "%s %s;\n",
4627 type->get_genname_template(my_scope).c_str(), genname_str);
4628 if (base_template) {
4629 // copy the base template with an assignment
4630 init = mputprintf(init, "%s = %s;\n", genname_str,
4631 base_template->get_genname_from_scope(my_scope).c_str());
4632 }
4633 // finally assign the body
4634 init = body->generate_code_init(init, genname_str);
4635 if (template_restriction!=TR_NONE && gen_restriction_check)
4636 init = Template::generate_restriction_check_code(init, genname_str,
4637 template_restriction);
4638 }
4639 }
4640
4641 void Def_Template::dump_internal(unsigned level) const
4642 {
4643 DEBUG(level, "Template: %s", id->get_dispname().c_str());
4644 if (fp_list) fp_list->dump(level + 1);
4645 if (derived_ref)
4646 DEBUG(level + 1, "modifies: %s", derived_ref->get_dispname().c_str());
4647 if (template_restriction!=TR_NONE)
4648 DEBUG(level + 1, "restriction: %s",
4649 Template::get_restriction_name(template_restriction));
4650 type->dump(level + 1);
4651 body->dump(level + 1);
4652 }
4653
4654 // =================================
4655 // ===== Def_Var
4656 // =================================
4657
4658 Def_Var::Def_Var(Identifier *p_id, Type *p_type, Value *p_initial_value)
4659 : Definition(A_VAR, p_id), type(p_type), initial_value(p_initial_value)
4660 {
4661 if (!p_type) FATAL_ERROR("Ttcn::Def_Var::Def_Var()");
4662 type->set_ownertype(Type::OT_VAR_DEF, this);
4663 }
4664
4665 Def_Var::~Def_Var()
4666 {
4667 delete type;
4668 delete initial_value;
4669 }
4670
4671 Def_Var *Def_Var::clone() const
4672 {
4673 FATAL_ERROR("Def_Var::clone");
4674 }
4675
4676 void Def_Var::set_fullname(const string& p_fullname)
4677 {
4678 Definition::set_fullname(p_fullname);
4679 type->set_fullname(p_fullname + ".<type>");
4680 if (initial_value)
4681 initial_value->set_fullname(p_fullname + ".<initial_value>");
4682 }
4683
4684 void Def_Var::set_my_scope(Scope *p_scope)
4685 {
4686 Definition::set_my_scope(p_scope);
4687 type->set_my_scope(p_scope);
4688 if (initial_value) initial_value->set_my_scope(p_scope);
4689 }
4690
4691 Type *Def_Var::get_Type()
4692 {
4693 chk();
4694 return type;
4695 }
4696
4697 void Def_Var::chk()
4698 {
4699 if(checked) return;
4700 Error_Context cntxt(this, "In variable definition `%s'",
4701 id->get_dispname().c_str());
4702 type->set_genname(_T_, get_genname());
4703 type->chk();
4704 checked = true;
4705 Type *t = type->get_type_refd_last();
4706 switch (t->get_typetype()) {
4707 case Type::T_PORT:
4708 error("Variable cannot be defined for port type `%s'",
4709 t->get_fullname().c_str());
4710 break;
4711 case Type::T_SIGNATURE:
4712 error("Variable cannot be defined for signature `%s'",
4713 t->get_fullname().c_str());
4714 break;
4715 default:
4716 if (initial_value) {
4717 initial_value->set_my_governor(type);
4718 type->chk_this_value_ref(initial_value);
4719 type->chk_this_value(initial_value, this, is_local() ?
4720 Type::EXPECTED_DYNAMIC_VALUE : Type::EXPECTED_STATIC_VALUE,
4721 INCOMPLETE_ALLOWED, OMIT_NOT_ALLOWED, SUB_CHK);
4722 if (!semantic_check_only) {
4723 initial_value->set_genname_recursive(get_genname());
4724 initial_value->set_code_section(GovernedSimple::CS_INLINE);
4725 }
4726 }
4727 break;
4728 }
4729
4730 if (w_attrib_path) {
4731 w_attrib_path->chk_global_attrib();
4732 w_attrib_path->chk_no_qualif();
4733 }
4734 }
4735
4736 bool Def_Var::chk_identical(Definition *p_def)
4737 {
4738 chk();
4739 p_def->chk();
4740 if (p_def->get_asstype() != A_VAR) {
4741 const char *dispname_str = id->get_dispname().c_str();
4742 error("Local definition `%s' is a variable, but the definition "
4743 "inherited from component type `%s' is a %s", dispname_str,
4744 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
4745 p_def->note("The inherited definition of `%s' is here", dispname_str);
4746 return false;
4747 }
4748 Def_Var *p_def_var = dynamic_cast<Def_Var*>(p_def);
4749 if (!p_def_var) FATAL_ERROR("Def_Var::chk_identical()");
4750 if (!type->is_identical(p_def_var->type)) {
4751 const char *dispname_str = id->get_dispname().c_str();
4752 type->error("Local variable `%s' has type `%s', but the variable "
4753 "inherited from component type `%s' has type `%s'", dispname_str,
4754 type->get_typename().c_str(),
4755 p_def_var->get_my_scope()->get_fullname().c_str(),
4756 p_def_var->type->get_typename().c_str());
4757 p_def_var->note("The inherited variable `%s' is here", dispname_str);
4758 return false;
4759 }
4760 if (initial_value) {
4761 if (p_def_var->initial_value) {
4762 if (!initial_value->is_unfoldable() &&
4763 !p_def_var->initial_value->is_unfoldable() &&
4764 !(*initial_value == *p_def_var->initial_value)) {
4765 const char *dispname_str = id->get_dispname().c_str();
4766 initial_value->warning("Local variable `%s' and the variable "
4767 "inherited from component type `%s' have different initial values",
4768 dispname_str, p_def_var->get_my_scope()->get_fullname().c_str());
4769 p_def_var->note("The inherited variable `%s' is here", dispname_str);
4770 }
4771 } else {
4772 const char *dispname_str = id->get_dispname().c_str();
4773 initial_value->warning("Local variable `%s' has initial value, but "
4774 "the variable inherited from component type `%s' does not",
4775 dispname_str, p_def_var->get_my_scope()->get_fullname().c_str());
4776 p_def_var->note("The inherited variable `%s' is here", dispname_str);
4777 }
4778 } else if (p_def_var->initial_value) {
4779 const char *dispname_str = id->get_dispname().c_str();
4780 warning("Local variable `%s' does not have initial value, but the "
4781 "variable inherited from component type `%s' has", dispname_str,
4782 p_def_var->get_my_scope()->get_fullname().c_str());
4783 p_def_var->note("The inherited variable `%s' is here", dispname_str);
4784 }
4785 return true;
4786 }
4787
4788 void Def_Var::generate_code(output_struct *target, bool clean_up)
4789 {
4790 type->generate_code(target);
4791 const_def cdef;
4792 Code::init_cdef(&cdef);
4793 type->generate_code_object(&cdef, my_scope, get_genname(), 0, false);
4794 Code::merge_cdef(target, &cdef);
4795 Code::free_cdef(&cdef);
4796 if (initial_value) {
4797 target->functions.init_comp =
4798 initial_value->generate_code_init(target->functions.init_comp,
4799 initial_value->get_lhs_name().c_str());
4800 } else if (clean_up) { // No initial value.
4801 target->functions.init_comp = mputprintf(target->functions.init_comp,
4802 "%s.clean_up();\n", get_genname().c_str());
4803 }
4804 }
4805
4806 void Def_Var::generate_code(CodeGenHelper& cgh)
4807 {
4808 generate_code(cgh.get_outputstruct(this));
4809 }
4810
4811 char *Def_Var::generate_code_str(char *str)
4812 {
4813 const string& t_genname = get_genname();
4814 const char *genname_str = t_genname.c_str();
4815 if (initial_value && initial_value->has_single_expr()) {
4816 // the initial value can be represented by a single C++ expression
4817 // the object is initialized by the constructor
4818 str = mputprintf(str, "%s %s(%s);\n",
4819 type->get_genname_value(my_scope).c_str(), genname_str,
4820 initial_value->get_single_expr().c_str());
4821 } else {
4822 // use the default constructor
4823 str = mputprintf(str, "%s %s;\n",
4824 type->get_genname_value(my_scope).c_str(), genname_str);
4825 if (initial_value) {
4826 // the initial value is assigned using subsequent statements
4827 str = initial_value->generate_code_init(str, genname_str);
4828 }
4829 }
4830 if (debugger_active) {
4831 str = generate_code_debugger_add_var(str, this);
4832 }
4833 return str;
4834 }
4835
4836 void Def_Var::ilt_generate_code(ILT *ilt)
4837 {
4838 const string& t_genname = get_genname();
4839 const char *genname_str = t_genname.c_str();
4840 char*& def=ilt->get_out_def();
4841 char*& init=ilt->get_out_branches();
4842 def = mputprintf(def, "%s %s;\n", type->get_genname_value(my_scope).c_str(),
4843 genname_str);
4844 if (initial_value)
4845 init = initial_value->generate_code_init(init, genname_str);
4846 }
4847
4848 char *Def_Var::generate_code_init_comp(char *str, Definition *base_defn)
4849 {
4850 if (initial_value) {
4851 str = initial_value->generate_code_init(str,
4852 base_defn->get_genname_from_scope(my_scope).c_str());
4853 }
4854 return str;
4855 }
4856
4857 void Def_Var::dump_internal(unsigned level) const
4858 {
4859 DEBUG(level, "Variable %s", id->get_dispname().c_str());
4860 type->dump(level + 1);
4861 if (initial_value) initial_value->dump(level + 1);
4862 }
4863
4864 // =================================
4865 // ===== Def_Var_Template
4866 // =================================
4867
4868 Def_Var_Template::Def_Var_Template(Identifier *p_id, Type *p_type,
4869 Template *p_initial_value, template_restriction_t p_template_restriction)
4870 : Definition(A_VAR_TEMPLATE, p_id), type(p_type),
4871 initial_value(p_initial_value), template_restriction(p_template_restriction)
4872 {
4873 if (!p_type) FATAL_ERROR("Ttcn::Def_Var_Template::Def_Var_Template()");
4874 type->set_ownertype(Type::OT_VARTMPL_DEF, this);
4875 }
4876
4877 Def_Var_Template::~Def_Var_Template()
4878 {
4879 delete type;
4880 delete initial_value;
4881 }
4882
4883 Def_Var_Template *Def_Var_Template::clone() const
4884 {
4885 FATAL_ERROR("Def_Var_Template::clone");
4886 }
4887
4888 void Def_Var_Template::set_fullname(const string& p_fullname)
4889 {
4890 Definition::set_fullname(p_fullname);
4891 type->set_fullname(p_fullname + ".<type>");
4892 if (initial_value)
4893 initial_value->set_fullname(p_fullname + ".<initial_value>");
4894 }
4895
4896 void Def_Var_Template::set_my_scope(Scope *p_scope)
4897 {
4898 Definition::set_my_scope(p_scope);
4899 type->set_my_scope(p_scope);
4900 if (initial_value) initial_value->set_my_scope(p_scope);
4901 }
4902
4903 Type *Def_Var_Template::get_Type()
4904 {
4905 chk();
4906 return type;
4907 }
4908
4909 void Def_Var_Template::chk()
4910 {
4911 if(checked) return;
4912 Error_Context cntxt(this, "In template variable definition `%s'",
4913 id->get_dispname().c_str());
4914 type->set_genname(_T_, get_genname());
4915 type->chk();
4916 checked = true;
4917 Type *t = type->get_type_refd_last();
4918 if (t->get_typetype() == Type::T_PORT) {
4919 error("Template variable cannot be defined for port type `%s'",
4920 t->get_fullname().c_str());
4921 }
4922
4923 if (initial_value) {
4924 initial_value->set_my_governor(type);
4925 initial_value->flatten(false);
4926
4927 if (initial_value->get_templatetype() == Template::CSTR_PATTERN &&
4928 type->get_type_refd_last()->get_typetype() == Type::T_USTR) {
4929 initial_value->set_templatetype(Template::USTR_PATTERN);
4930 initial_value->get_ustr_pattern()->set_pattern_type(
4931 PatternString::USTR_PATTERN);
4932 }
4933
4934 type->chk_this_template_ref(initial_value);
4935 // temporary hack: to allow incomplete body as initial value
4936 // checking as a modified template, but without a base template
4937 type->chk_this_template_generic(initial_value, INCOMPLETE_ALLOWED,
4938 OMIT_ALLOWED, ANY_OR_OMIT_ALLOWED, SUB_CHK, IMPLICIT_OMIT, 0);
4939 gen_restriction_check =
4940 initial_value->chk_restriction("template variable definition",
4941 template_restriction, initial_value);
4942 if (!semantic_check_only) {
4943 initial_value->set_genname_recursive(get_genname());
4944 initial_value->set_code_section(GovernedSimple::CS_INLINE);
4945 }
4946 }
4947 if (w_attrib_path) {
4948 w_attrib_path->chk_global_attrib();
4949 w_attrib_path->chk_no_qualif();
4950 }
4951 }
4952
4953 bool Def_Var_Template::chk_identical(Definition *p_def)
4954 {
4955 chk();
4956 p_def->chk();
4957 if (p_def->get_asstype() != A_VAR_TEMPLATE) {
4958 const char *dispname_str = id->get_dispname().c_str();
4959 error("Local definition `%s' is a template variable, but the definition "
4960 "inherited from component type `%s' is a %s", dispname_str,
4961 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
4962 p_def->note("The inherited definition of `%s' is here", dispname_str);
4963 return false;
4964 }
4965 Def_Var_Template *p_def_var_template =
4966 dynamic_cast<Def_Var_Template*>(p_def);
4967 if (!p_def_var_template) FATAL_ERROR("Def_Var_Template::chk_identical()");
4968 if (!type->is_identical(p_def_var_template->type)) {
4969 const char *dispname_str = id->get_dispname().c_str();
4970 type->error("Local template variable `%s' has type `%s', but the "
4971 "template variable inherited from component type `%s' has type `%s'",
4972 dispname_str, type->get_typename().c_str(),
4973 p_def_var_template->get_my_scope()->get_fullname().c_str(),
4974 p_def_var_template->type->get_typename().c_str());
4975 p_def_var_template->note("The inherited template variable `%s' is here",
4976 dispname_str);
4977 return false;
4978 }
4979 if (initial_value) {
4980 if (!p_def_var_template->initial_value) {
4981 const char *dispname_str = id->get_dispname().c_str();
4982 initial_value->warning("Local template variable `%s' has initial "
4983 "value, but the template variable inherited from component type "
4984 "`%s' does not", dispname_str,
4985 p_def_var_template->get_my_scope()->get_fullname().c_str());
4986 p_def_var_template->note("The inherited template variable `%s' is here",
4987 dispname_str);
4988 }
4989 } else if (p_def_var_template->initial_value) {
4990 const char *dispname_str = id->get_dispname().c_str();
4991 warning("Local template variable `%s' does not have initial value, but "
4992 "the template variable inherited from component type `%s' has",
4993 dispname_str,
4994 p_def_var_template->get_my_scope()->get_fullname().c_str());
4995 p_def_var_template->note("The inherited template variable `%s' is here",
4996 dispname_str);
4997 }
4998 return true;
4999 }
5000
5001 void Def_Var_Template::generate_code(output_struct *target, bool clean_up)
5002 {
5003 type->generate_code(target);
5004 const_def cdef;
5005 Code::init_cdef(&cdef);
5006 type->generate_code_object(&cdef, my_scope, get_genname(), 0, true);
5007 Code::merge_cdef(target, &cdef);
5008 Code::free_cdef(&cdef);
5009 if (initial_value) {
5010 if (Common::Type::T_SEQOF == initial_value->get_my_governor()->get_typetype() ||
5011 Common::Type::T_ARRAY == initial_value->get_my_governor()->get_typetype()) {
5012 target->functions.init_comp = mputprintf(target->functions.init_comp,
5013 "%s.remove_all_permutations();\n", initial_value->get_lhs_name().c_str());
5014 }
5015 target->functions.init_comp =
5016 initial_value->generate_code_init(target->functions.init_comp,
5017 initial_value->get_lhs_name().c_str());
5018 if (template_restriction!=TR_NONE && gen_restriction_check)
5019 target->functions.init_comp = Template::generate_restriction_check_code(
5020 target->functions.init_comp, initial_value->get_lhs_name().c_str(),
5021 template_restriction);
5022 } else if (clean_up) { // No initial value.
5023 // Always reset component variables/variable templates on component
5024 // reinitialization. Fix for HM79493.
5025 target->functions.init_comp = mputprintf(target->functions.init_comp,
5026 "%s.clean_up();\n", get_genname().c_str());
5027 }
5028 }
5029
5030 void Def_Var_Template::generate_code(CodeGenHelper& cgh)
5031 {
5032 generate_code(cgh.get_outputstruct(this));
5033 }
5034
5035 char *Def_Var_Template::generate_code_str(char *str)
5036 {
5037 const string& t_genname = get_genname();
5038 const char *genname_str = t_genname.c_str();
5039 if (initial_value && initial_value->has_single_expr()) {
5040 // The initial value can be represented by a single C++ expression
5041 // the object is initialized by the constructor.
5042 str = mputprintf(str, "%s %s(%s);\n",
5043 type->get_genname_template(my_scope).c_str(), genname_str,
5044 initial_value->get_single_expr(false).c_str());
5045 } else {
5046 // Use the default constructor.
5047 str = mputprintf(str, "%s %s;\n",
5048 type->get_genname_template(my_scope).c_str(), genname_str);
5049 if (initial_value) {
5050 // The initial value is assigned using subsequent statements.
5051 if (use_runtime_2 && TypeConv::needs_conv_refd(initial_value))
5052 str = TypeConv::gen_conv_code_refd(str, genname_str, initial_value);
5053 else str = initial_value->generate_code_init(str, genname_str);
5054 }
5055 }
5056 if (initial_value && template_restriction != TR_NONE
5057 && gen_restriction_check)
5058 str = Template::generate_restriction_check_code(str, genname_str,
5059 template_restriction);
5060 if (debugger_active) {
5061 str = generate_code_debugger_add_var(str, this);
5062 }
5063 return str;
5064 }
5065
5066 void Def_Var_Template::ilt_generate_code(ILT *ilt)
5067 {
5068 const string& t_genname = get_genname();
5069 const char *genname_str = t_genname.c_str();
5070 char*& def=ilt->get_out_def();
5071 char*& init=ilt->get_out_branches();
5072 def = mputprintf(def, "%s %s;\n",
5073 type->get_genname_template(my_scope).c_str(), genname_str);
5074 if (initial_value) {
5075 init = initial_value->generate_code_init(init, genname_str);
5076 if (template_restriction!=TR_NONE && gen_restriction_check)
5077 init = Template::generate_restriction_check_code(init, genname_str,
5078 template_restriction);
5079 }
5080 }
5081
5082 char *Def_Var_Template::generate_code_init_comp(char *str,
5083 Definition *base_defn)
5084 {
5085 if (initial_value) {
5086 str = initial_value->generate_code_init(str,
5087 base_defn->get_genname_from_scope(my_scope).c_str());
5088 if (template_restriction != TR_NONE && gen_restriction_check)
5089 str = Template::generate_restriction_check_code(str,
5090 base_defn->get_genname_from_scope(my_scope).c_str(),
5091 template_restriction);
5092 }
5093 return str;
5094 }
5095
5096 void Def_Var_Template::dump_internal(unsigned level) const
5097 {
5098 DEBUG(level, "Template variable %s", id->get_dispname().c_str());
5099 if (template_restriction!=TR_NONE)
5100 DEBUG(level + 1, "restriction: %s",
5101 Template::get_restriction_name(template_restriction));
5102 type->dump(level + 1);
5103 if (initial_value) initial_value->dump(level + 1);
5104 }
5105
5106 // =================================
5107 // ===== Def_Timer
5108 // =================================
5109
5110 Def_Timer::~Def_Timer()
5111 {
5112 delete dimensions;
5113 delete default_duration;
5114 }
5115
5116 Def_Timer *Def_Timer::clone() const
5117 {
5118 FATAL_ERROR("Def_Timer::clone");
5119 }
5120
5121 void Def_Timer::set_fullname(const string& p_fullname)
5122 {
5123 Definition::set_fullname(p_fullname);
5124 if (dimensions) dimensions->set_fullname(p_fullname + ".<dimensions>");
5125 if (default_duration)
5126 default_duration->set_fullname(p_fullname + ".<default_duration>");
5127 }
5128
5129 void Def_Timer::set_my_scope(Scope *p_scope)
5130 {
5131 Definition::set_my_scope(p_scope);
5132 if (dimensions) dimensions->set_my_scope(p_scope);
5133 if (default_duration) default_duration->set_my_scope(p_scope);
5134 }
5135
5136 ArrayDimensions *Def_Timer::get_Dimensions()
5137 {
5138 if (!checked) chk();
5139 return dimensions;
5140 }
5141
5142 void Def_Timer::chk()
5143 {
5144 if(checked) return;
5145 Error_Context cntxt(this, "In timer definition `%s'",
5146 id->get_dispname().c_str());
5147 if (dimensions) dimensions->chk();
5148 if (default_duration) {
5149 Error_Context cntxt2(default_duration, "In default duration");
5150 if (dimensions) chk_array_duration(default_duration);
5151 else chk_single_duration(default_duration);
5152 if (!semantic_check_only) {
5153 default_duration->set_code_section(GovernedSimple::CS_POST_INIT);
5154 }
5155 }
5156 checked = true;
5157 if (w_attrib_path) {
5158 w_attrib_path->chk_global_attrib();
5159 w_attrib_path->chk_no_qualif();
5160 }
5161 }
5162
5163 bool Def_Timer::chk_identical(Definition *p_def)
5164 {
5165 chk();
5166 p_def->chk();
5167 if (p_def->get_asstype() != A_TIMER) {
5168 const char *dispname_str = id->get_dispname().c_str();
5169 error("Local definition `%s' is a timer, but the definition inherited "
5170 "from component type `%s' is a %s", dispname_str,
5171 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
5172 p_def->note("The inherited definition of `%s' is here", dispname_str);
5173 return false;
5174 }
5175 Def_Timer *p_def_timer = dynamic_cast<Def_Timer*>(p_def);
5176 if (!p_def_timer) FATAL_ERROR("Def_Timer::chk_identical()");
5177 if (dimensions) {
5178 if (p_def_timer->dimensions) {
5179 if (!dimensions->is_identical(p_def_timer->dimensions)) {
5180 const char *dispname_str = id->get_dispname().c_str();
5181 error("Local timer `%s' and the timer inherited from component type "
5182 "`%s' have different array dimensions", dispname_str,
5183 p_def_timer->get_my_scope()->get_fullname().c_str());
5184 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5185 return false;
5186 }
5187 } else {
5188 const char *dispname_str = id->get_dispname().c_str();
5189 error("Local definition `%s' is a timer array, but the definition "
5190 "inherited from component type `%s' is a single timer", dispname_str,
5191 p_def_timer->get_my_scope()->get_fullname().c_str());
5192 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5193 return false;
5194 }
5195 } else if (p_def_timer->dimensions) {
5196 const char *dispname_str = id->get_dispname().c_str();
5197 error("Local definition `%s' is a single timer, but the definition "
5198 "inherited from component type `%s' is a timer array", dispname_str,
5199 p_def_timer->get_my_scope()->get_fullname().c_str());
5200 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5201 return false;
5202 }
5203 if (default_duration) {
5204 if (p_def_timer->default_duration) {
5205 if (!default_duration->is_unfoldable() &&
5206 !p_def_timer->default_duration->is_unfoldable() &&
5207 !(*default_duration == *p_def_timer->default_duration)) {
5208 const char *dispname_str = id->get_dispname().c_str();
5209 default_duration->warning("Local timer `%s' and the timer inherited "
5210 "from component type `%s' have different default durations",
5211 dispname_str, p_def_timer->get_my_scope()->get_fullname().c_str());
5212 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5213 }
5214 } else {
5215 const char *dispname_str = id->get_dispname().c_str();
5216 default_duration->error("Local timer `%s' has default duration, but "
5217 "the timer inherited from component type `%s' does not", dispname_str,
5218 p_def_timer->get_my_scope()->get_fullname().c_str());
5219 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5220 return false;
5221 }
5222 } else if (p_def_timer->default_duration) {
5223 const char *dispname_str = id->get_dispname().c_str();
5224 error("Local timer `%s' does not have default duration, but the timer "
5225 "inherited from component type `%s' has", dispname_str,
5226 p_def_timer->get_my_scope()->get_fullname().c_str());
5227 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5228 return false;
5229 }
5230 return true;
5231 }
5232
5233 bool Def_Timer::has_default_duration(FieldOrArrayRefs *p_subrefs)
5234 {
5235 // return true in case of any uncertainity
5236 if (!default_duration) return false;
5237 else if (!dimensions || !p_subrefs) return true;
5238 Value *v = default_duration;
5239 size_t nof_dims = dimensions->get_nof_dims();
5240 size_t nof_refs = p_subrefs->get_nof_refs();
5241 size_t upper_limit = nof_dims < nof_refs ? nof_dims : nof_refs;
5242 for (size_t i = 0; i < upper_limit; i++) {
5243 v = v->get_value_refd_last();
5244 if (v->get_valuetype() != Value::V_SEQOF) break;
5245 FieldOrArrayRef *ref = p_subrefs->get_ref(i);
5246 if (ref->get_type() != FieldOrArrayRef::ARRAY_REF) return true;
5247 Value *v_index = ref->get_val()->get_value_refd_last();
5248 if (v_index->get_valuetype() != Value::V_INT) return true;
5249 Int index = v_index->get_val_Int()->get_val()
5250 - dimensions->get_dim_byIndex(i)->get_offset();
5251 if (index >= 0 && index < static_cast<Int>(v->get_nof_comps()))
5252 v = v->get_comp_byIndex(index);
5253 else return true;
5254 }
5255 return v->get_valuetype() != Value::V_NOTUSED;
5256 }
5257
5258 void Def_Timer::chk_single_duration(Value *dur)
5259 {
5260 dur->chk_expr_float(is_local() ?
5261 Type::EXPECTED_DYNAMIC_VALUE : Type::EXPECTED_STATIC_VALUE);
5262 Value *v = dur->get_value_refd_last();
5263 if (v->get_valuetype() == Value::V_REAL) {
5264 ttcn3float v_real = v->get_val_Real();
5265 if ( (v_real<0.0) || isSpecialFloatValue(v_real) ) {
5266 dur->error("A non-negative float value was expected "
5267 "as timer duration instead of `%s'", Real2string(v_real).c_str());
5268 }
5269 }
5270 }
5271
5272 void Def_Timer::chk_array_duration(Value *dur, size_t start_dim)
5273 {
5274 ArrayDimension *dim = dimensions->get_dim_byIndex(start_dim);
5275 bool array_size_known = !dim->get_has_error();
5276 size_t array_size = 0;
5277 if (array_size_known) array_size = dim->get_size();
5278 Value *v = dur->get_value_refd_last();
5279 switch (v->get_valuetype()) {
5280 case Value::V_ERROR:
5281 return;
5282 case Value::V_SEQOF: {
5283 size_t nof_vs = v->get_nof_comps();
5284 // Value-list notation.
5285 if (!v->is_indexed()) {
5286 if (array_size_known) {
5287 if (array_size > nof_vs) {
5288 dur->error("Too few elements in the default duration of timer "
5289 "array: %lu was expected instead of %lu",
5290 (unsigned long)array_size, (unsigned long)nof_vs);
5291 } else if (array_size < nof_vs) {
5292 dur->error("Too many elements in the default duration of timer "
5293 "array: %lu was expected instead of %lu",
5294 (unsigned long)array_size, (unsigned long)nof_vs);
5295 }
5296 }
5297 bool last_dimension = start_dim + 1 >= dimensions->get_nof_dims();
5298 for (size_t i = 0; i < nof_vs; i++) {
5299 Value *array_v = v->get_comp_byIndex(i);
5300 if (array_v->get_valuetype() == Value::V_NOTUSED) continue;
5301 if (last_dimension) chk_single_duration(array_v);
5302 else chk_array_duration(array_v, start_dim + 1);
5303 }
5304 } else {
5305 // Indexed-notation.
5306 bool last_dimension = start_dim + 1 >= dimensions->get_nof_dims();
5307 map<Int, Int> index_map;
5308 for (size_t i = 0; i < nof_vs; i++) {
5309 Value *array_v = v->get_comp_byIndex(i);
5310 if (array_v->get_valuetype() == Value::V_NOTUSED) continue;
5311 if (last_dimension) chk_single_duration(array_v);
5312 else chk_array_duration(array_v, start_dim + 1);
5313 Error_Context cntxt(this, "In timer array element %lu",
5314 (unsigned long)(i + 1));
5315 Value *index = v->get_index_byIndex(i);
5316 dim->chk_index(index, Type::EXPECTED_DYNAMIC_VALUE);
5317 if (index->get_value_refd_last()->get_valuetype() == Value::V_INT) {
5318 const int_val_t *index_int = index->get_value_refd_last()
5319 ->get_val_Int();
5320 if (*index_int > INT_MAX) {
5321 index->error("An integer value less than `%d' was expected for "
5322 "indexing timer array instead of `%s'", INT_MAX,
5323 (index_int->t_str()).c_str());
5324 index->set_valuetype(Value::V_ERROR);
5325 } else {
5326 Int index_val = index_int->get_val();
5327 if (index_map.has_key(index_val)) {
5328 index->error("Duplicate index value `%s' for timer array "
5329 "elements `%s' and `%s'",
5330 Int2string(index_val).c_str(),
5331 Int2string((Int)i + 1).c_str(),
5332 Int2string(*index_map[index_val]).c_str());
5333 index->set_valuetype(Value::V_ERROR);
5334 } else {
5335 index_map.add(index_val, new Int((Int)i + 1));
5336 }
5337 }
5338 }
5339 }
5340 // It's not possible to have "index_map.size() > array_size", since we
5341 // add only correct constant-index values into the map. It's possible
5342 // to create partially initialized timer arrays.
5343 for (size_t i = 0; i < index_map.size(); i++)
5344 delete index_map.get_nth_elem(i);
5345 index_map.clear();
5346 }
5347 break; }
5348 default:
5349 if (array_size_known) {
5350 dur->error("An array value (with %lu elements) was expected as "
5351 "default duration of timer array",
5352 (unsigned long)array_size);
5353 } else {
5354 dur->error("An array value was expected as default duration of timer "
5355 "array");
5356 }
5357 dur->set_valuetype(Value::V_ERROR);
5358 return;
5359 }
5360 }
5361
5362 void Def_Timer::generate_code(output_struct *target, bool)
5363 {
5364 const string& t_genname = get_genname();
5365 const char *genname_str = t_genname.c_str();
5366 const string& dispname = id->get_dispname();
5367 if (dimensions) {
5368 // timer array
5369 const string& array_type = dimensions->get_timer_type();
5370 const char *array_type_str = array_type.c_str();
5371 target->header.global_vars = mputprintf(target->header.global_vars,
5372 "extern %s %s;\n", array_type_str, genname_str);
5373 target->source.global_vars = mputprintf(target->source.global_vars,
5374 "%s %s;\n", array_type_str, genname_str);
5375 target->functions.pre_init = mputstr(target->functions.pre_init, "{\n"
5376 "static const char * const timer_name = \"");
5377 target->functions.pre_init = mputstr(target->functions.pre_init,
5378 dispname.c_str());
5379 target->functions.pre_init = mputprintf(target->functions.pre_init,
5380 "\";\n"
5381 "%s.set_name(timer_name);\n"
5382 "}\n", genname_str);
5383 if (default_duration) target->functions.post_init =
5384 generate_code_array_duration(target->functions.post_init, genname_str,
5385 default_duration);
5386 } else {
5387 // single timer
5388 target->header.global_vars = mputprintf(target->header.global_vars,
5389 "extern TIMER %s;\n", genname_str);
5390 if (default_duration) {
5391 // has default duration
5392 Value *v = default_duration->get_value_refd_last();
5393 if (v->get_valuetype() == Value::V_REAL) {
5394 // duration is known at compilation time -> set in the constructor
5395 target->source.global_vars = mputprintf(target->source.global_vars,
5396 "TIMER %s(\"%s\", %s);\n", genname_str, dispname.c_str(),
5397 v->get_single_expr().c_str());
5398 } else {
5399 // duration is known only at runtime -> set in post_init
5400 target->source.global_vars = mputprintf(target->source.global_vars,
5401 "TIMER %s(\"%s\");\n", genname_str, dispname.c_str());
5402 expression_struct expr;
5403 Code::init_expr(&expr);
5404 expr.expr = mputprintf(expr.expr, "%s.set_default_duration(",
5405 genname_str);
5406 default_duration->generate_code_expr(&expr);
5407 expr.expr = mputc(expr.expr, ')');
5408 target->functions.post_init =
5409 Code::merge_free_expr(target->functions.post_init, &expr);
5410 }
5411 } else {
5412 // does not have default duration
5413 target->source.global_vars = mputprintf(target->source.global_vars,
5414 "TIMER %s(\"%s\");\n", genname_str, dispname.c_str());
5415 }
5416 }
5417 }
5418
5419 void Def_Timer::generate_code(CodeGenHelper& cgh) {
5420 generate_code(cgh.get_current_outputstruct());
5421 }
5422
5423 char *Def_Timer::generate_code_array_duration(char *str,
5424 const char *object_name, Value *dur, size_t start_dim)
5425 {
5426 ArrayDimension *dim = dimensions->get_dim_byIndex(start_dim);
5427 size_t dim_size = dim->get_size();
5428 Value *v = dur->get_value_refd_last();
5429 if (v->get_valuetype() != Value::V_SEQOF
5430 || (v->get_nof_comps() != dim_size && !v->is_indexed()))
5431 FATAL_ERROR("Def_Timer::generate_code_array_duration()");
5432 // Value-list notation.
5433 if (!v->is_indexed()) {
5434 if (start_dim + 1 < dimensions->get_nof_dims()) {
5435 // There are more dimensions, the elements of "v" are arrays a
5436 // temporary reference shall be introduced if the next dimension has
5437 // more than 1 elements.
5438 bool temp_ref_needed =
5439 dimensions->get_dim_byIndex(start_dim + 1)->get_size() > 1;
5440 for (size_t i = 0; i < dim_size; i++) {
5441 Value *v_elem = v->get_comp_byIndex(i);
5442 if (v_elem->get_valuetype() == Value::V_NOTUSED) continue;
5443 if (temp_ref_needed) {
5444 const string& tmp_id = my_scope->get_scope_mod_gen()
5445 ->get_temporary_id();
5446 const char *tmp_str = tmp_id.c_str();
5447 str = mputprintf(str, "{\n"
5448 "%s& %s = %s.array_element(%lu);\n",
5449 dimensions->get_timer_type(start_dim + 1).c_str(),
5450 tmp_str, object_name, (unsigned long)i);
5451 str = generate_code_array_duration(str, tmp_str, v_elem,
5452 start_dim + 1);
5453 str = mputstr(str, "}\n");
5454 } else {
5455 char *tmp_str = mprintf("%s.array_element(%lu)", object_name,
5456 (unsigned long)i);
5457 str = generate_code_array_duration(str, tmp_str, v_elem,
5458 start_dim + 1);
5459 Free(tmp_str);
5460 }
5461 }
5462 } else {
5463 // We are in the last dimension, the elements of "v" are floats.
5464 for (size_t i = 0; i < dim_size; i++) {
5465 Value *v_elem = v->get_comp_byIndex(i);
5466 if (v_elem->get_valuetype() == Value::V_NOTUSED) continue;
5467 expression_struct expr;
5468 Code::init_expr(&expr);
5469 expr.expr = mputprintf(expr.expr,
5470 "%s.array_element(%lu).set_default_duration(",
5471 object_name, (unsigned long)i);
5472 v_elem->generate_code_expr(&expr);
5473 expr.expr = mputc(expr.expr, ')');
5474 str = Code::merge_free_expr(str, &expr);
5475 }
5476 }
5477 // Indexed-list notation.
5478 } else {
5479 if (start_dim + 1 < dimensions->get_nof_dims()) {
5480 bool temp_ref_needed =
5481 dimensions->get_dim_byIndex(start_dim + 1)->get_size() > 1;
5482 for (size_t i = 0; i < v->get_nof_comps(); i++) {
5483 Value *v_elem = v->get_comp_byIndex(i);
5484 if (v_elem->get_valuetype() == Value::V_NOTUSED) continue;
5485 if (temp_ref_needed) {
5486 const string& tmp_id = my_scope->get_scope_mod_gen()
5487 ->get_temporary_id();
5488 const string& idx_id = my_scope->get_scope_mod_gen()
5489 ->get_temporary_id();
5490 const char *tmp_str = tmp_id.c_str();
5491 str = mputstr(str, "{\n");
5492 str = mputprintf(str, "int %s;\n", idx_id.c_str());
5493 str = v->get_index_byIndex(i)->generate_code_init(str,
5494 idx_id.c_str());
5495 str = mputprintf(str, "%s& %s = %s.array_element(%s);\n",
5496 dimensions->get_timer_type(start_dim + 1).c_str(),
5497 tmp_str, object_name, idx_id.c_str());
5498 str = generate_code_array_duration(str, tmp_str, v_elem,
5499 start_dim + 1);
5500 str = mputstr(str, "}\n");
5501 } else {
5502 const string& idx_id = my_scope->get_scope_mod_gen()
5503 ->get_temporary_id();
5504 str = mputstr(str, "{\n");
5505 str = mputprintf(str, "int %s;\n", idx_id.c_str());
5506 str = v->get_index_byIndex(i)->generate_code_init(str,
5507 idx_id.c_str());
5508 char *tmp_str = mprintf("%s.array_element(%s)", object_name,
5509 idx_id.c_str());
5510 str = generate_code_array_duration(str, tmp_str, v_elem,
5511 start_dim + 1);
5512 str = mputstr(str, "}\n");
5513 Free(tmp_str);
5514 }
5515 }
5516 } else {
5517 for (size_t i = 0; i < v->get_nof_comps(); i++) {
5518 Value *v_elem = v->get_comp_byIndex(i);
5519 if (v_elem->get_valuetype() == Value::V_NOTUSED) continue;
5520 expression_struct expr;
5521 Code::init_expr(&expr);
5522 str = mputstr(str, "{\n");
5523 const string& idx_id = my_scope->get_scope_mod_gen()
5524 ->get_temporary_id();
5525 str = mputprintf(str, "int %s;\n", idx_id.c_str());
5526 str = v->get_index_byIndex(i)->generate_code_init(str,
5527 idx_id.c_str());
5528 str = mputprintf(str,
5529 "%s.array_element(%s).set_default_duration(",
5530 object_name, idx_id.c_str());
5531 v_elem->generate_code_expr(&expr);
5532 expr.expr = mputc(expr.expr, ')');
5533 str = Code::merge_free_expr(str, &expr);
5534 str = mputstr(str, "}\n");
5535 }
5536 }
5537 }
5538 return str;
5539 }
5540
5541 char *Def_Timer::generate_code_str(char *str)
5542 {
5543 const string& t_genname = get_genname();
5544 const char *genname_str = t_genname.c_str();
5545 const string& dispname = id->get_dispname();
5546 if (dimensions) {
5547 // timer array
5548 const string& array_type = dimensions->get_timer_type();
5549 const char *array_type_str = array_type.c_str();
5550 str = mputprintf(str, "%s %s;\n", array_type_str, genname_str);
5551 str = mputstr(str, "{\n"
5552 "static const char * const timer_name = \"");
5553 str = mputstr(str, dispname.c_str());
5554 str = mputprintf(str, "\";\n"
5555 "%s.set_name(timer_name);\n"
5556 "}\n", genname_str);
5557 if (default_duration) str = generate_code_array_duration(str,
5558 genname_str, default_duration);
5559 } else {
5560 // single timer
5561 if (default_duration && default_duration->has_single_expr()) {
5562 // the default duration can be passed to the constructor
5563 str = mputprintf(str, "TIMER %s(\"%s\", %s);\n", genname_str,
5564 dispname.c_str(), default_duration->get_single_expr().c_str());
5565 } else {
5566 // only the name is passed to the constructor
5567 str = mputprintf(str, "TIMER %s(\"%s\");\n", genname_str,
5568 dispname.c_str());
5569 if (default_duration) {
5570 // the default duration is set explicitly
5571 expression_struct expr;
5572 Code::init_expr(&expr);
5573 expr.expr = mputprintf(expr.expr, "%s.set_default_duration(",
5574 genname_str);
5575 default_duration->generate_code_expr(&expr);
5576 expr.expr = mputc(expr.expr, ')');
5577 str = Code::merge_free_expr(str, &expr);
5578 }
5579 }
5580 }
5581 if (debugger_active) {
5582 str = generate_code_debugger_add_var(str, this);
5583 }
5584 return str;
5585 }
5586
5587 void Def_Timer::ilt_generate_code(ILT *ilt)
5588 {
5589 const string& t_genname = get_genname();
5590 const char *genname_str = t_genname.c_str();
5591 const string& dispname = id->get_dispname();
5592
5593 char*& def = ilt->get_out_def();
5594 char*& init = ilt->get_out_branches();
5595
5596 if (dimensions) {
5597 // timer array
5598 const string& array_type = dimensions->get_timer_type();
5599 const char *array_type_str = array_type.c_str();
5600 def = mputprintf(def, "%s %s;\n", array_type_str, genname_str);
5601 def = mputstr(def, "{\n"
5602 "static const char * const timer_names[] = { ");
5603 def = dimensions->generate_element_names(def, dispname);
5604 def = mputprintf(def, " };\n"
5605 "%s.set_name(%lu, timer_names);\n"
5606 "}\n", genname_str, (unsigned long) dimensions->get_array_size());
5607 if (default_duration) init = generate_code_array_duration(init,
5608 genname_str, default_duration);
5609 } else {
5610 // single timer
5611 if (default_duration) {
5612 // has default duration
5613 Value *v = default_duration->get_value_refd_last();
5614 if (v->get_valuetype() == Value::V_REAL) {
5615 // duration is known at compilation time -> set in the constructor
5616 def = mputprintf(def, "TIMER %s(\"%s\", %s);\n", genname_str,
5617 dispname.c_str(), v->get_single_expr().c_str());
5618 } else {
5619 // duration is known only at runtime -> set when control reaches the
5620 // timer definition
5621 def = mputprintf(def, "TIMER %s(\"%s\");\n", genname_str,
5622 dispname.c_str());
5623 expression_struct expr;
5624 Code::init_expr(&expr);
5625 expr.expr = mputprintf(expr.expr, "%s.set_default_duration(",
5626 genname_str);
5627 default_duration->generate_code_expr(&expr);
5628 expr.expr = mputc(expr.expr, ')');
5629 init = Code::merge_free_expr(init, &expr);
5630 }
5631 } else {
5632 // does not have default duration
5633 def = mputprintf(def, "TIMER %s(\"%s\");\n", genname_str,
5634 dispname.c_str());
5635 }
5636 }
5637 }
5638
5639 char *Def_Timer::generate_code_init_comp(char *str, Definition *base_defn)
5640 {
5641 if (default_duration) {
5642 Def_Timer *base_timer_defn = dynamic_cast<Def_Timer*>(base_defn);
5643 if (!base_timer_defn || !base_timer_defn->default_duration)
5644 FATAL_ERROR("Def_Timer::generate_code_init_comp()");
5645 // initializer is not needed if the default durations are the same
5646 // constants in both timers
5647 if (default_duration->is_unfoldable() ||
5648 base_timer_defn->default_duration->is_unfoldable() ||
5649 !(*default_duration == *base_timer_defn->default_duration)) {
5650 if (dimensions) {
5651 str = generate_code_array_duration(str,
5652 base_timer_defn->get_genname_from_scope(my_scope).c_str(),
5653 default_duration);
5654 } else {
5655 expression_struct expr;
5656 Code::init_expr(&expr);
5657 expr.expr = mputprintf(expr.expr, "%s.set_default_duration(",
5658 base_timer_defn->get_genname_from_scope(my_scope).c_str());
5659 default_duration->generate_code_expr(&expr);
5660 expr.expr = mputc(expr.expr, ')');
5661 str = Code::merge_free_expr(str, &expr);
5662 }
5663 }
5664 }
5665 return str;
5666 }
5667
5668 void Def_Timer::dump_internal(unsigned level) const
5669 {
5670 DEBUG(level, "Timer: %s", id->get_dispname().c_str());
5671 if (dimensions) dimensions->dump(level + 1);
5672 if (default_duration) {
5673 DEBUG(level + 1, "Default duration:");
5674 default_duration->dump(level + 1);
5675 }
5676 }
5677
5678 // =================================
5679 // ===== Def_Port
5680 // =================================
5681
5682 Def_Port::Def_Port(Identifier *p_id, Reference *p_tref,
5683 ArrayDimensions *p_dims)
5684 : Definition(A_PORT, p_id), type_ref(p_tref), port_type(0),
5685 dimensions(p_dims)
5686 {
5687 if (!p_tref) FATAL_ERROR("Def_Port::Def_Port()");
5688 }
5689
5690 Def_Port::~Def_Port()
5691 {
5692 delete type_ref;
5693 delete dimensions;
5694 }
5695
5696 Def_Port *Def_Port::clone() const
5697 {
5698 FATAL_ERROR("Def_Port::clone");
5699 }
5700
5701 void Def_Port::set_fullname(const string& p_fullname)
5702 {
5703 Definition::set_fullname(p_fullname);
5704 type_ref->set_fullname(p_fullname + ".<type_ref>");
5705 if (dimensions) dimensions->set_fullname(p_fullname);
5706 }
5707
5708 void Def_Port::set_my_scope(Scope *p_scope)
5709 {
5710 Definition::set_my_scope(p_scope);
5711 type_ref->set_my_scope(p_scope);
5712 if (dimensions) dimensions->set_my_scope(p_scope);
5713 }
5714
5715 Type *Def_Port::get_Type()
5716 {
5717 chk();
5718 return port_type;
5719 }
5720
5721 ArrayDimensions *Def_Port::get_Dimensions()
5722 {
5723 if (!checked) chk();
5724 return dimensions;
5725 }
5726
5727 void Def_Port::chk()
5728 {
5729 if (checked) return;
5730 checked = true;
5731 Error_Context cntxt(this, "In port definition `%s'",
5732 id->get_dispname().c_str());
5733 Common::Assignment *ass = type_ref->get_refd_assignment();
5734 if (ass) {
5735 if (ass->get_asstype() == A_TYPE) {
5736 Type *t = ass->get_Type()->get_type_refd_last();
5737 if (t->get_typetype() == Type::T_PORT) port_type = t;
5738 else type_ref->error("Type reference `%s' does not refer to a "
5739 "port type", type_ref->get_dispname().c_str());
5740 } else type_ref->error("Reference `%s' does not refer to a "
5741 "type", type_ref->get_dispname().c_str());
5742 }
5743 if (dimensions) dimensions->chk();
5744 if (w_attrib_path) {
5745 w_attrib_path->chk_global_attrib();
5746 w_attrib_path->chk_no_qualif();
5747 }
5748 }
5749
5750 bool Def_Port::chk_identical(Definition *p_def)
5751 {
5752 chk();
5753 p_def->chk();
5754 if (p_def->get_asstype() != A_PORT) {
5755 const char *dispname_str = id->get_dispname().c_str();
5756 error("Local definition `%s' is a port, but the definition inherited "
5757 "from component type `%s' is a %s", dispname_str,
5758 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
5759 p_def->note("The inherited definition of `%s' is here", dispname_str);
5760 return false;
5761 }
5762 Def_Port *p_def_port = dynamic_cast<Def_Port*>(p_def);
5763 if (!p_def_port) FATAL_ERROR("Def_Port::chk_identical()");
5764 if (port_type && p_def_port->port_type &&
5765 port_type != p_def_port->port_type) {
5766 const char *dispname_str = id->get_dispname().c_str();
5767 type_ref->error("Local port `%s' has type `%s', but the port inherited "
5768 "from component type `%s' has type `%s'", dispname_str,
5769 port_type->get_typename().c_str(),
5770 p_def_port->get_my_scope()->get_fullname().c_str(),
5771 p_def_port->port_type->get_typename().c_str());
5772 p_def_port->note("The inherited port `%s' is here", dispname_str);
5773 return false;
5774 }
5775 if (dimensions) {
5776 if (p_def_port->dimensions) {
5777 if (!dimensions->is_identical(p_def_port->dimensions)) {
5778 const char *dispname_str = id->get_dispname().c_str();
5779 error("Local port `%s' and the port inherited from component type "
5780 "`%s' have different array dimensions", dispname_str,
5781 p_def_port->get_my_scope()->get_fullname().c_str());
5782 p_def_port->note("The inherited port `%s' is here", dispname_str);
5783 return false;
5784 }
5785 } else {
5786 const char *dispname_str = id->get_dispname().c_str();
5787 error("Local definition `%s' is a port array, but the definition "
5788 "inherited from component type `%s' is a single port", dispname_str,
5789 p_def_port->get_my_scope()->get_fullname().c_str());
5790 p_def_port->note("The inherited port `%s' is here", dispname_str);
5791 return false;
5792 }
5793 } else if (p_def_port->dimensions) {
5794 const char *dispname_str = id->get_dispname().c_str();
5795 error("Local definition `%s' is a single port, but the definition "
5796 "inherited from component type `%s' is a port array", dispname_str,
5797 p_def_port->get_my_scope()->get_fullname().c_str());
5798 p_def_port->note("The inherited port `%s' is here", dispname_str);
5799 return false;
5800 }
5801 return true;
5802 }
5803
5804 void Def_Port::generate_code(output_struct *target, bool)
5805 {
5806 const string& t_genname = get_genname();
5807 const char *genname_str = t_genname.c_str();
5808 const string& type_genname = port_type->get_genname_value(my_scope);
5809 const string& dispname = id->get_dispname();
5810 if (dimensions) {
5811 // port array
5812 const string& array_type = dimensions->get_port_type(type_genname);
5813 const char *array_type_str = array_type.c_str();
5814 target->header.global_vars = mputprintf(target->header.global_vars,
5815 "extern %s %s;\n", array_type_str, genname_str);
5816 target->source.global_vars = mputprintf(target->source.global_vars,
5817 "%s %s;\n", array_type_str, genname_str);
5818 target->functions.pre_init = mputstr(target->functions.pre_init, "{\n"
5819 "static const char * const port_name = \"");
5820 target->functions.pre_init = mputstr(target->functions.pre_init,
5821 dispname.c_str());
5822 target->functions.pre_init = mputprintf(target->functions.pre_init,
5823 "\";\n"
5824 "%s.set_name(port_name);\n"
5825 "}\n", genname_str);
5826 } else {
5827 // single port
5828 const char *type_genname_str = type_genname.c_str();
5829 target->header.global_vars = mputprintf(target->header.global_vars,
5830 "extern %s %s;\n", type_genname_str, genname_str);
5831 target->source.global_vars = mputprintf(target->source.global_vars,
5832 "%s %s(\"%s\");\n", type_genname_str, genname_str, dispname.c_str());
5833 }
5834 target->functions.init_comp = mputprintf(target->functions.init_comp,
5835 "%s.activate_port();\n", genname_str);
5836 }
5837
5838 void Def_Port::generate_code(CodeGenHelper& cgh) {
5839 generate_code(cgh.get_current_outputstruct());
5840 }
5841
5842 char *Def_Port::generate_code_init_comp(char *str, Definition *base_defn)
5843 {
5844 return mputprintf(str, "%s.activate_port();\n",
5845 base_defn->get_genname_from_scope(my_scope).c_str());
5846 }
5847
5848 void Def_Port::dump_internal(unsigned level) const
5849 {
5850 DEBUG(level, "Port: %s", id->get_dispname().c_str());
5851 DEBUG(level + 1, "Port type:");
5852 type_ref->dump(level + 2);
5853 if (dimensions) dimensions->dump(level + 1);
5854 }
5855
5856 // =================================
5857 // ===== Def_Function_Base
5858 // =================================
5859
5860 Def_Function_Base::asstype_t Def_Function_Base::determine_asstype(
5861 bool is_external, bool has_return_type, bool returns_template)
5862 {
5863 if (is_external) {
5864 if (has_return_type) {
5865 if (returns_template) return A_EXT_FUNCTION_RTEMP;
5866 else return A_EXT_FUNCTION_RVAL;
5867 } else {
5868 if (returns_template)
5869 FATAL_ERROR("Def_Function_Base::determine_asstype()");
5870 return A_EXT_FUNCTION;
5871 }
5872 } else { // not an external function
5873 if (has_return_type) {
5874 if (returns_template) return A_FUNCTION_RTEMP;
5875 else return A_FUNCTION_RVAL;
5876 } else {
5877 if (returns_template)
5878 FATAL_ERROR("Def_Function_Base::determine_asstype()");
5879 return A_FUNCTION;
5880 }
5881 }
5882 }
5883
5884 Def_Function_Base::Def_Function_Base(const Def_Function_Base& p)
5885 : Definition(p), prototype(PROTOTYPE_NONE), input_type(0), output_type(0)
5886 {
5887 fp_list = p.fp_list->clone();
5888 fp_list->set_my_def(this);
5889 return_type = p.return_type ? p.return_type->clone() : 0;
5890 template_restriction = p.template_restriction;
5891 }
5892
5893 Def_Function_Base::Def_Function_Base(bool is_external, Identifier *p_id,
5894 FormalParList *p_fpl, Type *p_return_type, bool returns_template,
5895 template_restriction_t p_template_restriction)
5896 : Definition(determine_asstype(is_external, p_return_type != 0,
5897 returns_template), p_id), fp_list(p_fpl), return_type(p_return_type),
5898 prototype(PROTOTYPE_NONE), input_type(0), output_type(0),
5899 template_restriction(p_template_restriction)
5900 {
5901 if (!p_fpl) FATAL_ERROR("Def_Function_Base::Def_Function_Base()");
5902 fp_list->set_my_def(this);
5903 if (return_type) return_type->set_ownertype(Type::OT_FUNCTION_DEF, this);
5904 }
5905
5906 Def_Function_Base::~Def_Function_Base()
5907 {
5908 delete fp_list;
5909 delete return_type;
5910 }
5911
5912 void Def_Function_Base::set_fullname(const string& p_fullname)
5913 {
5914 Definition::set_fullname(p_fullname);
5915 fp_list->set_fullname(p_fullname + ".<formal_par_list>");
5916 if (return_type) return_type->set_fullname(p_fullname + ".<return_type>");
5917 }
5918
5919 void Def_Function_Base::set_my_scope(Scope *p_scope)
5920 {
5921 Definition::set_my_scope(p_scope);
5922 fp_list->set_my_scope(p_scope);
5923 if (return_type) return_type->set_my_scope(p_scope);
5924 }
5925
5926 Type *Def_Function_Base::get_Type()
5927 {
5928 if (!checked) chk();
5929 return return_type;
5930 }
5931
5932 FormalParList *Def_Function_Base::get_FormalParList()
5933 {
5934 if (!checked) chk();
5935 return fp_list;
5936 }
5937
5938 const char *Def_Function_Base::get_prototype_name() const
5939 {
5940 switch (prototype) {
5941 case PROTOTYPE_NONE:
5942 return "<no prototype>";
5943 case PROTOTYPE_CONVERT:
5944 return "convert";
5945 case PROTOTYPE_FAST:
5946 return "fast";
5947 case PROTOTYPE_BACKTRACK:
5948 return "backtrack";
5949 case PROTOTYPE_SLIDING:
5950 return "sliding";
5951 default:
5952 return "<unknown prototype>";
5953 }
5954 }
5955
5956 void Def_Function_Base::chk_prototype()
5957 {
5958 switch (prototype) {
5959 case PROTOTYPE_NONE:
5960 // return immediately
5961 return;
5962 case PROTOTYPE_CONVERT:
5963 case PROTOTYPE_FAST:
5964 case PROTOTYPE_BACKTRACK:
5965 case PROTOTYPE_SLIDING:
5966 // perform the checks below
5967 break;
5968 default:
5969 FATAL_ERROR("Def_Function_Base::chk_prototype()");
5970 }
5971 // checking the formal parameter list
5972 if (prototype == PROTOTYPE_CONVERT) {
5973 if (fp_list->get_nof_fps() == 1) {
5974 FormalPar *par = fp_list->get_fp_byIndex(0);
5975 if (par->get_asstype() == A_PAR_VAL_IN) {
5976 input_type = par->get_Type();
5977 } else {
5978 par->error("The parameter must be an `in' value parameter for "
5979 "attribute `prototype(%s)' instead of %s", get_prototype_name(),
5980 par->get_assname());
5981 }
5982 } else {
5983 fp_list->error("The function must have one parameter instead of %lu "
5984 "for attribute `prototype(%s)'", (unsigned long) fp_list->get_nof_fps(),
5985 get_prototype_name());
5986 }
5987 } else { // not PROTOTYPE_CONVERT
5988 if (fp_list->get_nof_fps() == 2) {
5989 FormalPar *first_par = fp_list->get_fp_byIndex(0);
5990 if (prototype == PROTOTYPE_SLIDING) {
5991 if (first_par->get_asstype() == A_PAR_VAL_INOUT) {
5992 Type *first_par_type = first_par->get_Type();
5993 switch (first_par_type->get_type_refd_last()
5994 ->get_typetype_ttcn3()) {
5995 case Type::T_ERROR:
5996 case Type::T_OSTR:
5997 case Type::T_CSTR:
5998 case Type::T_BSTR:
5999 input_type = first_par_type;
6000 break;
6001 default:
6002 first_par_type->error("The type of the first parameter must be "
6003 "`octetstring' or `charstring' or `bitstring' for attribute "
6004 "`prototype(%s)' instead of `%s'", get_prototype_name(),
6005 first_par_type->get_typename().c_str());
6006 }
6007 } else {
6008 first_par->error("The first parameter must be an `inout' value "
6009 "parameter for attribute `prototype(%s)' instead of %s",
6010 get_prototype_name(), first_par->get_assname());
6011 }
6012 } else {
6013 if (first_par->get_asstype() == A_PAR_VAL_IN) {
6014 input_type = first_par->get_Type();
6015 } else {
6016 first_par->error("The first parameter must be an `in' value "
6017 "parameter for attribute `prototype(%s)' instead of %s",
6018 get_prototype_name(), first_par->get_assname());
6019 }
6020 }
6021 FormalPar *second_par = fp_list->get_fp_byIndex(1);
6022 if (second_par->get_asstype() == A_PAR_VAL_OUT) {
6023 output_type = second_par->get_Type();
6024 } else {
6025 second_par->error("The second parameter must be an `out' value "
6026 "parameter for attribute `prototype(%s)' instead of %s",
6027 get_prototype_name(), second_par->get_assname());
6028 }
6029 } else {
6030 fp_list->error("The function must have two parameters for attribute "
6031 "`prototype(%s)' instead of %lu", get_prototype_name(),
6032 (unsigned long) fp_list->get_nof_fps());
6033 }
6034 }
6035 // checking the return type
6036 if (prototype == PROTOTYPE_FAST) {
6037 if (return_type) {
6038 return_type->error("The function cannot have return type for "
6039 "attribute `prototype(%s)'", get_prototype_name());
6040 }
6041 } else {
6042 if (return_type) {
6043 if (asstype == A_FUNCTION_RTEMP || asstype == A_EXT_FUNCTION_RTEMP)
6044 return_type->error("The function must return a value instead of a "
6045 "template for attribute `prototype(%s)'", get_prototype_name());
6046 if (prototype == PROTOTYPE_CONVERT) {
6047 output_type = return_type;
6048 } else {
6049 switch (return_type->get_type_refd_last()->get_typetype_ttcn3()) {
6050 case Type::T_ERROR:
6051 case Type::T_INT:
6052 break;
6053 default:
6054 return_type->error("The return type of the function must be "
6055 "`integer' instead of `%s' for attribute `prototype(%s)'",
6056 return_type->get_typename().c_str(), get_prototype_name());
6057 }
6058 }
6059 } else {
6060 error("The function must have return type for attribute "
6061 "`prototype(%s)'", get_prototype_name());
6062 }
6063 }
6064 // checking the 'runs on' clause
6065 if (get_RunsOnType()) {
6066 error("The function cannot have `runs on' clause for attribute "
6067 "`prototype(%s)'", get_prototype_name());
6068 }
6069 }
6070
6071 Type *Def_Function_Base::get_input_type()
6072 {
6073 if (!checked) chk();
6074 return input_type;
6075 }
6076
6077 Type *Def_Function_Base::get_output_type()
6078 {
6079 if (!checked) chk();
6080 return output_type;
6081 }
6082
6083
6084 // =================================
6085 // ===== Def_Function
6086 // =================================
6087
6088 Def_Function::Def_Function(Identifier *p_id, FormalParList *p_fpl,
6089 Reference *p_runs_on_ref, Type *p_return_type,
6090 bool returns_template,
6091 template_restriction_t p_template_restriction,
6092 StatementBlock *p_block)
6093 : Def_Function_Base(false, p_id, p_fpl, p_return_type, returns_template,
6094 p_template_restriction),
6095 runs_on_ref(p_runs_on_ref), runs_on_type(0), block(p_block),
6096 is_startable(false), transparent(false)
6097 {
6098 if (!p_block) FATAL_ERROR("Def_Function::Def_Function()");
6099 block->set_my_def(this);
6100 }
6101
6102 Def_Function::~Def_Function()
6103 {
6104 delete runs_on_ref;
6105 delete block;
6106 }
6107
6108 Def_Function *Def_Function::clone() const
6109 {
6110 FATAL_ERROR("Def_Function::clone");
6111 }
6112
6113 void Def_Function::set_fullname(const string& p_fullname)
6114 {
6115 Def_Function_Base::set_fullname(p_fullname);
6116 if (runs_on_ref) runs_on_ref->set_fullname(p_fullname + ".<runs_on_type>");
6117 block->set_fullname(p_fullname + ".<statement_block>");
6118 }
6119
6120 void Def_Function::set_my_scope(Scope *p_scope)
6121 {
6122 bridgeScope.set_parent_scope(p_scope);
6123 bridgeScope.set_scopeMacro_name(id->get_dispname());
6124
6125 Def_Function_Base::set_my_scope(&bridgeScope);
6126 if (runs_on_ref) runs_on_ref->set_my_scope(&bridgeScope);
6127 block->set_my_scope(fp_list);
6128 }
6129
6130 Type *Def_Function::get_RunsOnType()
6131 {
6132 if (!checked) chk();
6133 return runs_on_type;
6134 }
6135
6136 RunsOnScope *Def_Function::get_runs_on_scope(Type *comptype)
6137 {
6138 Module *my_module = dynamic_cast<Module*>(my_scope->get_scope_mod());
6139 if (!my_module) FATAL_ERROR("Def_Function::get_runs_on_scope()");
6140 return my_module->get_runs_on_scope(comptype);
6141 }
6142
6143 void Def_Function::chk()
6144 {
6145 if (checked) return;
6146 checked = true;
6147 Error_Context cntxt(this, "In function definition `%s'",
6148 id->get_dispname().c_str());
6149 // checking the `runs on' clause
6150 if (runs_on_ref) {
6151 Error_Context cntxt2(runs_on_ref, "In `runs on' clause");
6152 runs_on_type = runs_on_ref->chk_comptype_ref();
6153 // override the scope of the formal parameter list
6154 if (runs_on_type) {
6155 Scope *runs_on_scope = get_runs_on_scope(runs_on_type);
6156 runs_on_scope->set_parent_scope(my_scope);
6157 fp_list->set_my_scope(runs_on_scope);
6158 }
6159 }
6160 // checking the formal parameter list
6161 fp_list->chk(asstype);
6162 // checking of return type
6163 if (return_type) {
6164 Error_Context cntxt2(return_type, "In return type");
6165 return_type->chk();
6166 return_type->chk_as_return_type(asstype == A_FUNCTION_RVAL,"function");
6167 }
6168 // decision of startability
6169 is_startable = runs_on_ref != 0;
6170 if (is_startable && !fp_list->get_startability()) is_startable = false;
6171 if (is_startable && return_type && return_type->is_component_internal())
6172 is_startable = false;
6173 // checking of statement block
6174 block->chk();
6175 if (return_type) {
6176 // checking the presence of return statements
6177 switch (block->has_return()) {
6178 case StatementBlock::RS_NO:
6179 error("The function has return type, but it does not have any return "
6180 "statement");
6181 break;
6182 case StatementBlock::RS_MAYBE:
6183 error("The function has return type, but control might leave it "
6184 "without reaching a return statement");
6185 default:
6186 break;
6187 }
6188 }
6189 if (!semantic_check_only) {
6190 fp_list->set_genname(get_genname());
6191 block->set_code_section(GovernedSimple::CS_INLINE);
6192 }
6193 if (w_attrib_path) {
6194 w_attrib_path->chk_global_attrib();
6195 w_attrib_path->chk_no_qualif();
6196 Ttcn::ExtensionAttributes * extattrs = parse_extattributes(w_attrib_path);
6197 if (extattrs != 0) { // NULL means parsing error
6198 size_t num_atrs = extattrs->size();
6199 for (size_t i=0; i < num_atrs; ++i) {
6200 ExtensionAttribute &ea = extattrs->get(i);
6201 switch (ea.get_type()) {
6202 case ExtensionAttribute::PROTOTYPE: {
6203 if (get_prototype() != Def_Function_Base::PROTOTYPE_NONE) {
6204 ea.error("Duplicate attribute `prototype'");
6205 }
6206 Def_Function_Base::prototype_t proto = ea.get_proto();
6207 set_prototype(proto);
6208 break; }
6209
6210 case ExtensionAttribute::ANYTYPELIST: // ignore it
6211 case ExtensionAttribute::NONE: // erroneous, do not issue an error
6212 break;
6213
6214 case ExtensionAttribute::TRANSPARENT:
6215 transparent = true;
6216 break;
6217
6218 case ExtensionAttribute::ENCODE:
6219 case ExtensionAttribute::DECODE:
6220 case ExtensionAttribute::ERRORBEHAVIOR:
6221 case ExtensionAttribute::PRINTING:
6222 ea.error("Extension attribute 'encode', 'decode', 'errorbehavior'"
6223 " or 'printing' can only be applied to external functions");
6224 // fall through
6225
6226 default: // complain
6227 ea.error("Function definition can only have the 'prototype'"
6228 " extension attribute");
6229 break;
6230 }
6231 }
6232 delete extattrs;
6233 }
6234 }
6235 chk_prototype();
6236 }
6237
6238 bool Def_Function::chk_startable()
6239 {
6240 if (!checked) chk();
6241 if (is_startable) return true;
6242 if (!runs_on_ref) error("Function `%s' cannot be started on a parallel "
6243 "test component because it does not have `runs on' clause",
6244 get_fullname().c_str());
6245 fp_list->chk_startability("Function", get_fullname().c_str());
6246 if (return_type && return_type->is_component_internal()) {
6247 map<Type*,void> type_chain;
6248 char* err_str = mprintf("the return type or embedded in the return type "
6249 "of function `%s' if it is started on a parallel test component",
6250 get_fullname().c_str());
6251 return_type->chk_component_internal(type_chain, err_str);
6252 Free(err_str);
6253 }
6254 return false;
6255 }
6256
6257 void Def_Function::generate_code(output_struct *target, bool)
6258 {
6259 transparency_holder glass(*this);
6260 const string& t_genname = get_genname();
6261 const char *genname_str = t_genname.c_str();
6262 const char *dispname_str = id->get_dispname().c_str();
6263 string return_type_name;
6264 switch (asstype) {
6265 case A_FUNCTION:
6266 return_type_name = "void";
6267 break;
6268 case A_FUNCTION_RVAL:
6269 return_type_name = return_type->get_genname_value(my_scope);
6270 break;
6271 case A_FUNCTION_RTEMP:
6272 return_type_name = return_type->get_genname_template(my_scope);
6273 break;
6274 default:
6275 FATAL_ERROR("Def_Function::generate_code()");
6276 }
6277 const char *return_type_str = return_type_name.c_str();
6278
6279 // assemble the function body first (this also determines which parameters
6280 // are never used)
6281 char* body = create_location_object(memptystr(), "FUNCTION", dispname_str);
6282 if (!enable_set_bound_out_param)
6283 body = fp_list->generate_code_set_unbound(body); // conform the standard out parameter is unbound
6284 body = fp_list->generate_shadow_objects(body);
6285 if (debugger_active) {
6286 body = generate_code_debugger_function_init(body, this);
6287 }
6288 body = block->generate_code(body);
6289 // smart formal parameter list (names of unused parameters are omitted)
6290 char *formal_par_list = fp_list->generate_code(memptystr());
6291 fp_list->generate_code_defval(target);
6292 // function prototype
6293 target->header.function_prototypes =
6294 mputprintf(target->header.function_prototypes, "extern %s %s(%s);\n",
6295 return_type_str, genname_str, formal_par_list);
6296
6297 // function body
6298 target->source.function_bodies = mputprintf(target->source.function_bodies,
6299 "%s %s(%s)\n"
6300 "{\n"
6301 "%s"
6302 "}\n\n", return_type_str, genname_str, formal_par_list, body);
6303 Free(formal_par_list);
6304 Free(body);
6305
6306 if (is_startable) {
6307 size_t nof_fps = fp_list->get_nof_fps();
6308 // use the full list of formal parameters here (since they are all logged)
6309 char *full_formal_par_list = fp_list->generate_code(memptystr(), nof_fps);
6310 // starter function (stub)
6311 // function prototype
6312 target->header.function_prototypes =
6313 mputprintf(target->header.function_prototypes,
6314 "extern void start_%s(const COMPONENT& component_reference%s%s);\n",
6315 genname_str, nof_fps>0?", ":"", full_formal_par_list);
6316 // function body
6317 body = mprintf("void start_%s(const COMPONENT& component_reference%s"
6318 "%s)\n"
6319 "{\n"
6320 "TTCN_Logger::begin_event(TTCN_Logger::PARALLEL_PTC);\n"
6321 "TTCN_Logger::log_event_str(\"Starting function %s(\");\n",
6322 genname_str, nof_fps>0?", ":"", full_formal_par_list, dispname_str);
6323 for (size_t i = 0; i < nof_fps; i++) {
6324 if (i > 0) body = mputstr(body,
6325 "TTCN_Logger::log_event_str(\", \");\n");
6326 body = mputprintf(body, "%s.log();\n",
6327 fp_list->get_fp_byIndex(i)->get_reference_name(my_scope).c_str());
6328 }
6329 body = mputprintf(body,
6330 "TTCN_Logger::log_event_str(\") on component \");\n"
6331 "component_reference.log();\n"
6332 "TTCN_Logger::log_char('.');\n"
6333 "TTCN_Logger::end_event();\n"
6334 "Text_Buf text_buf;\n"
6335 "TTCN_Runtime::prepare_start_component(component_reference, "
6336 "\"%s\", \"%s\", text_buf);\n",
6337 my_scope->get_scope_mod()->get_modid().get_dispname().c_str(),
6338 dispname_str);
6339 for (size_t i = 0; i < nof_fps; i++) {
6340 body = mputprintf(body, "%s.encode_text(text_buf);\n",
6341 fp_list->get_fp_byIndex(i)->get_reference_name(my_scope).c_str());
6342 }
6343 body = mputstr(body, "TTCN_Runtime::send_start_component(text_buf);\n"
6344 "}\n\n");
6345 target->source.function_bodies = mputstr(target->source.function_bodies,
6346 body);
6347 Free(body);
6348
6349 // an entry in start_ptc_function
6350 body = mprintf("if (!strcmp(function_name, \"%s\")) {\n",
6351 dispname_str);
6352 if (nof_fps > 0) {
6353 body = fp_list->generate_code_object(body, "", ' ');
6354 for (size_t i = 0; i < nof_fps; i++) {
6355 body = mputprintf(body, "%s.decode_text(function_arguments);\n",
6356 fp_list->get_fp_byIndex(i)->get_reference_name(my_scope).c_str());
6357 }
6358 body = mputprintf(body,
6359 "TTCN_Logger::begin_event(TTCN_Logger::PARALLEL_PTC);\n"
6360 "TTCN_Logger::log_event_str(\"Starting function %s(\");\n",
6361 dispname_str);
6362 for (size_t i = 0; i < nof_fps; i++) {
6363 if (i > 0) body = mputstr(body,
6364 "TTCN_Logger::log_event_str(\", \");\n");
6365 body = mputprintf(body, "%s.log();\n",
6366 fp_list->get_fp_byIndex(i)->get_reference_name(my_scope).c_str());
6367 }
6368 body = mputstr(body, "TTCN_Logger::log_event_str(\").\");\n"
6369 "TTCN_Logger::end_event();\n");
6370 } else {
6371 body = mputprintf(body,
6372 "TTCN_Logger::log_str(TTCN_Logger::PARALLEL_PTC, \"Starting function "
6373 "%s().\");\n", dispname_str);
6374 }
6375 body = mputstr(body,
6376 "TTCN_Runtime::function_started(function_arguments);\n");
6377 char *actual_par_list =
6378 fp_list->generate_code_actual_parlist(memptystr(), "");
6379 bool return_value_kept = false;
6380 if (asstype == A_FUNCTION_RVAL) {
6381 // the return value is kept only if the function returns a value
6382 // (rather than a template) and the return type has the "done"
6383 // extension attribute
6384 for (Type *t = return_type; ; t = t->get_type_refd()) {
6385 if (t->has_done_attribute()) {
6386 return_value_kept = true;
6387 break;
6388 } else if (!t->is_ref()) break;
6389 }
6390 }
6391 if (return_value_kept) {
6392 const string& return_type_dispname = return_type->get_typename();
6393 const char *return_type_dispname_str = return_type_dispname.c_str();
6394 body = mputprintf(body, "%s ret_val(%s(%s));\n"
6395 "TTCN_Logger::begin_event(TTCN_PARALLEL);\n"
6396 "TTCN_Logger::log_event_str(\"Function %s returned %s : \");\n"
6397 "ret_val.log();\n"
6398 "Text_Buf text_buf;\n"
6399 "TTCN_Runtime::prepare_function_finished(\"%s\", text_buf);\n"
6400 "ret_val.encode_text(text_buf);\n"
6401 "TTCN_Runtime::send_function_finished(text_buf);\n",
6402 return_type_str, genname_str, actual_par_list, dispname_str,
6403 return_type_dispname_str, return_type_dispname_str);
6404 } else {
6405 body = mputprintf(body, "%s(%s);\n"
6406 "TTCN_Runtime::function_finished(\"%s\");\n",
6407 genname_str, actual_par_list, dispname_str);
6408 }
6409 Free(actual_par_list);
6410 body = mputstr(body, "return TRUE;\n"
6411 "} else ");
6412 target->functions.start = mputstr(target->functions.start, body);
6413 Free(body);
6414 Free(full_formal_par_list);
6415 }
6416
6417 target->functions.pre_init = mputprintf(target->functions.pre_init,
6418 "%s.add_function(\"%s\", (genericfunc_t)&%s, ", get_module_object_name(),
6419 dispname_str, genname_str);
6420 if(is_startable)
6421 target->functions.pre_init = mputprintf(target->functions.pre_init,
6422 "(genericfunc_t)&start_%s);\n", genname_str);
6423 else
6424 target->functions.pre_init = mputstr(target->functions.pre_init,
6425 "NULL);\n");
6426 }
6427
6428 void Def_Function::generate_code(CodeGenHelper& cgh) {
6429 generate_code(cgh.get_current_outputstruct());
6430 }
6431
6432 void Def_Function::dump_internal(unsigned level) const
6433 {
6434 DEBUG(level, "Function: %s", id->get_dispname().c_str());
6435 DEBUG(level + 1, "Parameters:");
6436 fp_list->dump(level + 1);
6437 if (runs_on_ref) {
6438 DEBUG(level + 1, "Runs on clause:");
6439 runs_on_ref->dump(level + 2);
6440 }
6441 if (return_type) {
6442 DEBUG(level + 1, "Return type:");
6443 return_type->dump(level + 2);
6444 if (asstype == A_FUNCTION_RTEMP) DEBUG(level + 1, "Returns template");
6445 }
6446 if (prototype != PROTOTYPE_NONE)
6447 DEBUG(level + 1, "Prototype: %s", get_prototype_name());
6448 //DEBUG(level + 1, "Statement block:");
6449 block->dump(level + 1);
6450 }
6451
6452 void Def_Function::set_parent_path(WithAttribPath* p_path) {
6453 Def_Function_Base::set_parent_path(p_path);
6454 block->set_parent_path(w_attrib_path);
6455 }
6456
6457 // =================================
6458 // ===== Def_ExtFunction
6459 // =================================
6460
6461 Def_ExtFunction::~Def_ExtFunction()
6462 {
6463 delete encoding_options;
6464 delete eb_list;
6465 if (NULL != json_printing) {
6466 delete json_printing;
6467 }
6468 }
6469
6470 Def_ExtFunction *Def_ExtFunction::clone() const
6471 {
6472 FATAL_ERROR("Def_ExtFunction::clone");
6473 }
6474
6475 void Def_ExtFunction::set_fullname(const string& p_fullname)
6476 {
6477 Def_Function_Base::set_fullname(p_fullname);
6478 if (eb_list) eb_list->set_fullname(p_fullname + ".<errorbehavior_list>");
6479 }
6480
6481 void Def_ExtFunction::set_encode_parameters(Type::MessageEncodingType_t
6482 p_encoding_type, string *p_encoding_options)
6483 {
6484 function_type = EXTFUNC_ENCODE;
6485 encoding_type = p_encoding_type;
6486 delete encoding_options;
6487 encoding_options = p_encoding_options;
6488 }
6489
6490 void Def_ExtFunction::set_decode_parameters(Type::MessageEncodingType_t
6491 p_encoding_type, string *p_encoding_options)
6492 {
6493 function_type = EXTFUNC_DECODE;
6494 encoding_type = p_encoding_type;
6495 delete encoding_options;
6496 encoding_options = p_encoding_options;
6497 }
6498
6499 void Def_ExtFunction::add_eb_list(Ttcn::ErrorBehaviorList *p_eb_list)
6500 {
6501 if (!p_eb_list) FATAL_ERROR("Def_ExtFunction::add_eb_list()");
6502 if (eb_list) {
6503 eb_list->steal_ebs(p_eb_list);
6504 delete p_eb_list;
6505 } else {
6506 eb_list = p_eb_list;
6507 eb_list->set_fullname(get_fullname() + ".<errorbehavior_list>");
6508 }
6509 }
6510
6511 void Def_ExtFunction::chk_function_type()
6512 {
6513 switch (function_type) {
6514 case EXTFUNC_MANUAL:
6515 if (eb_list) {
6516 eb_list->error("Attribute `errorbehavior' can only be used together "
6517 "with `encode' or `decode'");
6518 eb_list->chk();
6519 }
6520 break;
6521 case EXTFUNC_ENCODE:
6522 switch (prototype) {
6523 case PROTOTYPE_NONE:
6524 error("Attribute `encode' cannot be used without `prototype'");
6525 break;
6526 case PROTOTYPE_BACKTRACK:
6527 case PROTOTYPE_SLIDING:
6528 error("Attribute `encode' cannot be used with `prototype(%s)'",
6529 get_prototype_name());
6530 default: /* CONVERT and FAST allowed */
6531 break;
6532 }
6533
6534 if (input_type) {
6535 if (!input_type->has_encoding(encoding_type, encoding_options)) {
6536 if (Common::Type::CT_CUSTOM == encoding_type) {
6537 input_type->error("Input type `%s' does not support custom encoding '%s'",
6538 input_type->get_typename().c_str(), encoding_options->c_str());
6539 }
6540 else {
6541 input_type->error("Input type `%s' does not support %s encoding",
6542 input_type->get_typename().c_str(),
6543 Type::get_encoding_name(encoding_type));
6544 }
6545 }
6546 else {
6547 if (Common::Type::CT_XER == encoding_type
6548 && input_type->get_type_refd_last()->is_untagged()) {
6549 // "untagged" on the (toplevel) input type will have no effect.
6550 warning("UNTAGGED encoding attribute is ignored on top-level type");
6551 }
6552 if (Common::Type::CT_CUSTOM == encoding_type) {
6553 if (PROTOTYPE_CONVERT != prototype) {
6554 error("Only `prototype(convert)' is allowed for custom encoding functions");
6555 }
6556 else {
6557 // let the input type know that this is its encoding function
6558 input_type->get_type_refd()->set_coding_function(true,
6559 get_genname_from_scope(input_type->get_type_refd()->get_my_scope()));
6560 // treat this as a manual external function during code generation
6561 function_type = EXTFUNC_MANUAL;
6562 }
6563 }
6564 }
6565 }
6566 if (output_type) {
6567 if(encoding_type == Common::Type::CT_TEXT) { // TEXT encoding supports both octetstring and charstring stream types
6568 Type *stream_type = Type::get_stream_type(encoding_type,0);
6569 Type *stream_type2 = Type::get_stream_type(encoding_type,1);
6570 if ( (!stream_type->is_identical(output_type)) && (!stream_type2->is_identical(output_type)) ) {
6571 output_type->error("The output type of %s encoding should be `%s' or `%s' "
6572 "instead of `%s'", Type::get_encoding_name(encoding_type),
6573 stream_type->get_typename().c_str(),
6574 stream_type2->get_typename().c_str(),
6575 output_type->get_typename().c_str());
6576 }
6577 } else {
6578 Type *stream_type = Type::get_stream_type(encoding_type);
6579 if (!stream_type->is_identical(output_type)) {
6580 output_type->error("The output type of %s encoding should be `%s' "
6581 "instead of `%s'", Type::get_encoding_name(encoding_type),
6582 stream_type->get_typename().c_str(),
6583 output_type->get_typename().c_str());
6584 }
6585 }
6586 }
6587 if (eb_list) eb_list->chk();
6588 chk_allowed_encode();
6589 break;
6590 case EXTFUNC_DECODE:
6591 if (prototype == PROTOTYPE_NONE) {
6592 error("Attribute `decode' cannot be used without `prototype'");
6593 }
6594 if (input_type) {
6595 if(encoding_type == Common::Type::CT_TEXT) { // TEXT encoding supports both octetstring and charstring stream types
6596 Type *stream_type = Type::get_stream_type(encoding_type,0);
6597 Type *stream_type2 = Type::get_stream_type(encoding_type,1);
6598 if ( (!stream_type->is_identical(input_type)) && (!stream_type2->is_identical(input_type)) ) {
6599 input_type->error("The input type of %s decoding should be `%s' or `%s' "
6600 "instead of `%s'", Type::get_encoding_name(encoding_type),
6601 stream_type->get_typename().c_str(),
6602 stream_type2->get_typename().c_str(),
6603 input_type->get_typename().c_str());
6604 }
6605 } else {
6606 Type *stream_type = Type::get_stream_type(encoding_type);
6607 if (!stream_type->is_identical(input_type)) {
6608 input_type->error("The input type of %s decoding should be `%s' "
6609 "instead of `%s'", Type::get_encoding_name(encoding_type),
6610 stream_type->get_typename().c_str(),
6611 input_type->get_typename().c_str());
6612 }
6613 }
6614
6615 }
6616 if (output_type && !output_type->has_encoding(encoding_type, encoding_options)) {
6617 if (Common::Type::CT_CUSTOM == encoding_type) {
6618 output_type->error("Output type `%s' does not support custom encoding '%s'",
6619 output_type->get_typename().c_str(), encoding_options->c_str());
6620 }
6621 else {
6622 output_type->error("Output type `%s' does not support %s encoding",
6623 output_type->get_typename().c_str(),
6624 Type::get_encoding_name(encoding_type));
6625 }
6626 }
6627 else {
6628 if (Common::Type::CT_CUSTOM == encoding_type) {
6629 if (PROTOTYPE_SLIDING != prototype) {
6630 error("Only `prototype(sliding)' is allowed for custom decoding functions");
6631 }
6632 else if (output_type) {
6633 // let the output type know that this is its decoding function
6634 output_type->get_type_refd()->set_coding_function(false,
6635 get_genname_from_scope(output_type->get_type_refd()->get_my_scope()));
6636 // treat this as a manual external function during code generation
6637 function_type = EXTFUNC_MANUAL;
6638 }
6639 }
6640 }
6641 if (eb_list) eb_list->chk();
6642 chk_allowed_encode();
6643 break;
6644 default:
6645 FATAL_ERROR("Def_ExtFunction::chk()");
6646 }
6647 }
6648
6649 void Def_ExtFunction::chk_allowed_encode()
6650 {
6651 switch (encoding_type) {
6652 case Type::CT_BER:
6653 if (enable_ber()) return; // ok
6654 break;
6655 case Type::CT_RAW:
6656 if (enable_raw()) return; // ok
6657 break;
6658 case Type::CT_TEXT:
6659 if (enable_text()) return; // ok
6660 break;
6661 case Type::CT_XER:
6662 if (enable_xer()) return; // ok
6663 break;
6664 case Type::CT_PER:
6665 if (enable_per()) return; // ok?
6666 break;
6667 case Type::CT_JSON:
6668 if (enable_json()) return;
6669 break;
6670 case Type::CT_CUSTOM:
6671 return; // cannot be disabled
6672 default:
6673 FATAL_ERROR("Def_ExtFunction::chk_allowed_encode");
6674 break;
6675 }
6676
6677 error("%s encoding is disallowed by license or commandline options",
6678 Type::get_encoding_name(encoding_type));
6679 }
6680
6681 void Def_ExtFunction::chk()
6682 {
6683 if (checked) return;
6684 checked = true;
6685 Error_Context cntxt(this, "In external function definition `%s'",
6686 id->get_dispname().c_str());
6687 fp_list->chk(asstype);
6688 if (return_type) {
6689 Error_Context cntxt2(return_type, "In return type");
6690 return_type->chk();
6691 return_type->chk_as_return_type(asstype == A_EXT_FUNCTION_RVAL,
6692 "external function");
6693 }
6694 if (!semantic_check_only) fp_list->set_genname(get_genname());
6695 if (w_attrib_path) {
6696 w_attrib_path->chk_global_attrib();
6697 w_attrib_path->chk_no_qualif();
6698 const Ttcn::ExtensionAttributes * extattrs = parse_extattributes(w_attrib_path);
6699 if (extattrs != 0) {
6700 size_t num_atrs = extattrs->size();
6701 for (size_t i=0; i < num_atrs; ++i) {
6702 ExtensionAttribute &ea = extattrs->get(i);
6703 switch (ea.get_type()) {
6704 case ExtensionAttribute::PROTOTYPE: {
6705 if (get_prototype() != Def_Function_Base::PROTOTYPE_NONE) {
6706 ea.error("Duplicate attribute `prototype'");
6707 }
6708 Def_Function_Base::prototype_t proto = ea.get_proto();
6709 set_prototype(proto);
6710 break; }
6711
6712 case ExtensionAttribute::ENCODE: {
6713 switch (get_function_type()) {
6714 case Def_ExtFunction::EXTFUNC_MANUAL:
6715 break;
6716 case Def_ExtFunction::EXTFUNC_ENCODE: {
6717 ea.error("Duplicate attribute `encode'");
6718 break; }
6719 case Def_ExtFunction::EXTFUNC_DECODE: {
6720 ea.error("Attributes `decode' and `encode' "
6721 "cannot be used at the same time");
6722 break; }
6723 default:
6724 FATAL_ERROR("coding_attrib_parse(): invalid external function type");
6725 }
6726 Type::MessageEncodingType_t et;
6727 string *opt;
6728 ea.get_encdec_parameters(et, opt);
6729 set_encode_parameters(et, opt);
6730 break; }
6731
6732 case ExtensionAttribute::ERRORBEHAVIOR: {
6733 add_eb_list(ea.get_eb_list());
6734 break; }
6735
6736 case ExtensionAttribute::DECODE: {
6737 switch (get_function_type()) {
6738 case Def_ExtFunction::EXTFUNC_MANUAL:
6739 break;
6740 case Def_ExtFunction::EXTFUNC_ENCODE: {
6741 ea.error("Attributes `encode' and `decode' "
6742 "cannot be used at the same time");
6743 break; }
6744 case Def_ExtFunction::EXTFUNC_DECODE: {
6745 ea.error("Duplicate attribute `decode'");
6746 break; }
6747 default:
6748 FATAL_ERROR("coding_attrib_parse(): invalid external function type");
6749 }
6750 Type::MessageEncodingType_t et;
6751 string *opt;
6752 ea.get_encdec_parameters(et, opt);
6753 set_decode_parameters(et, opt);
6754 break; }
6755
6756 case ExtensionAttribute::PRINTING: {
6757 json_printing = ea.get_printing();
6758 break; }
6759
6760 case ExtensionAttribute::ANYTYPELIST:
6761 // ignore, because we can't distinguish between a local
6762 // "extension anytype" (which is bogus) and an inherited one
6763 // (which was meant for a type definition)
6764 break;
6765
6766 case ExtensionAttribute::NONE:
6767 // Ignore, do not issue "wrong type" error
6768 break;
6769
6770 default:
6771 ea.error(
6772 "Only the following extension attributes may be applied to "
6773 "external functions: 'prototype', 'encode', 'decode', 'errorbehavior'");
6774 break;
6775 } // switch type
6776 } // next attribute
6777 delete extattrs;
6778 } // if extatrs
6779 }
6780 chk_prototype();
6781 chk_function_type();
6782
6783 if (NULL != json_printing && (EXTFUNC_ENCODE != function_type ||
6784 Type::CT_JSON != encoding_type)) {
6785 error("Attribute 'printing' is only allowed for JSON encoding functions.");
6786 }
6787 }
6788
6789 char *Def_ExtFunction::generate_code_encode(char *str)
6790 {
6791 const char *function_name = id->get_dispname().c_str();
6792 const char *first_par_name =
6793 fp_list->get_fp_byIndex(0)->get_id().get_name().c_str();
6794 // producing debug printout of the input PDU
6795 str = mputprintf(str,
6796 #ifndef NDEBUG
6797 "// written by %s in " __FILE__ " at %d\n"
6798 #endif
6799 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6800 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6801 "TTCN_Logger::log_event_str(\"%s(): Encoding %s: \");\n"
6802 "%s.log();\n"
6803 "TTCN_Logger::end_event();\n"
6804 "}\n"
6805 #ifndef NDEBUG
6806 , __FUNCTION__, __LINE__
6807 #endif
6808 , function_name, input_type->get_typename().c_str(), first_par_name);
6809 // setting error behavior
6810 if (eb_list) str = eb_list->generate_code(str);
6811 else str = mputstr(str, "TTCN_EncDec::set_error_behavior("
6812 "TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);\n");
6813 // encoding PDU into the buffer
6814 str = mputstr(str, "TTCN_Buffer ttcn_buffer;\n");
6815 str = mputprintf(str, "%s.encode(%s_descr_, ttcn_buffer, TTCN_EncDec::CT_%s",
6816 first_par_name,
6817 input_type->get_genname_typedescriptor(my_scope).c_str(),
6818 Type::get_encoding_name(encoding_type));
6819 if (encoding_type == Type::CT_JSON) {
6820 if (json_printing != NULL) {
6821 str = json_printing->generate_code(str);
6822 } else {
6823 str = mputstr(str, ", 0");
6824 }
6825 }
6826 if (encoding_options) str = mputprintf(str, ", %s",
6827 encoding_options->c_str());
6828 str = mputstr(str, ");\n");
6829 const char *result_name;
6830 switch (prototype) {
6831 case PROTOTYPE_CONVERT:
6832 result_name = "ret_val";
6833 // creating a local variable for the result stream
6834 str = mputprintf(str, "%s ret_val;\n",
6835 output_type->get_genname_value(my_scope).c_str());
6836 break;
6837 case PROTOTYPE_FAST:
6838 result_name = fp_list->get_fp_byIndex(1)->get_id().get_name().c_str();
6839 break;
6840 default:
6841 FATAL_ERROR("Def_ExtFunction::generate_code_encode()");
6842 result_name = 0;
6843 }
6844 // taking the result from the buffer and producing debug printout
6845 str = mputprintf(str, "ttcn_buffer.get_string(%s);\n"
6846 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6847 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6848 "TTCN_Logger::log_event_str(\"%s(): Stream after encoding: \");\n"
6849 "%s.log();\n"
6850 "TTCN_Logger::end_event();\n"
6851 "}\n", result_name, function_name, result_name);
6852 // returning the result stream if necessary
6853 if (prototype == PROTOTYPE_CONVERT) {
6854 if (debugger_active) {
6855 str = mputstr(str,
6856 "ttcn3_debugger.set_return_value((TTCN_Logger::begin_event_log2str(), "
6857 "ret_val.log(), TTCN_Logger::end_event_log2str()));\n");
6858 }
6859 str = mputstr(str, "return ret_val;\n");
6860 }
6861 return str;
6862 }
6863
6864 char *Def_ExtFunction::generate_code_decode(char *str)
6865 {
6866 const char *function_name = id->get_dispname().c_str();
6867 const char *first_par_name =
6868 fp_list->get_fp_byIndex(0)->get_id().get_name().c_str();
6869 // producing debug printout of the input stream
6870 str = mputprintf(str,
6871 #ifndef NDEBUG
6872 "// written by %s in " __FILE__ " at %d\n"
6873 #endif
6874 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6875 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6876 "TTCN_Logger::log_event_str(\"%s(): Stream before decoding: \");\n"
6877 "%s.log();\n"
6878 "TTCN_Logger::end_event();\n"
6879 "}\n"
6880 #ifndef NDEBUG
6881 , __FUNCTION__, __LINE__
6882 #endif
6883 , function_name, first_par_name);
6884 // setting error behavior
6885 if (eb_list) str = eb_list->generate_code(str);
6886 else if (prototype == PROTOTYPE_BACKTRACK || prototype == PROTOTYPE_SLIDING) {
6887 str = mputstr(str, "TTCN_EncDec::set_error_behavior("
6888 "TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);\n");
6889 } else str = mputstr(str, "TTCN_EncDec::set_error_behavior("
6890 "TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);\n");
6891 // creating a buffer from the input stream
6892 str = mputprintf(str, "TTCN_EncDec::clear_error();\n"
6893 "TTCN_Buffer ttcn_buffer(%s);\n", first_par_name);
6894 const char *result_name;
6895 if (prototype == PROTOTYPE_CONVERT) {
6896 // creating a local variable for the result
6897 str = mputprintf(str, "%s ret_val;\n",
6898 output_type->get_genname_value(my_scope).c_str());
6899 result_name = "ret_val";
6900 } else {
6901 result_name = fp_list->get_fp_byIndex(1)->get_id().get_name().c_str();
6902 }
6903 if(encoding_type==Type::CT_TEXT){
6904 str = mputprintf(str,
6905 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6906 " TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_LOG_MATCHING, TTCN_EncDec::EB_WARNING);\n"
6907 "}\n");
6908 }
6909 str = mputprintf(str, "%s.decode(%s_descr_, ttcn_buffer, "
6910 "TTCN_EncDec::CT_%s", result_name,
6911 output_type->get_genname_typedescriptor(my_scope).c_str(),
6912 Type::get_encoding_name(encoding_type));
6913 if (encoding_options) str = mputprintf(str, ", %s",
6914 encoding_options->c_str());
6915 str = mputstr(str, ");\n");
6916 // producing debug printout of the result PDU
6917 str = mputprintf(str,
6918 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6919 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6920 "TTCN_Logger::log_event_str(\"%s(): Decoded %s: \");\n"
6921 "%s.log();\n"
6922 "TTCN_Logger::end_event();\n"
6923 "}\n", function_name, output_type->get_typename().c_str(), result_name);
6924 if (prototype != PROTOTYPE_SLIDING) {
6925 // checking for remaining data in the buffer if decoding was successful
6926 str = mputprintf(str, "if (TTCN_EncDec::get_last_error_type() == "
6927 "TTCN_EncDec::ET_NONE) {\n"
6928 "if (ttcn_buffer.get_pos() < ttcn_buffer.get_len()-1 && "
6929 "TTCN_Logger::log_this_event(TTCN_WARNING)) {\n"
6930 "ttcn_buffer.cut();\n"
6931 "%s remaining_stream;\n"
6932 "ttcn_buffer.get_string(remaining_stream);\n"
6933 "TTCN_Logger::begin_event(TTCN_WARNING);\n"
6934 "TTCN_Logger::log_event_str(\"%s(): Warning: Data remained at the end "
6935 "of the stream after successful decoding: \");\n"
6936 "remaining_stream.log();\n"
6937 "TTCN_Logger::end_event();\n"
6938 "}\n", input_type->get_genname_value(my_scope).c_str(), function_name);
6939 // closing the block and returning the appropriate result or status code
6940 if (prototype == PROTOTYPE_BACKTRACK) {
6941 if (debugger_active) {
6942 str = mputstr(str, "ttcn3_debugger.set_return_value(\"0\");\n");
6943 }
6944 str = mputstr(str,
6945 "return 0;\n"
6946 "} else {\n");
6947 if (debugger_active) {
6948 str = mputstr(str, "ttcn3_debugger.set_return_value(\"1\");\n");
6949 }
6950 str = mputstr(str,
6951 "return 1;\n"
6952 "}\n");
6953 } else {
6954 str = mputstr(str, "}\n");
6955 if (prototype == PROTOTYPE_CONVERT) {
6956 if (debugger_active) {
6957 str = mputstr(str,
6958 "ttcn3_debugger.set_return_value((TTCN_Logger::begin_event_log2str(), "
6959 "ret_val.log(), TTCN_Logger::end_event_log2str()));\n");
6960 }
6961 str = mputstr(str, "return ret_val;\n");
6962 }
6963 }
6964 } else {
6965 // result handling and debug printout for sliding decoders
6966 str = mputprintf(str, "switch (TTCN_EncDec::get_last_error_type()) {\n"
6967 "case TTCN_EncDec::ET_NONE:\n"
6968 // TTCN_Buffer::get_string will call OCTETSTRING::clean_up()
6969 "ttcn_buffer.cut();\n"
6970 "ttcn_buffer.get_string(%s);\n"
6971 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6972 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6973 "TTCN_Logger::log_event_str(\"%s(): Stream after decoding: \");\n"
6974 "%s.log();\n"
6975 "TTCN_Logger::end_event();\n"
6976 "}\n"
6977 "%sreturn 0;\n"
6978 "case TTCN_EncDec::ET_INCOMPL_MSG:\n"
6979 "case TTCN_EncDec::ET_LEN_ERR:\n"
6980 "%sreturn 2;\n"
6981 "default:\n"
6982 "%sreturn 1;\n"
6983 "}\n", first_par_name, function_name, first_par_name,
6984 debugger_active ? "ttcn3_debugger.set_return_value(\"0\");\n" : "",
6985 debugger_active ? "ttcn3_debugger.set_return_value(\"2\");\n" : "",
6986 debugger_active ? "ttcn3_debugger.set_return_value(\"1\");\n" : "");
6987 }
6988 return str;
6989 }
6990
6991 void Def_ExtFunction::generate_code(output_struct *target, bool)
6992 {
6993 const string& t_genname = get_genname();
6994 const char *genname_str = t_genname.c_str();
6995 string return_type_name;
6996 switch (asstype) {
6997 case A_EXT_FUNCTION:
6998 return_type_name = "void";
6999 break;
7000 case A_EXT_FUNCTION_RVAL:
7001 return_type_name = return_type->get_genname_value(my_scope);
7002 break;
7003 case A_EXT_FUNCTION_RTEMP:
7004 return_type_name = return_type->get_genname_template(my_scope);
7005 break;
7006 default:
7007 FATAL_ERROR("Def_ExtFunction::generate_code()");
7008 }
7009 const char *return_type_str = return_type_name.c_str();
7010 char *formal_par_list = fp_list->generate_code(memptystr(), fp_list->get_nof_fps());
7011 fp_list->generate_code_defval(target);
7012 // function prototype
7013 target->header.function_prototypes =
7014 mputprintf(target->header.function_prototypes, "extern %s %s(%s);\n",
7015 return_type_str, genname_str, formal_par_list);
7016
7017 if (function_type != EXTFUNC_MANUAL) {
7018 // function body written by the compiler
7019 char *body = 0;
7020 #ifndef NDEBUG
7021 body = mprintf("// written by %s in " __FILE__ " at %d\n"
7022 , __FUNCTION__, __LINE__);
7023 #endif
7024 body = mputprintf(body,
7025 "%s %s(%s)\n"
7026 "{\n"
7027 , return_type_str, genname_str, formal_par_list);
7028 if (debugger_active) {
7029 body = generate_code_debugger_function_init(body, this);
7030 }
7031 switch (function_type) {
7032 case EXTFUNC_ENCODE:
7033 body = generate_code_encode(body);
7034 break;
7035 case EXTFUNC_DECODE:
7036 body = generate_code_decode(body);
7037 break;
7038 default:
7039 FATAL_ERROR("Def_ExtFunction::generate_code()");
7040 }
7041 body = mputstr(body, "}\n\n");
7042 target->source.function_bodies = mputstr(target->source.function_bodies,
7043 body);
7044 Free(body);
7045 }
7046
7047 Free(formal_par_list);
7048
7049 target->functions.pre_init = mputprintf(target->functions.pre_init,
7050 "%s.add_function(\"%s\", (genericfunc_t)&%s, NULL);\n",
7051 get_module_object_name(), id->get_dispname().c_str(), genname_str);
7052 }
7053
7054 void Def_ExtFunction::generate_code(CodeGenHelper& cgh) {
7055 generate_code(cgh.get_current_outputstruct());
7056 }
7057
7058 void Def_ExtFunction::dump_internal(unsigned level) const
7059 {
7060 DEBUG(level, "External function: %s", id->get_dispname().c_str());
7061 DEBUG(level + 1, "Parameters:");
7062 fp_list->dump(level + 2);
7063 if (return_type) {
7064 DEBUG(level + 1, "Return type:");
7065 return_type->dump(level + 2);
7066 if(asstype == A_EXT_FUNCTION_RTEMP) DEBUG(level + 1, "Returns template");
7067 }
7068 if (prototype != PROTOTYPE_NONE)
7069 DEBUG(level + 1, "Prototype: %s", get_prototype_name());
7070 if (function_type != EXTFUNC_MANUAL) {
7071 DEBUG(level + 1, "Automatically generated: %s",
7072 function_type == EXTFUNC_ENCODE ? "encoder" : "decoder");
7073 DEBUG(level + 2, "Encoding type: %s",
7074 Type::get_encoding_name(encoding_type));
7075 if (encoding_options)
7076 DEBUG(level + 2, "Encoding options: %s", encoding_options->c_str());
7077 }
7078 if (eb_list) eb_list->dump(level + 1);
7079 }
7080
7081 void Def_ExtFunction::generate_json_schema_ref(map<Type*, JSON_Tokenizer>& json_refs)
7082 {
7083 // only do anything if this is a JSON encoding or decoding function
7084 if (encoding_type == Type::CT_JSON &&
7085 (function_type == EXTFUNC_ENCODE || function_type == EXTFUNC_DECODE)) {
7086 // retrieve the encoded type
7087 Type* type = NULL;
7088 if (function_type == EXTFUNC_ENCODE) {
7089 // for encoding functions it's always the first parameter
7090 type = fp_list->get_fp_byIndex(0)->get_Type();
7091 } else {
7092 // for decoding functions it depends on the prototype
7093 switch (prototype) {
7094 case PROTOTYPE_CONVERT:
7095 type = return_type;
7096 break;
7097 case PROTOTYPE_FAST:
7098 case PROTOTYPE_BACKTRACK:
7099 case PROTOTYPE_SLIDING:
7100 type = fp_list->get_fp_byIndex(1)->get_Type();
7101 break;
7102 default:
7103 FATAL_ERROR("Def_ExtFunction::generate_json_schema_ref");
7104 }
7105 }
7106
7107 // step over the type reference created for this function
7108 type = type->get_type_refd();
7109
7110 JSON_Tokenizer* json = NULL;
7111 if (json_refs.has_key(type)) {
7112 // the schema segment containing the type's reference already exists
7113 json = json_refs[type];
7114 } else {
7115 // the schema segment doesn't exist yet, create it and insert the reference
7116 json = new JSON_Tokenizer;
7117 json_refs.add(type, json);
7118 type->generate_json_schema_ref(*json);
7119 }
7120
7121 // insert a property to specify which function this is (encoding or decoding)
7122 json->put_next_token(JSON_TOKEN_NAME, (function_type == EXTFUNC_ENCODE) ?
7123 "encoding" : "decoding");
7124
7125 // place the function's info in an object
7126 json->put_next_token(JSON_TOKEN_OBJECT_START);
7127
7128 // insert information related to the function's prototype in an array
7129 json->put_next_token(JSON_TOKEN_NAME, "prototype");
7130 json->put_next_token(JSON_TOKEN_ARRAY_START);
7131
7132 // 1st element: external function prototype name (as string)
7133 switch(prototype) {
7134 case PROTOTYPE_CONVERT:
7135 json->put_next_token(JSON_TOKEN_STRING, "\"convert\"");
7136 break;
7137 case PROTOTYPE_FAST:
7138 json->put_next_token(JSON_TOKEN_STRING, "\"fast\"");
7139 break;
7140 case PROTOTYPE_BACKTRACK:
7141 json->put_next_token(JSON_TOKEN_STRING, "\"backtrack\"");
7142 break;
7143 case PROTOTYPE_SLIDING:
7144 json->put_next_token(JSON_TOKEN_STRING, "\"sliding\"");
7145 break;
7146 default:
7147 FATAL_ERROR("Def_ExtFunction::generate_json_schema_ref");
7148 }
7149
7150 // 2nd element: external function name
7151 char* func_name_str = mprintf("\"%s\"", id->get_dispname().c_str());
7152 json->put_next_token(JSON_TOKEN_STRING, func_name_str);
7153 Free(func_name_str);
7154
7155 // the rest of the elements contain the names of the function's parameters (1 or 2)
7156 for (size_t i = 0; i < fp_list->get_nof_fps(); ++i) {
7157 char* param_str = mprintf("\"%s\"",
7158 fp_list->get_fp_byIndex(i)->get_id().get_dispname().c_str());
7159 json->put_next_token(JSON_TOKEN_STRING, param_str);
7160 Free(param_str);
7161 }
7162
7163 // end of the prototype's array
7164 json->put_next_token(JSON_TOKEN_ARRAY_END);
7165
7166 // insert error behavior data
7167 if (eb_list != NULL) {
7168 json->put_next_token(JSON_TOKEN_NAME, "errorBehavior");
7169 json->put_next_token(JSON_TOKEN_OBJECT_START);
7170
7171 // add each error behavior modification as a property
7172 for (size_t i = 0; i < eb_list->get_nof_ebs(); ++i) {
7173 ErrorBehaviorSetting* eb = eb_list->get_ebs_byIndex(i);
7174 json->put_next_token(JSON_TOKEN_NAME, eb->get_error_type().c_str());
7175 char* handling_str = mprintf("\"%s\"", eb->get_error_handling().c_str());
7176 json->put_next_token(JSON_TOKEN_STRING, handling_str);
7177 Free(handling_str);
7178 }
7179
7180 json->put_next_token(JSON_TOKEN_OBJECT_END);
7181 }
7182
7183 // insert printing type
7184 if (json_printing != NULL) {
7185 json->put_next_token(JSON_TOKEN_NAME, "printing");
7186 json->put_next_token(JSON_TOKEN_STRING,
7187 (json_printing->get_printing() == PrintingType::PT_PRETTY) ?
7188 "\"pretty\"" : "\"compact\"");
7189 }
7190
7191 // end of this function's object
7192 json->put_next_token(JSON_TOKEN_OBJECT_END);
7193 }
7194 }
7195
7196 // =================================
7197 // ===== Def_Altstep
7198 // =================================
7199
7200 Def_Altstep::Def_Altstep(Identifier *p_id, FormalParList *p_fpl,
7201 Reference *p_runs_on_ref, StatementBlock *p_sb,
7202 AltGuards *p_ags)
7203 : Definition(A_ALTSTEP, p_id), fp_list(p_fpl), runs_on_ref(p_runs_on_ref),
7204 runs_on_type(0), sb(p_sb), ags(p_ags)
7205 {
7206 if (!p_fpl || !p_sb || !p_ags)
7207 FATAL_ERROR("Def_Altstep::Def_Altstep()");
7208 fp_list->set_my_def(this);
7209 sb->set_my_def(this);
7210 ags->set_my_def(this);
7211 ags->set_my_sb(sb, 0);
7212 }
7213
7214 Def_Altstep::~Def_Altstep()
7215 {
7216 delete fp_list;
7217 delete runs_on_ref;
7218 delete sb;
7219 delete ags;
7220 }
7221
7222 Def_Altstep *Def_Altstep::clone() const
7223 {
7224 FATAL_ERROR("Def_Altstep::clone");
7225 }
7226
7227 void Def_Altstep::set_fullname(const string& p_fullname)
7228 {
7229 Definition::set_fullname(p_fullname);
7230 fp_list->set_fullname(p_fullname + ".<formal_par_list>");
7231 if (runs_on_ref) runs_on_ref->set_fullname(p_fullname + ".<runs_on_type>");
7232 sb->set_fullname(p_fullname+".<block>");
7233 ags->set_fullname(p_fullname + ".<guards>");
7234 }
7235
7236 void Def_Altstep::set_my_scope(Scope *p_scope)
7237 {
7238 bridgeScope.set_parent_scope(p_scope);
7239 bridgeScope.set_scopeMacro_name(id->get_dispname());
7240
7241 Definition::set_my_scope(&bridgeScope);
7242 // the scope of the parameter list is set during checking
7243 if (runs_on_ref) runs_on_ref->set_my_scope(&bridgeScope);
7244 sb->set_my_scope(fp_list);
7245 ags->set_my_scope(sb);
7246 }
7247
7248 Type *Def_Altstep::get_RunsOnType()
7249 {
7250 if (!checked) chk();
7251 return runs_on_type;
7252 }
7253
7254 FormalParList *Def_Altstep::get_FormalParList()
7255 {
7256 if (!checked) chk();
7257 return fp_list;
7258 }
7259
7260 RunsOnScope *Def_Altstep::get_runs_on_scope(Type *comptype)
7261 {
7262 Module *my_module = dynamic_cast<Module*>(my_scope->get_scope_mod());
7263 if (!my_module) FATAL_ERROR("Def_Altstep::get_runs_on_scope()");
7264 return my_module->get_runs_on_scope(comptype);
7265 }
7266
7267 void Def_Altstep::chk()
7268 {
7269 if (checked) return;
7270 checked = true;
7271 Error_Context cntxt(this, "In altstep definition `%s'",
7272 id->get_dispname().c_str());
7273 Scope *parlist_scope = my_scope;
7274 if (runs_on_ref) {
7275 Error_Context cntxt2(runs_on_ref, "In `runs on' clause");
7276 runs_on_type = runs_on_ref->chk_comptype_ref();
7277 if (runs_on_type) {
7278 Scope *runs_on_scope = get_runs_on_scope(runs_on_type);
7279 runs_on_scope->set_parent_scope(my_scope);
7280 parlist_scope = runs_on_scope;
7281 }
7282 }
7283 fp_list->set_my_scope(parlist_scope);
7284 fp_list->chk(asstype);
7285 sb->chk();
7286 ags->set_is_altstep();
7287 ags->set_my_ags(ags);
7288 ags->set_my_laic_stmt(ags, 0);
7289 ags->chk();
7290 if (!semantic_check_only) {
7291 fp_list->set_genname(get_genname());
7292 sb->set_code_section(GovernedSimple::CS_INLINE);
7293 ags->set_code_section(GovernedSimple::CS_INLINE);
7294 }
7295 if (w_attrib_path) {
7296 w_attrib_path->chk_global_attrib();
7297 w_attrib_path->chk_no_qualif();
7298 }
7299 }
7300
7301 void Def_Altstep::generate_code(output_struct *target, bool)
7302 {
7303 const string& t_genname = get_genname();
7304 const char *genname_str = t_genname.c_str();
7305 const char *dispname_str = id->get_dispname().c_str();
7306
7307 // function for altstep instance:
7308 // assemble the function body first (this also determines which parameters
7309 // are never used)
7310 char* body = create_location_object(memptystr(), "ALTSTEP", dispname_str);
7311 body = fp_list->generate_shadow_objects(body);
7312 if (debugger_active) {
7313 body = generate_code_debugger_function_init(body, this);
7314 }
7315 body = sb->generate_code(body);
7316 body = ags->generate_code_altstep(body);
7317 // generate a smart formal parameter list (omits unused parameter names)
7318 char *formal_par_list = fp_list->generate_code(memptystr());
7319 fp_list->generate_code_defval(target);
7320
7321 // function for altstep instance: prototype
7322 target->header.function_prototypes =
7323 mputprintf(target->header.function_prototypes,
7324 "extern alt_status %s_instance(%s);\n", genname_str, formal_par_list);
7325
7326 // function for altstep instance: body
7327 target->source.function_bodies = mputprintf(target->source.function_bodies,
7328 "alt_status %s_instance(%s)\n"
7329 "{\n"
7330 "%s"
7331 "}\n\n", genname_str, formal_par_list, body);
7332 Free(formal_par_list);
7333 Free(body);
7334
7335 char *actual_par_list =
7336 fp_list->generate_code_actual_parlist(memptystr(), "");
7337
7338 // use a full formal parameter list for the rest of the functions
7339 char *full_formal_par_list = fp_list->generate_code(memptystr(),
7340 fp_list->get_nof_fps());
7341
7342 // wrapper function for stand-alone instantiation: prototype
7343 target->header.function_prototypes =
7344 mputprintf(target->header.function_prototypes,
7345 "extern void %s(%s);\n", genname_str, full_formal_par_list);
7346
7347 // wrapper function for stand-alone instantiation: body
7348 target->source.function_bodies =
7349 mputprintf(target->source.function_bodies, "void %s(%s)\n"
7350 "{\n"
7351 "altstep_begin:\n"
7352 "boolean block_flag = FALSE;\n"
7353 "alt_status altstep_flag = ALT_UNCHECKED, "
7354 "default_flag = ALT_UNCHECKED;\n"
7355 "for ( ; ; ) {\n"
7356 "TTCN_Snapshot::take_new(block_flag);\n"
7357 "if (altstep_flag != ALT_NO) {\n"
7358 "altstep_flag = %s_instance(%s);\n"
7359 "if (altstep_flag == ALT_YES || altstep_flag == ALT_BREAK) return;\n"
7360 "else if (altstep_flag == ALT_REPEAT) goto altstep_begin;\n"
7361 "}\n"
7362 "if (default_flag != ALT_NO) {\n"
7363 "default_flag = TTCN_Default::try_altsteps();\n"
7364 "if (default_flag == ALT_YES || default_flag == ALT_BREAK) return;\n"
7365 "else if (default_flag == ALT_REPEAT) goto altstep_begin;\n"
7366 "}\n"
7367 "if (altstep_flag == ALT_NO && default_flag == ALT_NO) "
7368 "TTCN_error(\"None of the branches can be chosen in altstep %s.\");\n"
7369 "else block_flag = TRUE;\n"
7370 "}\n"
7371 "}\n\n", genname_str, full_formal_par_list, genname_str, actual_par_list,
7372 dispname_str);
7373
7374 // class for keeping the altstep in the default context
7375 // the class is for internal use, we do not need to publish it in the
7376 // header file
7377 char* str = mprintf("class %s_Default : public Default_Base {\n", genname_str);
7378 str = fp_list->generate_code_object(str, "par_");
7379 str = mputprintf(str, "public:\n"
7380 "%s_Default(%s);\n"
7381 "alt_status call_altstep();\n"
7382 "};\n\n", genname_str, full_formal_par_list);
7383 target->source.class_defs = mputstr(target->source.class_defs, str);
7384 Free(str);
7385 // member functions of the class
7386 str = mprintf("%s_Default::%s_Default(%s)\n"
7387 " : Default_Base(\"%s\")", genname_str, genname_str, full_formal_par_list,
7388 dispname_str);
7389 for (size_t i = 0; i < fp_list->get_nof_fps(); i++) {
7390 const char *fp_name_str =
7391 fp_list->get_fp_byIndex(i)->get_id().get_name().c_str();
7392 str = mputprintf(str, ", par_%s(%s)", fp_name_str, fp_name_str);
7393 }
7394 str = mputstr(str, "\n{\n}\n\n");
7395 char *actual_par_list_prefixed =
7396 fp_list->generate_code_actual_parlist(memptystr(), "par_");
7397 str = mputprintf(str, "alt_status %s_Default::call_altstep()\n"
7398 "{\n"
7399 "return %s_instance(%s);\n"
7400 "}\n\n", genname_str, genname_str, actual_par_list_prefixed);
7401 Free(actual_par_list_prefixed);
7402 target->source.methods = mputstr(target->source.methods, str);
7403 Free(str);
7404
7405 // function for default activation: prototype
7406 target->header.function_prototypes =
7407 mputprintf(target->header.function_prototypes,
7408 "extern Default_Base *activate_%s(%s);\n", genname_str,
7409 full_formal_par_list);
7410
7411 // function for default activation: body
7412 str = mprintf("Default_Base *activate_%s(%s)\n"
7413 "{\n", genname_str, full_formal_par_list);
7414 str = mputprintf(str, "return new %s_Default(%s);\n"
7415 "}\n\n", genname_str, actual_par_list);
7416 target->source.function_bodies = mputstr(target->source.function_bodies,
7417 str);
7418 Free(str);
7419
7420 Free(full_formal_par_list);
7421 Free(actual_par_list);
7422
7423 target->functions.pre_init = mputprintf(target->functions.pre_init,
7424 "%s.add_altstep(\"%s\", (genericfunc_t)&%s_instance, (genericfunc_t )&activate_%s, "
7425 "(genericfunc_t )&%s);\n", get_module_object_name(), dispname_str, genname_str,
7426 genname_str, genname_str);
7427 }
7428
7429 void Def_Altstep::generate_code(CodeGenHelper& cgh) {
7430 generate_code(cgh.get_current_outputstruct());
7431 }
7432
7433 void Def_Altstep::dump_internal(unsigned level) const
7434 {
7435 DEBUG(level, "Altstep: %s", id->get_dispname().c_str());
7436 DEBUG(level + 1, "Parameters:");
7437 fp_list->dump(level + 1);
7438 if (runs_on_ref) {
7439 DEBUG(level + 1, "Runs on clause:");
7440 runs_on_ref->dump(level + 2);
7441 }
7442 /*
7443 DEBUG(level + 1, "Local definitions:");
7444 sb->dump(level + 2);
7445 */
7446 DEBUG(level + 1, "Guards:");
7447 ags->dump(level + 2);
7448 }
7449
7450 void Def_Altstep::set_parent_path(WithAttribPath* p_path) {
7451 Definition::set_parent_path(p_path);
7452 sb->set_parent_path(w_attrib_path);
7453 }
7454
7455 // =================================
7456 // ===== Def_Testcase
7457 // =================================
7458
7459 Def_Testcase::Def_Testcase(Identifier *p_id, FormalParList *p_fpl,
7460 Reference *p_runs_on_ref, Reference *p_system_ref,
7461 StatementBlock *p_block)
7462 : Definition(A_TESTCASE, p_id), fp_list(p_fpl), runs_on_ref(p_runs_on_ref),
7463 runs_on_type(0), system_ref(p_system_ref), system_type(0), block(p_block)
7464 {
7465 if (!p_fpl || !p_runs_on_ref || !p_block)
7466 FATAL_ERROR("Def_Testcase::Def_Testcase()");
7467 fp_list->set_my_def(this);
7468 block->set_my_def(this);
7469 }
7470
7471 Def_Testcase::~Def_Testcase()
7472 {
7473 delete fp_list;
7474 delete runs_on_ref;
7475 delete system_ref;
7476 delete block;
7477 }
7478
7479 Def_Testcase *Def_Testcase::clone() const
7480 {
7481 FATAL_ERROR("Def_Testcase::clone");
7482 }
7483
7484 void Def_Testcase::set_fullname(const string& p_fullname)
7485 {
7486 Definition::set_fullname(p_fullname);
7487 fp_list->set_fullname(p_fullname + ".<formal_par_list>");
7488 runs_on_ref->set_fullname(p_fullname + ".<runs_on_type>");
7489 if (system_ref) system_ref->set_fullname(p_fullname + ".<system_type>");
7490 block->set_fullname(p_fullname + ".<statement_block>");
7491 }
7492
7493 void Def_Testcase::set_my_scope(Scope *p_scope)
7494 {
7495 bridgeScope.set_parent_scope(p_scope);
7496 bridgeScope.set_scopeMacro_name(id->get_dispname());
7497
7498 Definition::set_my_scope(&bridgeScope);
7499 // the scope of the parameter list is set during checking
7500 runs_on_ref->set_my_scope(&bridgeScope);
7501 if (system_ref) system_ref->set_my_scope(&bridgeScope);
7502 block->set_my_scope(fp_list);
7503 }
7504
7505 Type *Def_Testcase::get_RunsOnType()
7506 {
7507 if (!checked) chk();
7508 return runs_on_type;
7509 }
7510
7511 Type *Def_Testcase::get_SystemType()
7512 {
7513 if (!checked) chk();
7514 return system_type;
7515 }
7516
7517 FormalParList *Def_Testcase::get_FormalParList()
7518 {
7519 if (!checked) chk();
7520 return fp_list;
7521 }
7522
7523 RunsOnScope *Def_Testcase::get_runs_on_scope(Type *comptype)
7524 {
7525 Module *my_module = dynamic_cast<Module*>(my_scope->get_scope_mod());
7526 if (!my_module) FATAL_ERROR("Def_Testcase::get_runs_on_scope()");
7527 return my_module->get_runs_on_scope(comptype);
7528 }
7529
7530 void Def_Testcase::chk()
7531 {
7532 if (checked) return;
7533 checked = true;
7534 Error_Context cntxt(this, "In testcase definition `%s'",
7535 id->get_dispname().c_str());
7536 Scope *parlist_scope = my_scope;
7537 {
7538 Error_Context cntxt2(runs_on_ref, "In `runs on' clause");
7539 runs_on_type = runs_on_ref->chk_comptype_ref();
7540 if (runs_on_type) {
7541 Scope *runs_on_scope = get_runs_on_scope(runs_on_type);
7542 runs_on_scope->set_parent_scope(my_scope);
7543 parlist_scope = runs_on_scope;
7544 }
7545 }
7546 if (system_ref) {
7547 Error_Context cntxt2(system_ref, "In `system' clause");
7548 system_type = system_ref->chk_comptype_ref();;
7549 }
7550 fp_list->set_my_scope(parlist_scope);
7551 fp_list->chk(asstype);
7552 block->chk();
7553 if (!semantic_check_only) {
7554 fp_list->set_genname(get_genname());
7555 block->set_code_section(GovernedSimple::CS_INLINE);
7556 }
7557 if (w_attrib_path) {
7558 w_attrib_path->chk_global_attrib();
7559 w_attrib_path->chk_no_qualif();
7560 }
7561 }
7562
7563 void Def_Testcase::generate_code(output_struct *target, bool)
7564 {
7565 const string& t_genname = get_genname();
7566 const char *genname_str = t_genname.c_str();
7567 const char *dispname_str = id->get_dispname().c_str();
7568
7569 // assemble the function body first (this also determines which parameters
7570 // are never used)
7571
7572 // Checking whether the testcase was invoked from another one.
7573 // At this point the location information should refer to the execute()
7574 // statement rather than this testcase.
7575 char* body = mputstr(memptystr(), "TTCN_Runtime::check_begin_testcase(has_timer, "
7576 "timer_value);\n");
7577 body = create_location_object(body, "TESTCASE", dispname_str);
7578 body = fp_list->generate_shadow_objects(body);
7579 if (debugger_active) {
7580 body = generate_code_debugger_function_init(body, this);
7581 }
7582 body = mputprintf(body, "try {\n"
7583 "TTCN_Runtime::begin_testcase(\"%s\", \"%s\", ",
7584 my_scope->get_scope_mod()->get_modid().get_dispname().c_str(),
7585 dispname_str);
7586 ComponentTypeBody *runs_on_body = runs_on_type->get_CompBody();
7587 body = runs_on_body->generate_code_comptype_name(body);
7588 body = mputstr(body, ", ");
7589 if (system_type)
7590 body = system_type->get_CompBody()->generate_code_comptype_name(body);
7591 else body = runs_on_body->generate_code_comptype_name(body);
7592 body = mputstr(body, ", has_timer, timer_value);\n");
7593 body = block->generate_code(body);
7594 body = mputprintf(body,
7595 "} catch (const TC_Error& tc_error) {\n"
7596 "} catch (const TC_End& tc_end) {\n"
7597 "TTCN_Logger::log_str(TTCN_FUNCTION, \"Test case %s was stopped.\");\n"
7598 "}\n", dispname_str);
7599 body = mputstr(body, "return TTCN_Runtime::end_testcase();\n");
7600
7601 // smart formal parameter list (names of unused parameters are omitted)
7602 char *formal_par_list = fp_list->generate_code(memptystr());
7603 fp_list->generate_code_defval(target);
7604 if (fp_list->get_nof_fps() > 0)
7605 formal_par_list = mputstr(formal_par_list, ", ");
7606 formal_par_list = mputstr(formal_par_list,
7607 "boolean has_timer, double timer_value");
7608
7609 // function prototype
7610 target->header.function_prototypes =
7611 mputprintf(target->header.function_prototypes,
7612 "extern verdicttype testcase_%s(%s);\n", genname_str, formal_par_list);
7613
7614 // function body
7615 target->source.function_bodies = mputprintf(target->source.function_bodies,
7616 "verdicttype testcase_%s(%s)\n"
7617 "{\n"
7618 "%s"
7619 "}\n\n", genname_str, formal_par_list, body);
7620 Free(formal_par_list);
7621 Free(body);
7622
7623 if (fp_list->get_nof_fps() == 0) {
7624 // adding to the list of startable testcases
7625 target->functions.pre_init = mputprintf(target->functions.pre_init,
7626 "%s.add_testcase_nonpard(\"%s\", testcase_%s);\n",
7627 get_module_object_name(), dispname_str, genname_str);
7628 } else {
7629 target->functions.pre_init = mputprintf(target->functions.pre_init,
7630 "%s.add_testcase_pard(\"%s\", (genericfunc_t)&testcase_%s);\n",
7631 get_module_object_name(), dispname_str, genname_str);
7632
7633 // If every formal parameter has a default value, the testcase
7634 // might be callable after all.
7635 bool callable = true;
7636 for (size_t i = 0; i < fp_list->get_nof_fps(); ++i) {
7637 FormalPar *fp = fp_list->get_fp_byIndex(i);
7638 if (!fp->has_defval()) {
7639 callable = false;
7640 break;
7641 }
7642 }
7643
7644 if (callable) {
7645 // Write a wrapper, which acts as a no-param testcase
7646 // by calling the parameterized testcase with the default values.
7647 target->header.function_prototypes =
7648 mputprintf(target->header.function_prototypes,
7649 "extern verdicttype testcase_%s_defparams(boolean has_timer, double timer_value);\n",
7650 genname_str);
7651 target->source.function_bodies = mputprintf(target->source.function_bodies,
7652 "verdicttype testcase_%s_defparams(boolean has_timer, double timer_value) {\n"
7653 " return testcase_%s(",
7654 genname_str, genname_str);
7655
7656 for (size_t i = 0; i < fp_list->get_nof_fps(); ++i) {
7657 FormalPar *fp = fp_list->get_fp_byIndex(i);
7658 ActualPar *ap = fp->get_defval();
7659 switch (ap->get_selection()) {
7660 case ActualPar::AP_VALUE:
7661 target->source.function_bodies = mputstr(target->source.function_bodies,
7662 ap->get_Value()->get_genname_own(my_scope).c_str());
7663 break;
7664 case ActualPar::AP_TEMPLATE:
7665 target->source.function_bodies = mputstr(target->source.function_bodies,
7666 ap->get_TemplateInstance()->get_Template()->get_genname_own(my_scope).c_str());
7667 break;
7668 case ActualPar::AP_REF:
7669 target->source.function_bodies = mputstr(target->source.function_bodies,
7670 ap->get_Ref()->get_refd_assignment()->get_genname_from_scope(my_scope).c_str());
7671 break;
7672 case ActualPar::AP_DEFAULT:
7673 // Can't happen. This ActualPar was created by
7674 // Ttcn::FormalPar::chk_actual_par as the default value for
7675 // a FormalPar, and it only ever creates vale, template or ref.
7676 // no break
7677 default:
7678 FATAL_ERROR("Def_Testcase::generate_code()");
7679 }
7680
7681 // always append a comma, because has_timer and timer_value follows
7682 target->source.function_bodies = mputstrn(target->source.function_bodies,
7683 ", ", 2);
7684 }
7685
7686 target->source.function_bodies = mputstr(target->source.function_bodies,
7687 "has_timer, timer_value);\n"
7688 "}\n\n");
7689 // Add the non-parameterized wrapper *after* the parameterized one,
7690 // with the same name. Linear search will always find the first
7691 // (user-visible, parameterized) testcase.
7692 // TTCN_Module::execute_testcase knows that if after a parameterized
7693 // testcase another testcase with the same name follows,
7694 // it's the callable, non-parameterized wrapper.
7695 //
7696 // TTCN_Module::list_testcases skips parameterized testcases;
7697 // it will now list the non-parameterized wrapper.
7698 target->functions.pre_init = mputprintf(target->functions.pre_init,
7699 "%s.add_testcase_nonpard(\"%s\", testcase_%s_defparams);\n",
7700 get_module_object_name(), dispname_str, genname_str);
7701 }
7702 } // has formal parameters
7703 }
7704
7705 void Def_Testcase::generate_code(CodeGenHelper& cgh) {
7706 generate_code(cgh.get_current_outputstruct());
7707 }
7708
7709 void Def_Testcase::dump_internal(unsigned level) const
7710 {
7711 DEBUG(level, "Testcase: %s", id->get_dispname().c_str());
7712 DEBUG(level + 1, "Parameters:");
7713 fp_list->dump(level + 1);
7714 DEBUG(level + 1, "Runs on clause:");
7715 runs_on_ref->dump(level + 2);
7716 if (system_ref) {
7717 DEBUG(level + 1, "System clause:");
7718 system_ref->dump(level + 2);
7719 }
7720 DEBUG(level + 1, "Statement block:");
7721 block->dump(level + 2);
7722 }
7723
7724 void Def_Testcase::set_parent_path(WithAttribPath* p_path) {
7725 Definition::set_parent_path(p_path);
7726 if (block)
7727 block->set_parent_path(w_attrib_path);
7728 }
7729
7730 // =================================
7731 // ===== FormalPar
7732 // =================================
7733
7734 FormalPar::FormalPar(asstype_t p_asstype, Type *p_type, Identifier* p_name,
7735 TemplateInstance *p_defval, bool p_lazy_eval)
7736 : Definition(p_asstype, p_name), type(p_type), my_parlist(0),
7737 used_as_lvalue(false), template_restriction(TR_NONE),
7738 lazy_eval(p_lazy_eval), defval_generated(false), usage_found(false)
7739 {
7740 switch (p_asstype) {
7741 case A_PAR_VAL:
7742 case A_PAR_VAL_IN:
7743 case A_PAR_VAL_OUT:
7744 case A_PAR_VAL_INOUT:
7745 case A_PAR_TEMPL_IN:
7746 case A_PAR_TEMPL_OUT:
7747 case A_PAR_TEMPL_INOUT:
7748 case A_PAR_PORT:
7749 break;
7750 default:
7751 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): invalid parameter type");
7752 }
7753 if (!p_type)
7754 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): NULL pointer");
7755 type->set_ownertype(Type::OT_FORMAL_PAR, this);
7756 defval.ti = p_defval;
7757 }
7758
7759 FormalPar::FormalPar(asstype_t p_asstype,
7760 template_restriction_t p_template_restriction, Type *p_type,
7761 Identifier* p_name, TemplateInstance *p_defval, bool p_lazy_eval)
7762 : Definition(p_asstype, p_name), type(p_type), my_parlist(0),
7763 used_as_lvalue(false), template_restriction(p_template_restriction),
7764 lazy_eval(p_lazy_eval), defval_generated(false), usage_found(false)
7765 {
7766 switch (p_asstype) {
7767 case A_PAR_TEMPL_IN:
7768 case A_PAR_TEMPL_OUT:
7769 case A_PAR_TEMPL_INOUT:
7770 break;
7771 default:
7772 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): parameter not template");
7773 }
7774 if (!p_type)
7775 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): NULL pointer");
7776 type->set_ownertype(Type::OT_FORMAL_PAR, this);
7777 defval.ti = p_defval;
7778 }
7779
7780 FormalPar::FormalPar(asstype_t p_asstype, Identifier* p_name,
7781 TemplateInstance *p_defval)
7782 : Definition(p_asstype, p_name), type(0), my_parlist(0),
7783 used_as_lvalue(false), template_restriction(TR_NONE), lazy_eval(false),
7784 defval_generated(false), usage_found(false)
7785 {
7786 if (p_asstype != A_PAR_TIMER)
7787 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): invalid parameter type");
7788 defval.ti = p_defval;
7789 }
7790
7791 FormalPar::~FormalPar()
7792 {
7793 delete type;
7794 if (checked) delete defval.ap;
7795 else delete defval.ti;
7796 }
7797
7798 FormalPar* FormalPar::clone() const
7799 {
7800 FATAL_ERROR("FormalPar::clone");
7801 }
7802
7803 void FormalPar::set_fullname(const string& p_fullname)
7804 {
7805 Definition::set_fullname(p_fullname);
7806 if (type) type->set_fullname(p_fullname + ".<type>");
7807 if (checked) {
7808 if (defval.ap) defval.ap->set_fullname(p_fullname + ".<default_value>");
7809 } else {
7810 if (defval.ti) defval.ti->set_fullname(p_fullname + ".<default_value>");
7811 }
7812 }
7813
7814 void FormalPar::set_my_scope(Scope *p_scope)
7815 {
7816 Definition::set_my_scope(p_scope);
7817 if (type) type->set_my_scope(p_scope);
7818 if (checked) {
7819 if (defval.ap) defval.ap->set_my_scope(p_scope);
7820 } else {
7821 if (defval.ti) defval.ti->set_my_scope(p_scope);
7822 }
7823 }
7824
7825 bool FormalPar::is_local() const
7826 {
7827 return true;
7828 }
7829
7830 Type *FormalPar::get_Type()
7831 {
7832 if (!checked) chk();
7833 if (!type) FATAL_ERROR("FormalPar::get_Type()");
7834 return type;
7835 }
7836
7837 void FormalPar::chk()
7838 {
7839 if (checked) return;
7840 checked = true;
7841 TemplateInstance *default_value = defval.ti;
7842 defval.ti = 0;
7843 if (type) {
7844 type->chk();
7845 Type *t = type->get_type_refd_last();
7846 // checks for forbidden type <-> parameter combinations
7847 switch (t->get_typetype()) {
7848 case Type::T_PORT:
7849 switch (asstype) {
7850 case A_PAR_VAL:
7851 case A_PAR_VAL_INOUT:
7852 asstype = A_PAR_PORT;
7853 break;
7854 default:
7855 error("Port type `%s' cannot be used as %s",
7856 t->get_fullname().c_str(), get_assname());
7857 }
7858 break;
7859 case Type::T_SIGNATURE:
7860 switch (asstype) {
7861 case A_PAR_TEMPL_IN:
7862 case A_PAR_TEMPL_OUT:
7863 case A_PAR_TEMPL_INOUT:
7864 break;
7865 default:
7866 error("Signature `%s' cannot be used as %s",
7867 t->get_fullname().c_str(), get_assname());
7868 }
7869 break;
7870 default:
7871 switch (asstype) {
7872 case A_PAR_PORT:
7873 case A_PAR_TIMER:
7874 FATAL_ERROR("FormalPar::chk()");
7875 case A_PAR_VAL:
7876 asstype = A_PAR_VAL_IN;
7877 default:
7878 break;
7879 }
7880 }
7881 } else if (asstype != A_PAR_TIMER) FATAL_ERROR("FormalPar::chk()");
7882
7883 if (default_value) {
7884 Error_Context cntxt(default_value, "In default value");
7885 defval.ap = chk_actual_par(default_value, Type::EXPECTED_STATIC_VALUE);
7886 delete default_value;
7887 if (!semantic_check_only)
7888 defval.ap->set_code_section(GovernedSimple::CS_POST_INIT);
7889 }
7890 }
7891
7892 bool FormalPar::has_defval() const
7893 {
7894 if (checked) return defval.ap != 0;
7895 else return defval.ti != 0;
7896 }
7897
7898 bool FormalPar::has_notused_defval() const
7899 {
7900 if (checked) FATAL_ERROR("FormalPar::has_notused_defval");
7901 if (!defval.ti || !defval.ti->get_Template())
7902 return false;
7903 return defval.ti->get_Template()->get_templatetype()
7904 == Template::TEMPLATE_NOTUSED;
7905 }
7906
7907 ActualPar *FormalPar::get_defval() const
7908 {
7909 if (!checked) FATAL_ERROR("FormalPar::get_defval()");
7910 return defval.ap;
7911 }
7912
7913 // Extract the TemplateInstance from an ActualPar.
7914 void FormalPar::set_defval(ActualPar *defpar)
7915 {
7916 // ActualPar::clone() is not implemented, since we need such a function
7917 // only here only for AP_{VALUE,TEMPLATE} parameters. AP_ERROR can also
7918 // happen for Def_Template nodes, but they will be errors later.
7919 // FIXME: This function is Def_Template specific.
7920 if (!defval.ti->get_Template() || defval.ti->get_Template()
7921 ->get_templatetype() != Template::TEMPLATE_NOTUSED)
7922 FATAL_ERROR("FormalPar::set_defval()");
7923 TemplateInstance *reversed_ti = 0;
7924 switch (defpar->get_selection()) {
7925 case ActualPar::AP_VALUE:
7926 reversed_ti = new TemplateInstance(type->clone(), 0, new Template
7927 (defpar->get_Value()->clone())); // Trust the clone().
7928 break;
7929 case ActualPar::AP_TEMPLATE:
7930 reversed_ti = defpar->get_TemplateInstance()->clone();
7931 break;
7932 case ActualPar::AP_ERROR:
7933 break; // Can happen, but let it go.
7934 case ActualPar::AP_REF:
7935 case ActualPar::AP_DEFAULT:
7936 default:
7937 FATAL_ERROR("FormalPar::set_defval()");
7938 }
7939 if (reversed_ti) {
7940 delete defval.ti;
7941 reversed_ti->set_my_scope(get_my_scope());
7942 defval.ti = reversed_ti;
7943 }
7944 }
7945
7946 ActualPar *FormalPar::chk_actual_par(TemplateInstance *actual_par,
7947 Type::expected_value_t exp_val)
7948 {
7949 if (!checked) chk();
7950 switch (asstype) {
7951 case A_PAR_VAL:
7952 case A_PAR_VAL_IN:
7953 return chk_actual_par_value(actual_par, exp_val);
7954 case A_PAR_VAL_OUT:
7955 case A_PAR_VAL_INOUT:
7956 return chk_actual_par_by_ref(actual_par, false, exp_val);
7957 case A_PAR_TEMPL_IN:
7958 return chk_actual_par_template(actual_par, exp_val);
7959 case A_PAR_TEMPL_OUT:
7960 case A_PAR_TEMPL_INOUT:
7961 return chk_actual_par_by_ref(actual_par, true, exp_val);
7962 case A_PAR_TIMER:
7963 return chk_actual_par_timer(actual_par, exp_val);
7964 case A_PAR_PORT:
7965 return chk_actual_par_port(actual_par, exp_val);
7966 default:
7967 FATAL_ERROR("FormalPar::chk_actual_par()");
7968 }
7969 return 0; // to avoid warnings
7970 }
7971
7972 ActualPar *FormalPar::chk_actual_par_value(TemplateInstance *actual_par,
7973 Type::expected_value_t exp_val)
7974 {
7975 actual_par->chk_Type(type);
7976 Ref_base *derived_ref = actual_par->get_DerivedRef();
7977 if (derived_ref) {
7978 derived_ref->error("An in-line modified template cannot be used as %s",
7979 get_assname());
7980 actual_par->chk_DerivedRef(type);
7981 }
7982 Template *ap_template = actual_par->get_Template();
7983 if (ap_template->is_Value()) {
7984 Value *v = ap_template->get_Value();
7985 v->set_my_governor(type);
7986 type->chk_this_value_ref(v);
7987 type->chk_this_value(v, 0, exp_val, INCOMPLETE_NOT_ALLOWED,
7988 OMIT_NOT_ALLOWED, SUB_CHK);
7989 return new ActualPar(v);
7990 } else {
7991 actual_par->error("A specific value without matching symbols "
7992 "was expected for a %s", get_assname());
7993 return new ActualPar();
7994 }
7995 }
7996
7997 static void chk_defpar_value(const Value* v)
7998 {
7999 Common::Reference *vref = v->get_reference();
8000 Common::Assignment *ass2 = vref->get_refd_assignment();
8001 ass2->chk();
8002 Scope *scope = ass2->get_my_scope();
8003 ComponentTypeBody *ctb = dynamic_cast<ComponentTypeBody *>(scope);
8004 if (ctb) { // this is a component variable
8005 v->error("default value cannot refer to"
8006 " a template field of the component in the `runs on' clause");
8007 }
8008 }
8009
8010 static void chk_defpar_template(const Template *body,
8011 Type::expected_value_t exp_val)
8012 {
8013 switch (body->get_templatetype()) {
8014 case Template::TEMPLATE_ERROR:
8015 break; // could be erroneous in the source; skip it
8016 case Template::TEMPLATE_NOTUSED:
8017 case Template::OMIT_VALUE:
8018 case Template::ANY_VALUE:
8019 case Template::ANY_OR_OMIT:
8020 break; // acceptable (?)
8021 case Template::TEMPLATE_INVOKE: // calling a function is not acceptable
8022 body->error("default value can not be a function invocation");
8023 break;
8024 case Template::VALUE_RANGE: {
8025 ValueRange *range = body->get_value_range();
8026 Value *low = range->get_min_v();
8027 Type::typetype_t tt_low = low->get_expr_returntype(exp_val);
8028 Value *high = range->get_max_v();
8029 Type::typetype_t tt_high = high->get_expr_returntype(exp_val);
8030 if (tt_low == tt_high) break;
8031 break; }
8032
8033 case Template::BSTR_PATTERN:
8034 case Template::HSTR_PATTERN:
8035 case Template::OSTR_PATTERN:
8036 case Template::CSTR_PATTERN:
8037 case Template::USTR_PATTERN:
8038 break; // should be acceptable in all cases (if only fixed strings possible)
8039
8040 case Template::SPECIFIC_VALUE: {
8041 Common::Value *v = body->get_specific_value();
8042 if (v->get_valuetype() == Value::V_REFD) chk_defpar_value(v);
8043 break; }
8044
8045 case Template::ALL_FROM:
8046 case Template::VALUE_LIST_ALL_FROM:
8047 FATAL_ERROR("should have been flattened");
8048 break;
8049 case Template::SUPERSET_MATCH:
8050 case Template::SUBSET_MATCH:
8051 case Template::PERMUTATION_MATCH:
8052 case Template::TEMPLATE_LIST:
8053 case Template::COMPLEMENTED_LIST:
8054 case Template::VALUE_LIST: {
8055 // in template charstring par := charstring : ("foo", "bar", "baz")
8056 size_t num = body->get_nof_comps();
8057 for (size_t i = 0; i < num; ++i) {
8058 const Template *tpl = body->get_temp_byIndex(i);
8059 chk_defpar_template(tpl, exp_val);
8060 }
8061 break; }
8062
8063 case Template::NAMED_TEMPLATE_LIST: {
8064 size_t num = body->get_nof_comps();
8065 for (size_t i = 0; i < num; ++i) {
8066 const NamedTemplate *nt = body->get_namedtemp_byIndex(i);
8067 const Template *tpl = nt->get_template();
8068 chk_defpar_template(tpl, exp_val);
8069 }
8070 break; }
8071
8072 case Template::INDEXED_TEMPLATE_LIST: {
8073 size_t num = body->get_nof_comps();
8074 for (size_t i = 0; i < num; ++i) {
8075 const IndexedTemplate *it = body->get_indexedtemp_byIndex(i);
8076 const Template *tpl = it->get_template();
8077 chk_defpar_template(tpl, exp_val);
8078 }
8079 break; }
8080
8081 case Template::TEMPLATE_REFD: {
8082 Ref_base *ref = body->get_reference();
8083
8084 Ttcn::ActualParList *aplist = ref->get_parlist();
8085 if (!aplist) break;
8086 size_t num = aplist->get_nof_pars();
8087 for (size_t i = 0; i < num; ++i) {
8088 const Ttcn::ActualPar *ap = aplist->get_par(i);
8089 deeper:
8090 switch (ap->get_selection()) {
8091 case ActualPar::AP_ERROR: {
8092 break; }
8093 case ActualPar::AP_VALUE: {
8094 Value *v = ap->get_Value(); // "v_variable" as the parameter of the template
8095 v->chk();
8096 switch (v->get_valuetype()) {
8097 case Value::V_REFD: {
8098 chk_defpar_value(v);
8099 break; }
8100 default:
8101 break;
8102 }
8103 break; }
8104 case ActualPar::AP_TEMPLATE: {
8105 // A component cannot contain a template definition, parameterized or not.
8106 // Therefore the template this actual par references, cannot be
8107 // a field of a component => no error possible, nothing to do.
8108 break; }
8109 case ActualPar::AP_REF: {
8110 // A template cannot have an out/inout parameter
8111 FATAL_ERROR("Template with out parameter?");
8112 break; }
8113 case ActualPar::AP_DEFAULT: {
8114 ap = ap->get_ActualPar();
8115 goto deeper;
8116 break; }
8117 // no default
8118 } // switch actual par selection
8119 } // next
8120
8121 break; }
8122 } // switch templatetype
8123
8124 }
8125
8126 // This function is called in two situations:
8127 // 1. FormalParList::chk calls FormalPar::chk to compute the default value
8128 // (make an ActualPar from a TemplateInstance).
8129 // In this case, defval.ti==0, and actual_par contains its old value.
8130 // This case is called only if the formal par has a default value.
8131 // 2. FormalParList::chk_actual_parlist calls FormalPar::chk_actual_par
8132 // to check the parameters supplied by the execute statement to the tc.
8133 // In this case, defval.ap has the value computed in case 1.
8134 ActualPar *FormalPar::chk_actual_par_template(TemplateInstance *actual_par,
8135 Type::expected_value_t exp_val)
8136 {
8137 actual_par->chk(type);
8138 // actual_par->template_body may change: SPECIFIC_VALUE to TEMPLATE_REFD
8139 Definition *fplist_def = my_parlist->get_my_def();
8140 // The parameter list belongs to this definition. If it's a function
8141 // or testcase, it may have a "runs on" clause.
8142 Def_Function *parent_fn = dynamic_cast<Def_Function *>(fplist_def);
8143 Type *runs_on_type = 0;
8144 if (parent_fn) runs_on_type = parent_fn->get_RunsOnType();
8145 else { // not a function; maybe a testcase
8146 Def_Testcase *parent_tc = dynamic_cast<Def_Testcase *>(fplist_def);
8147 if (parent_tc) runs_on_type = parent_tc->get_RunsOnType();
8148 }
8149 if (runs_on_type) {
8150 // If it _has_ a runs on clause, the type must be a component.
8151 if (runs_on_type->get_typetype() != Type::T_COMPONENT) FATAL_ERROR("not component?");
8152 // The default value "shall not refer to elements of the component type
8153 // in the runs on clause"
8154 ComponentTypeBody *runs_on_component = runs_on_type->get_CompBody();
8155 size_t compass = runs_on_component->get_nof_asss();
8156 for (size_t c = 0; c < compass; c++) {
8157 Assignment *ass = runs_on_component->get_ass_byIndex(c);
8158 (void)ass;
8159 }
8160 }
8161
8162 Ttcn::Template * body = actual_par->get_Template();
8163 if (exp_val == Type::EXPECTED_STATIC_VALUE
8164 ||exp_val == Type::EXPECTED_CONSTANT) {
8165 chk_defpar_template(body, exp_val);
8166 }
8167 // Rip out the type, derived ref and template from actual_par
8168 // (which may come from a function invocation or the definition
8169 // of the default value) and give it to the new ActualPar.
8170 ActualPar *ret_val = new ActualPar(
8171 new TemplateInstance(actual_par->get_Type(),
8172 actual_par->get_DerivedRef(), actual_par->get_Template()));
8173 // Zero out these members because the caller will soon call delete
8174 // on actual_par, but they now belong to ret_val.
8175 // FIXME: should this really be in here, or outside in the caller before the delete ?
8176 actual_par->release();
8177
8178 if (template_restriction!=TR_NONE) {
8179 bool needs_runtime_check =
8180 ret_val->get_TemplateInstance()->chk_restriction(
8181 "template formal parameter", template_restriction,
8182 ret_val->get_TemplateInstance());
8183 if (needs_runtime_check)
8184 ret_val->set_gen_restriction_check(template_restriction);
8185 }
8186 return ret_val;
8187 }
8188
8189 ActualPar *FormalPar::chk_actual_par_by_ref(TemplateInstance *actual_par,
8190 bool is_template, Type::expected_value_t exp_val)
8191 {
8192 Type *ap_type = actual_par->get_Type();
8193 if (ap_type) {
8194 ap_type->warning("Explicit type specification is useless for an %s",
8195 get_assname());
8196 actual_par->chk_Type(type);
8197 }
8198 Ref_base *derived_ref = actual_par->get_DerivedRef();
8199 if (derived_ref) {
8200 derived_ref->error("An in-line modified template cannot be used as %s",
8201 get_assname());
8202 actual_par->chk_DerivedRef(type);
8203 }
8204 // needed for the error messages
8205 const char *expected_string = is_template ?
8206 "template variable or template parameter" :
8207 "variable or value parameter";
8208 Template *ap_template = actual_par->get_Template();
8209 if (ap_template->is_Ref()) {
8210 Ref_base *ref = ap_template->get_Ref();
8211 Common::Assignment *ass = ref->get_refd_assignment();
8212 if (!ass) {
8213 delete ref;
8214 return new ActualPar();
8215 }
8216 bool asstype_correct = false;
8217 switch (ass->get_asstype()) {
8218 case A_PAR_VAL_IN:
8219 ass->use_as_lvalue(*ref);
8220 if (get_asstype() == A_PAR_VAL_OUT || get_asstype() == A_PAR_TEMPL_OUT) {
8221 ass->warning("Passing an `in' parameter as another function's `out' parameter");
8222 }
8223 // no break
8224 case A_VAR:
8225 case A_PAR_VAL_OUT:
8226 case A_PAR_VAL_INOUT:
8227 if (!is_template) asstype_correct = true;
8228 break;
8229 case A_PAR_TEMPL_IN:
8230 ass->use_as_lvalue(*ref);
8231 if (get_asstype() == A_PAR_VAL_OUT || get_asstype() == A_PAR_TEMPL_OUT) {
8232 ass->warning("Passing an `in' parameter as another function's `out' parameter");
8233 }
8234 // no break
8235 case A_VAR_TEMPLATE:
8236 case A_PAR_TEMPL_OUT:
8237 case A_PAR_TEMPL_INOUT:
8238 if (is_template) asstype_correct = true;
8239 break;
8240 default:
8241 break;
8242 }
8243 if (asstype_correct) {
8244 FieldOrArrayRefs *t_subrefs = ref->get_subrefs();
8245 Type *ref_type = ass->get_Type()->get_field_type(t_subrefs, exp_val);
8246 if (ref_type) {
8247 if (!type->is_identical(ref_type)) {
8248 ref->error("Type mismatch: Reference to a %s of type "
8249 "`%s' was expected instead of `%s'", expected_string,
8250 type->get_typename().c_str(), ref_type->get_typename().c_str());
8251 } else if (type->get_sub_type() && ref_type->get_sub_type() &&
8252 (type->get_sub_type()->get_subtypetype()==ref_type->get_sub_type()->get_subtypetype()) &&
8253 (!type->get_sub_type()->is_compatible(ref_type->get_sub_type()))) {
8254 ref->error("Subtype mismatch: subtype %s has no common value with subtype %s",
8255 type->get_sub_type()->to_string().c_str(),
8256 ref_type->get_sub_type()->to_string().c_str());
8257 }
8258 if (t_subrefs && t_subrefs->refers_to_string_element()) {
8259 ref->error("Reference to a string element of type `%s' cannot be "
8260 "used in this context", ref_type->get_typename().c_str());
8261 }
8262 }
8263 } else {
8264 ref->error("Reference to a %s was expected for an %s instead of %s",
8265 expected_string, get_assname(), ass->get_description().c_str());
8266 }
8267 ActualPar* ret_val_ap = new ActualPar(ref);
8268 // restriction checking if this is a reference to a template variable
8269 // this is an 'out' or 'inout' template parameter
8270 if (is_template && asstype_correct) {
8271 template_restriction_t refd_tr;
8272 switch (ass->get_asstype()) {
8273 case A_VAR_TEMPLATE: {
8274 Def_Var_Template* dvt = dynamic_cast<Def_Var_Template*>(ass);
8275 if (!dvt) FATAL_ERROR("FormalPar::chk_actual_par_by_ref()");
8276 refd_tr = dvt->get_template_restriction();
8277 } break;
8278 case A_PAR_TEMPL_IN:
8279 case A_PAR_TEMPL_OUT:
8280 case A_PAR_TEMPL_INOUT: {
8281 FormalPar* fp = dynamic_cast<FormalPar*>(ass);
8282 if (!fp) FATAL_ERROR("FormalPar::chk_actual_par_by_ref()");
8283 refd_tr = fp->get_template_restriction();
8284 } break;
8285 default:
8286 FATAL_ERROR("FormalPar::chk_actual_par_by_ref()");
8287 break;
8288 }
8289 refd_tr = Template::get_sub_restriction(refd_tr, ref);
8290 if (template_restriction!=refd_tr) {
8291 bool pre_call_check =
8292 Template::is_less_restrictive(template_restriction, refd_tr);
8293 bool post_call_check =
8294 Template::is_less_restrictive(refd_tr, template_restriction);
8295 if (pre_call_check || post_call_check) {
8296 ref->warning("Inadequate restriction on the referenced %s `%s', "
8297 "this may cause a dynamic test case error at runtime",
8298 ass->get_assname(), ref->get_dispname().c_str());
8299 ass->note("Referenced %s is here", ass->get_assname());
8300 }
8301 if (pre_call_check)
8302 ret_val_ap->set_gen_restriction_check(template_restriction);
8303 if (post_call_check)
8304 ret_val_ap->set_gen_post_restriction_check(refd_tr);
8305 }
8306 // for out and inout template parameters of external functions
8307 // always check because we do not trust user written C++ code
8308 if (refd_tr!=TR_NONE) {
8309 switch (my_parlist->get_my_def()->get_asstype()) {
8310 case A_EXT_FUNCTION:
8311 case A_EXT_FUNCTION_RVAL:
8312 case A_EXT_FUNCTION_RTEMP:
8313 ret_val_ap->set_gen_post_restriction_check(refd_tr);
8314 break;
8315 default:
8316 break;
8317 }
8318 }
8319 }
8320 return ret_val_ap;
8321 } else {
8322 actual_par->error("Reference to a %s was expected for an %s",
8323 expected_string, get_assname());
8324 return new ActualPar();
8325 }
8326 }
8327
8328 ActualPar *FormalPar::chk_actual_par_timer(TemplateInstance *actual_par,
8329 Type::expected_value_t exp_val)
8330 {
8331 Type *ap_type = actual_par->get_Type();
8332 if (ap_type) {
8333 ap_type->error("Explicit type specification cannot be used for a "
8334 "timer parameter");
8335 actual_par->chk_Type(0);
8336 }
8337 Ref_base *derived_ref = actual_par->get_DerivedRef();
8338 if (derived_ref) {
8339 derived_ref->error("An in-line modified template cannot be used as "
8340 "timer parameter");
8341 actual_par->chk_DerivedRef(0);
8342 }
8343 Template *ap_template = actual_par->get_Template();
8344 if (ap_template->is_Ref()) {
8345 Ref_base *ref = ap_template->get_Ref();
8346 Common::Assignment *ass = ref->get_refd_assignment();
8347 if (!ass) {
8348 delete ref;
8349 return new ActualPar();
8350 }
8351 switch (ass->get_asstype()) {
8352 case A_TIMER: {
8353 ArrayDimensions *dims = ass->get_Dimensions();
8354 if (dims) dims->chk_indices(ref, "timer", false, exp_val);
8355 else if (ref->get_subrefs()) ref->error("Reference to single %s "
8356 "cannot have field or array sub-references",
8357 ass->get_description().c_str());
8358 break; }
8359 case A_PAR_TIMER:
8360 if (ref->get_subrefs()) ref->error("Reference to %s cannot have "
8361 "field or array sub-references", ass->get_description().c_str());
8362 break;
8363 default:
8364 ref->error("Reference to a timer or timer parameter was expected for "
8365 "a timer parameter instead of %s", ass->get_description().c_str());
8366 }
8367 return new ActualPar(ref);
8368 } else {
8369 actual_par->error("Reference to a timer or timer parameter was "
8370 "expected for a timer parameter");
8371 return new ActualPar();
8372 }
8373 }
8374
8375 ActualPar *FormalPar::chk_actual_par_port(TemplateInstance *actual_par,
8376 Type::expected_value_t exp_val)
8377 {
8378 Type *ap_type = actual_par->get_Type();
8379 if (ap_type) {
8380 ap_type->warning("Explicit type specification is useless for a port "
8381 "parameter");
8382 actual_par->chk_Type(type);
8383 }
8384 Ref_base *derived_ref = actual_par->get_DerivedRef();
8385 if (derived_ref) {
8386 derived_ref->error("An in-line modified template cannot be used as "
8387 "port parameter");
8388 actual_par->chk_DerivedRef(type);
8389 }
8390 Template *ap_template = actual_par->get_Template();
8391 if (ap_template->is_Ref()) {
8392 Ref_base *ref = ap_template->get_Ref();
8393 Common::Assignment *ass = ref->get_refd_assignment();
8394 if (!ass) {
8395 delete ref;
8396 return new ActualPar();
8397 }
8398 bool asstype_correct = false;
8399 switch (ass->get_asstype()) {
8400 case A_PORT: {
8401 ArrayDimensions *dims = ass->get_Dimensions();
8402 if (dims) dims->chk_indices(ref, "port", false, exp_val);
8403 else if (ref->get_subrefs()) ref->error("Reference to single %s "
8404 "cannot have field or array sub-references",
8405 ass->get_description().c_str());
8406 asstype_correct = true;
8407 break; }
8408 case A_PAR_PORT:
8409 if (ref->get_subrefs()) ref->error("Reference to %s cannot have "
8410 "field or array sub-references", ass->get_description().c_str());
8411 asstype_correct = true;
8412 break;
8413 default:
8414 ref->error("Reference to a port or port parameter was expected for a "
8415 "port parameter instead of %s", ass->get_description().c_str());
8416 }
8417 if (asstype_correct) {
8418 Type *ref_type = ass->get_Type();
8419 if (ref_type && !type->is_identical(ref_type))
8420 ref->error("Type mismatch: Reference to a port or port parameter "
8421 "of type `%s' was expected instead of `%s'",
8422 type->get_typename().c_str(), ref_type->get_typename().c_str());
8423 }
8424 return new ActualPar(ref);
8425 } else {
8426 actual_par->error("Reference to a port or port parameter was expected "
8427 "for a port parameter");
8428 return new ActualPar();
8429 }
8430 }
8431
8432 void FormalPar::use_as_lvalue(const Location& p_loc)
8433 {
8434 switch (asstype) {
8435 case A_PAR_VAL_IN:
8436 case A_PAR_TEMPL_IN:
8437 break;
8438 default:
8439 FATAL_ERROR("FormalPar::use_as_lvalue()");
8440 }
8441 if (!used_as_lvalue) {
8442 Definition *my_def = my_parlist->get_my_def();
8443 if (!my_def) FATAL_ERROR("FormalPar::use_as_lvalue()");
8444 if (my_def->get_asstype() == A_TEMPLATE)
8445 p_loc.error("Parameter `%s' of the template cannot be passed further "
8446 "as `out' or `inout' parameter", id->get_dispname().c_str());
8447 else {
8448 // update the genname so that all references in the generated code
8449 // will point to the shadow object
8450 if (!lazy_eval) {
8451 set_genname(id->get_name() + "_shadow");
8452 }
8453 used_as_lvalue = true;
8454 }
8455 }
8456 }
8457
8458 char* FormalPar::generate_code_defval(char* str)
8459 {
8460 if (!defval.ap || defval_generated) return str;
8461 defval_generated = true;
8462 switch (defval.ap->get_selection()) {
8463 case ActualPar::AP_VALUE: {
8464 Value *val = defval.ap->get_Value();
8465 if (use_runtime_2 && TypeConv::needs_conv_refd(val)) {
8466 str = TypeConv::gen_conv_code_refd(str, val->get_lhs_name().c_str(), val);
8467 } else {
8468 str = val->generate_code_init(str, val->get_lhs_name().c_str());
8469 }
8470 break; }
8471 case ActualPar::AP_TEMPLATE: {
8472 TemplateInstance *ti = defval.ap->get_TemplateInstance();
8473 Template *temp = ti->get_Template();
8474 Ref_base *dref = ti->get_DerivedRef();
8475 if (dref) {
8476 expression_struct expr;
8477 Code::init_expr(&expr);
8478 expr.expr = mputprintf(expr.expr, "%s = ",
8479 temp->get_lhs_name().c_str());
8480 dref->generate_code(&expr);
8481 str = Code::merge_free_expr(str, &expr, false);
8482 }
8483 if (use_runtime_2 && TypeConv::needs_conv_refd(temp)) {
8484 str = TypeConv::gen_conv_code_refd(str, temp->get_lhs_name().c_str(), temp);
8485 } else {
8486 str = temp->generate_code_init(str, temp->get_lhs_name().c_str());
8487 }
8488 if (defval.ap->get_gen_restriction_check() != TR_NONE) {
8489 str = Template::generate_restriction_check_code(str,
8490 temp->get_lhs_name().c_str(), defval.ap->get_gen_restriction_check());
8491 }
8492 break; }
8493 case ActualPar::AP_REF:
8494 break;
8495 default:
8496 FATAL_ERROR("FormalPar::generate_code()");
8497 }
8498 return str;
8499 }
8500
8501 void FormalPar::generate_code_defval(output_struct *target, bool)
8502 {
8503 if (!defval.ap) return;
8504 switch (defval.ap->get_selection()) {
8505 case ActualPar::AP_VALUE: {
8506 Value *val = defval.ap->get_Value();
8507 const_def cdef;
8508 Code::init_cdef(&cdef);
8509 type->generate_code_object(&cdef, val);
8510 Code::merge_cdef(target, &cdef);
8511 Code::free_cdef(&cdef);
8512 break; }
8513 case ActualPar::AP_TEMPLATE: {
8514 TemplateInstance *ti = defval.ap->get_TemplateInstance();
8515 Template *temp = ti->get_Template();
8516 const_def cdef;
8517 Code::init_cdef(&cdef);
8518 type->generate_code_object(&cdef, temp);
8519 Code::merge_cdef(target, &cdef);
8520 Code::free_cdef(&cdef);
8521 break; }
8522 case ActualPar::AP_REF:
8523 break;
8524 default:
8525 FATAL_ERROR("FormalPar::generate_code()");
8526 }
8527 target->functions.post_init = generate_code_defval(target->functions.post_init);
8528 }
8529
8530 char *FormalPar::generate_code_fpar(char *str, bool display_unused /* = false */)
8531 {
8532 // the name of the parameter should not be displayed if the parameter is not
8533 // used (to avoid a compiler warning)
8534 bool display_name = (usage_found || display_unused || debugger_active ||
8535 (!enable_set_bound_out_param && (asstype == A_PAR_VAL_OUT || asstype == A_PAR_TEMPL_OUT)));
8536 const char *name_str = display_name ? id->get_name().c_str() : "";
8537 switch (asstype) {
8538 case A_PAR_VAL_IN:
8539 if (lazy_eval) {
8540 str = mputprintf(str, "Lazy_Param<%s>& %s", type->get_genname_value(my_scope).c_str(), name_str);
8541 } else {
8542 str = mputprintf(str, "const %s& %s", type->get_genname_value(my_scope).c_str(), name_str);
8543 }
8544 break;
8545 case A_PAR_VAL_OUT:
8546 case A_PAR_VAL_INOUT:
8547 case A_PAR_PORT:
8548 str = mputprintf(str, "%s& %s", type->get_genname_value(my_scope).c_str(),
8549 name_str);
8550 break;
8551 case A_PAR_TEMPL_IN:
8552 if (lazy_eval) {
8553 str = mputprintf(str, "Lazy_Param<%s>& %s", type->get_genname_template(my_scope).c_str(), name_str);
8554 } else {
8555 str = mputprintf(str, "const %s& %s", type->get_genname_template(my_scope).c_str(), name_str);
8556 }
8557 break;
8558 case A_PAR_TEMPL_OUT:
8559 case A_PAR_TEMPL_INOUT:
8560 str = mputprintf(str, "%s& %s",
8561 type->get_genname_template(my_scope).c_str(), name_str);
8562 break;
8563 case A_PAR_TIMER:
8564 str = mputprintf(str, "TIMER& %s", name_str);
8565 break;
8566 default:
8567 FATAL_ERROR("FormalPar::generate_code()");
8568 }
8569 return str;
8570 }
8571
8572 string FormalPar::get_reference_name(Scope* scope) const
8573 {
8574 string ret_val;
8575 if (lazy_eval) {
8576 ret_val += "((";
8577 switch (asstype) {
8578 case A_PAR_TEMPL_IN:
8579 ret_val += type->get_genname_template(scope);
8580 break;
8581 default:
8582 ret_val += type->get_genname_value(scope);
8583 break;
8584 }
8585 ret_val += "&)";
8586 }
8587 ret_val += get_id().get_name();
8588 if (lazy_eval) {
8589 ret_val += ")";
8590 }
8591 return ret_val;
8592 }
8593
8594 char *FormalPar::generate_code_object(char *str, const char *p_prefix, char refch)
8595 {
8596 const char *name_str = id->get_name().c_str();
8597 switch (asstype) {
8598 case A_PAR_VAL_IN:
8599 if (lazy_eval) {
8600 str = mputprintf(str, "Lazy_Param<%s> %s%s;\n", type->get_genname_value(my_scope).c_str(), p_prefix, name_str);
8601 } else {
8602 str = mputprintf(str, "%s %s%s;\n", type->get_genname_value(my_scope).c_str(), p_prefix, name_str);
8603 }
8604 break;
8605 case A_PAR_VAL_OUT:
8606 case A_PAR_VAL_INOUT:
8607 case A_PAR_PORT:
8608 str = mputprintf(str, "%s%c %s%s;\n",
8609 type->get_genname_value(my_scope).c_str(), refch, p_prefix, name_str);
8610 break;
8611 case A_PAR_TEMPL_IN:
8612 if (lazy_eval) {
8613 str = mputprintf(str, "Lazy_Param<%s> %s%s;\n", type->get_genname_template(my_scope).c_str(), p_prefix, name_str);
8614 } else {
8615 str = mputprintf(str, "%s %s%s;\n", type->get_genname_template(my_scope).c_str(), p_prefix, name_str);
8616 }
8617 break;
8618 case A_PAR_TEMPL_OUT:
8619 case A_PAR_TEMPL_INOUT:
8620 str = mputprintf(str, "%s%c %s%s;\n",
8621 type->get_genname_template(my_scope).c_str(), refch, p_prefix, name_str);
8622 break;
8623 case A_PAR_TIMER:
8624 str = mputprintf(str, "TIMER& %s%s;\n", p_prefix, name_str);
8625 break;
8626 default:
8627 FATAL_ERROR("FormalPar::generate_code_object()");
8628 }
8629 return str;
8630 }
8631
8632 char *FormalPar::generate_shadow_object(char *str) const
8633 {
8634 if (used_as_lvalue && !lazy_eval) {
8635 const string& t_genname = get_genname();
8636 const char *genname_str = t_genname.c_str();
8637 const char *name_str = id->get_name().c_str();
8638 switch (asstype) {
8639 case A_PAR_VAL_IN:
8640 str = mputprintf(str, "%s %s(%s);\n",
8641 type->get_genname_value(my_scope).c_str(), genname_str, name_str);
8642 break;
8643 case A_PAR_TEMPL_IN:
8644 str = mputprintf(str, "%s %s(%s);\n",
8645 type->get_genname_template(my_scope).c_str(), genname_str, name_str);
8646 break;
8647 default:
8648 FATAL_ERROR("FormalPar::generate_shadow_object()");
8649 }
8650 }
8651 return str;
8652 }
8653
8654 char *FormalPar::generate_code_set_unbound(char *str) const
8655 {
8656 switch (asstype) {
8657 case A_PAR_TEMPL_OUT:
8658 case A_PAR_VAL_OUT:
8659 str = mputprintf(str, "%s.clean_up();\n", id->get_name().c_str());
8660 break;
8661 default:
8662 break;
8663 }
8664 return str;
8665 }
8666
8667 void FormalPar::dump_internal(unsigned level) const
8668 {
8669 DEBUG(level, "%s: %s", get_assname(), id->get_dispname().c_str());
8670 if (type) type->dump(level + 1);
8671 if (checked) {
8672 if (defval.ap) {
8673 DEBUG(level + 1, "default value:");
8674 defval.ap->dump(level + 2);
8675 }
8676 } else {
8677 if (defval.ti) {
8678 DEBUG(level + 1, "default value:");
8679 defval.ti->dump(level + 2);
8680 }
8681 }
8682 }
8683
8684 // =================================
8685 // ===== FormalParList
8686 // =================================
8687
8688 FormalParList::~FormalParList()
8689 {
8690 size_t nof_pars = pars_v.size();
8691 for (size_t i = 0; i < nof_pars; i++) delete pars_v[i];
8692 pars_v.clear();
8693 pars_m.clear();
8694 }
8695
8696 FormalParList *FormalParList::clone() const
8697 {
8698 FATAL_ERROR("FormalParList::clone");
8699 }
8700
8701 void FormalParList::set_fullname(const string& p_fullname)
8702 {
8703 Node::set_fullname(p_fullname);
8704 for (size_t i = 0; i < pars_v.size(); i++) {
8705 FormalPar *par = pars_v[i];
8706 par->set_fullname(p_fullname + "." + par->get_id().get_dispname());
8707 }
8708 }
8709
8710 void FormalParList::set_my_scope(Scope *p_scope)
8711 {
8712 set_parent_scope(p_scope);
8713 Node::set_my_scope(p_scope);
8714 // the scope of parameters is set to the parent scope instead of this
8715 // because they cannot refer to each other
8716 for (size_t i = 0; i < pars_v.size(); i++) pars_v[i]->set_my_scope(p_scope);
8717 }
8718
8719 void FormalParList::add_fp(FormalPar *p_fp)
8720 {
8721 if (!p_fp) FATAL_ERROR("NULL parameter: Ttcn::FormalParList::add_fp()");
8722 pars_v.add(p_fp);
8723 p_fp->set_my_parlist(this);
8724 checked = false;
8725 }
8726
8727 bool FormalParList::has_notused_defval() const
8728 {
8729 for (size_t i = 0; i < pars_v.size(); i++) {
8730 if (pars_v[i]->has_notused_defval())
8731 return true;
8732 }
8733 return false;
8734 }
8735
8736 bool FormalParList::has_only_default_values() const
8737 {
8738 for (size_t i = 0; i < pars_v.size(); i++) {
8739 if (!pars_v[i]->has_defval()) {
8740 return false;
8741 }
8742 }
8743
8744 return true;
8745 }
8746
8747 bool FormalParList::has_fp_withName(const Identifier& p_name)
8748 {
8749 if (!checked) chk(Definition::A_UNDEF);
8750 return pars_m.has_key(p_name.get_name());
8751 }
8752
8753 FormalPar *FormalParList::get_fp_byName(const Identifier& p_name)
8754 {
8755 if (!checked) chk(Definition::A_UNDEF);
8756 return pars_m[p_name.get_name()];
8757 }
8758
8759 bool FormalParList::get_startability()
8760 {
8761 if(!checked) FATAL_ERROR("FormalParList::get_startability()");
8762 return is_startable;
8763 }
8764
8765 Common::Assignment *FormalParList::get_ass_bySRef(Common::Ref_simple *p_ref)
8766 {
8767 if (!p_ref || !checked) FATAL_ERROR("FormalParList::get_ass_bySRef()");
8768 if (p_ref->get_modid()) return parent_scope->get_ass_bySRef(p_ref);
8769 else {
8770 const string& name = p_ref->get_id()->get_name();
8771 if (pars_m.has_key(name)) return pars_m[name];
8772 else return parent_scope->get_ass_bySRef(p_ref);
8773 }
8774 }
8775
8776 bool FormalParList::has_ass_withId(const Identifier& p_id)
8777 {
8778 if (!checked) FATAL_ERROR("Ttcn::FormalParList::has_ass_withId()");
8779 return pars_m.has_key(p_id.get_name())
8780 || parent_scope->has_ass_withId(p_id);
8781 }
8782
8783 void FormalParList::set_genname(const string& p_prefix)
8784 {
8785 for (size_t i = 0; i < pars_v.size(); i++) {
8786 FormalPar *par = pars_v[i];
8787 const string& par_name = par->get_id().get_name();
8788 if (par->get_asstype() != Definition::A_PAR_TIMER)
8789 par->get_Type()->set_genname(p_prefix, par_name);
8790 if (par->has_defval()) {
8791 string embedded_genname(p_prefix);
8792 embedded_genname += '_';
8793 embedded_genname += par_name;
8794 embedded_genname += "_defval";
8795 ActualPar *defval = par->get_defval();
8796 switch (defval->get_selection()) {
8797 case ActualPar::AP_ERROR:
8798 case ActualPar::AP_REF:
8799 break;
8800 case ActualPar::AP_VALUE: {
8801 Value *v = defval->get_Value();
8802 v->set_genname_prefix("const_");
8803 v->set_genname_recursive(embedded_genname);
8804 break; }
8805 case ActualPar::AP_TEMPLATE: {
8806 Template *t = defval->get_TemplateInstance()->get_Template();
8807 t->set_genname_prefix("template_");
8808 t->set_genname_recursive(embedded_genname);
8809 break; }
8810 default:
8811 FATAL_ERROR("FormalParList::set_genname()");
8812 }
8813 }
8814 }
8815 }
8816
8817 void FormalParList::chk(Definition::asstype_t deftype)
8818 {
8819 if (checked) return;
8820 checked = true;
8821 min_nof_pars = 0;
8822 is_startable = true;
8823 Error_Context cntxt(this, "In formal parameter list");
8824 for (size_t i = 0; i < pars_v.size(); i++) {
8825 FormalPar *par = pars_v[i];
8826 const Identifier& id = par->get_id();
8827 const string& name = id.get_name();
8828 const char *dispname = id.get_dispname().c_str();
8829 if (pars_m.has_key(name)) {
8830 par->error("Duplicate parameter with name `%s'", dispname);
8831 pars_m[name]->note("Previous definition of `%s' is here", dispname);
8832 } else {
8833 pars_m.add(name, par);
8834 if (parent_scope && parent_scope->has_ass_withId(id)) {
8835 par->error("Parameter name `%s' is not unique in the scope "
8836 "hierarchy", dispname);
8837 Reference ref(0, id.clone());
8838 Common::Assignment *ass = parent_scope->get_ass_bySRef(&ref);
8839 if (!ass) FATAL_ERROR("FormalParList::chk()");
8840 ass->note("Symbol `%s' is already defined here in a higher scope "
8841 "unit", dispname);
8842 }
8843 }
8844 Error_Context cntxt2(par, "In parameter `%s'", dispname);
8845 par->chk();
8846 // check whether the parameter type is allowed
8847 switch (deftype) {
8848 case Definition::A_TEMPLATE:
8849 switch (par->get_asstype()) {
8850 case Definition::A_PAR_VAL_IN:
8851 case Definition::A_PAR_TEMPL_IN:
8852 // these are allowed
8853 break;
8854 default:
8855 par->error("A template cannot have %s", par->get_assname());
8856 }
8857 break;
8858 case Definition::A_TESTCASE:
8859 switch (par->get_asstype()) {
8860 case Definition::A_PAR_TIMER:
8861 case Definition::A_PAR_PORT:
8862 // these are forbidden
8863 par->error("A testcase cannot have %s", par->get_assname());
8864 default:
8865 break;
8866 }
8867 default:
8868 // everything is allowed for functions and altsteps
8869 break;
8870 }
8871 //startability chk
8872 switch(par->get_asstype()) {
8873 case Common::Assignment::A_PAR_VAL_IN:
8874 case Common::Assignment::A_PAR_TEMPL_IN:
8875 case Common::Assignment::A_PAR_VAL_INOUT:
8876 case Common::Assignment::A_PAR_TEMPL_INOUT:
8877 if (is_startable && par->get_Type()->is_component_internal())
8878 is_startable = false;
8879 break;
8880 default:
8881 is_startable = false;
8882 break;
8883 }
8884 if (!par->has_defval()) min_nof_pars = i + 1;
8885 // the last parameter without a default value determines the minimum
8886 }
8887 }
8888
8889 // check that @lazy paramterization not used in cases currently unsupported
8890 void FormalParList::chk_noLazyParams() {
8891 Error_Context cntxt(this, "In formal parameter list");
8892 for (size_t i = 0; i < pars_v.size(); i++) {
8893 FormalPar *par = pars_v[i];
8894 if (par->get_lazy_eval()) {
8895 par->error("Formal parameter `%s' cannot be @lazy, not supported in this case.",
8896 par->get_id().get_dispname().c_str());
8897 }
8898 }
8899 }
8900
8901 void FormalParList::chk_startability(const char *p_what, const char *p_name)
8902 {
8903 if(!checked) FATAL_ERROR("FormalParList::chk_startability()");
8904 if (is_startable) return;
8905 for (size_t i = 0; i < pars_v.size(); i++) {
8906 FormalPar *par = pars_v[i];
8907 switch (par->get_asstype()) {
8908 case Common::Assignment::A_PAR_VAL_IN:
8909 case Common::Assignment::A_PAR_TEMPL_IN:
8910 case Common::Assignment::A_PAR_VAL_INOUT:
8911 case Common::Assignment::A_PAR_TEMPL_INOUT:
8912 if (par->get_Type()->is_component_internal()) {
8913 map<Type*,void> type_chain;
8914 char* err_str = mprintf("a parameter or embedded in a parameter of "
8915 "a function used in a start operation. "
8916 "%s `%s' cannot be started on a parallel test component "
8917 "because of `%s'", p_what, p_name, par->get_description().c_str());
8918 par->get_Type()->chk_component_internal(type_chain, err_str);
8919 Free(err_str);
8920 }
8921 break;
8922 default:
8923 par->error("%s `%s' cannot be started on a parallel test component "
8924 "because it has %s", p_what, p_name, par->get_description().c_str());
8925 }
8926 }
8927 }
8928
8929 void FormalParList::chk_compatibility(FormalParList* p_fp_list,
8930 const char* where)
8931 {
8932 size_t nof_type_pars = pars_v.size();
8933 size_t nof_function_pars = p_fp_list->pars_v.size();
8934 // check for the number of parameters
8935 if (nof_type_pars != nof_function_pars) {
8936 p_fp_list->error("Too %s parameters: %lu was expected instead of %lu",
8937 nof_type_pars < nof_function_pars ? "many" : "few",
8938 (unsigned long) nof_type_pars, (unsigned long) nof_function_pars);
8939 }
8940 size_t upper_limit =
8941 nof_type_pars < nof_function_pars ? nof_type_pars : nof_function_pars;
8942 for (size_t i = 0; i < upper_limit; i++) {
8943 FormalPar *type_par = pars_v[i];
8944 FormalPar *function_par = p_fp_list->pars_v[i];
8945 Error_Context cntxt(function_par, "In parameter #%lu",
8946 (unsigned long) (i + 1));
8947 FormalPar::asstype_t type_par_asstype = type_par->get_asstype();
8948 FormalPar::asstype_t function_par_asstype = function_par->get_asstype();
8949 // check for parameter kind equivalence
8950 // (in, out or inout / value or template)
8951 if (type_par_asstype != function_par_asstype) {
8952 function_par->error("The kind of the parameter is not the same as in "
8953 "type `%s': %s was expected instead of %s", where,
8954 type_par->get_assname(), function_par->get_assname());
8955 }
8956 // check for type equivalence
8957 if (type_par_asstype != FormalPar::A_PAR_TIMER &&
8958 function_par_asstype != FormalPar::A_PAR_TIMER) {
8959 Type *type_par_type = type_par->get_Type();
8960 Type *function_par_type = function_par->get_Type();
8961 if (!type_par_type->is_identical(function_par_type)) {
8962 function_par_type->error("The type of the parameter is not the same "
8963 "as in type `%s': `%s' was expected instead of `%s'", where,
8964 type_par_type->get_typename().c_str(),
8965 function_par_type->get_typename().c_str());
8966 } else if (type_par_type->get_sub_type() && function_par_type->get_sub_type() &&
8967 (type_par_type->get_sub_type()->get_subtypetype()==function_par_type->get_sub_type()->get_subtypetype()) &&
8968 (!type_par_type->get_sub_type()->is_compatible(function_par_type->get_sub_type()))) {
8969 // TODO: maybe equivalence should be checked, or maybe that is too strict
8970 function_par_type->error(
8971 "Subtype mismatch: subtype %s has no common value with subtype %s",
8972 type_par_type->get_sub_type()->to_string().c_str(),
8973 function_par_type->get_sub_type()->to_string().c_str());
8974 }
8975 }
8976 // check for template restriction equivalence
8977 if (type_par->get_template_restriction()!=
8978 function_par->get_template_restriction()) {
8979 function_par->error("The template restriction of the parameter is "
8980 "not the same as in type `%s': %s restriction was expected instead "
8981 "of %s restriction", where,
8982 type_par->get_template_restriction()==TR_NONE ? "no" :
8983 Template::get_restriction_name(type_par->get_template_restriction()),
8984 function_par->get_template_restriction()==TR_NONE ? "no" :
8985 Template::get_restriction_name(function_par->
8986 get_template_restriction()));
8987 }
8988 // check for @lazy equivalence
8989 if (type_par->get_lazy_eval()!=function_par->get_lazy_eval()) {
8990 function_par->error("Parameter @lazy-ness mismatch");
8991 }
8992 // check for name equivalence
8993 const Identifier& type_par_id = type_par->get_id();
8994 const Identifier& function_par_id = function_par->get_id();
8995 if (type_par_id != function_par_id) {
8996 function_par->warning("The name of the parameter is not the same "
8997 "as in type `%s': `%s' was expected instead of `%s'", where,
8998 type_par_id.get_dispname().c_str(),
8999 function_par_id.get_dispname().c_str());
9000 }
9001 }
9002 }
9003
9004 bool FormalParList::fold_named_and_chk(ParsedActualParameters *p_paps,
9005 ActualParList *p_aplist)
9006 {
9007 const size_t num_named = p_paps->get_nof_nps();
9008 const size_t num_unnamed = p_paps->get_nof_tis();
9009 size_t num_actual = num_unnamed;
9010
9011 // Construct a map to tell us what index a FormalPar has
9012 typedef map<FormalPar*, size_t> formalpar_map_t;
9013 formalpar_map_t formalpar_map;
9014
9015 size_t num_fp = get_nof_fps();
9016 for (size_t fpx = 0; fpx < num_fp; ++fpx) {
9017 FormalPar *fp = get_fp_byIndex(fpx);
9018 formalpar_map.add(fp, new size_t(fpx));
9019 }
9020
9021 // Go through the named parameters
9022 for (size_t i = 0; i < num_named; ++i) {
9023 NamedParam *np = p_paps->extract_np_byIndex(i);
9024 // We are now responsible for np.
9025
9026 if (has_fp_withName(*np->get_name())) {
9027 // there is a formal parameter with that name
9028 FormalPar *fp = get_fp_byName(*np->get_name());
9029 const size_t is_at = *formalpar_map[fp]; // the index of the formal par
9030 if (is_at >= num_actual) {
9031 // There is no actual par in the unnamed part.
9032 // Create one from the named param.
9033
9034 // First, pad the gap with '-'
9035 for (; num_actual < is_at; ++num_actual) {
9036 Template *not_used;
9037 if (pars_v[num_actual]->has_defval()) {
9038 not_used = new Template(Template::TEMPLATE_NOTUSED);
9039 }
9040 else { // cannot use '-' if no default value
9041 not_used = new Template(Template::TEMPLATE_ERROR);
9042 }
9043 TemplateInstance *new_ti = new TemplateInstance(0, 0, not_used);
9044 // Conjure a location info at the beginning of the unnamed part
9045 // (that is, the beginning of the actual parameter list)
9046 new_ti->set_location(p_paps->get_tis()->get_filename(),
9047 p_paps->get_tis()->get_first_line(),
9048 p_paps->get_tis()->get_first_column(), 0, 0);
9049 p_paps->get_tis()->add_ti(new_ti);
9050 }
9051 TemplateInstance * namedti = np->extract_ti();
9052 p_paps->get_tis()->add_ti(namedti);
9053 ++num_actual;
9054 } else {
9055 // There is already an actual par at that position, fetch it
9056 TemplateInstance * ti = p_paps->get_tis()->get_ti_byIndex(is_at);
9057 Template::templatetype_t tt = ti->get_Template()->get_templatetype();
9058
9059 if (is_at >= num_unnamed && !ti->get_Type() && !ti->get_DerivedRef()
9060 && (tt == Template::TEMPLATE_NOTUSED || tt == Template::TEMPLATE_ERROR)) {
9061 // NotUsed in the named part => padding
9062 np->error("Named parameter `%s' out of order",
9063 np->get_name()->get_dispname().c_str());
9064 } else {
9065 // attempt to override an original unnamed param with a named one
9066 np->error("Formal parameter `%s' assigned more than once",
9067 np->get_name()->get_dispname().c_str());
9068 }
9069 }
9070 }
9071 else { // no formal parameter with that name
9072 char * nam = 0;
9073 switch (my_def->get_asstype()) {
9074 case Common::Assignment::A_TYPE: {
9075 Type *t = my_def->get_Type();
9076
9077 switch (t ? t->get_typetype() : 0) {
9078 case Type::T_FUNCTION:
9079 nam = mcopystr("Function reference");
9080 break;
9081 case Type::T_ALTSTEP:
9082 nam = mcopystr("Altstep reference");
9083 break;
9084 case Type::T_TESTCASE:
9085 nam = mcopystr("Testcase reference");
9086 break;
9087 default:
9088 FATAL_ERROR("FormalParList::chk_actual_parlist() "
9089 "Unexpected type %s", t->get_typename().c_str());
9090 } // switch(typetype)
9091 break; }
9092 default:
9093 nam = mcopystr(my_def->get_assname());
9094 break;
9095 } // switch(asstype)
9096
9097 *nam &= ~('a'-'A'); // Make the first letter uppercase
9098 p_paps->get_tis()->error("%s `%s' has no formal parameter `%s'",
9099 nam,
9100 my_def->get_fullname().c_str(),
9101 np->get_name()->get_dispname().c_str());
9102 Free(nam);
9103 }
9104 delete np;
9105 }
9106
9107 // Cleanup
9108 for (size_t fpx = 0; fpx < num_fp; ++fpx) {
9109 delete formalpar_map.get_nth_elem(fpx);
9110 }
9111 formalpar_map.clear();
9112
9113 return chk_actual_parlist(p_paps->get_tis(), p_aplist);
9114 }
9115
9116 bool FormalParList::chk_actual_parlist(TemplateInstances *p_tis,
9117 ActualParList *p_aplist)
9118 {
9119 size_t formal_pars = pars_v.size();
9120 size_t actual_pars = p_tis->get_nof_tis();
9121 // p_aplist->get_nof_pars() is usually 0 on entry
9122 bool error_flag = false;
9123
9124 if (min_nof_pars == formal_pars) {
9125 // none of the parameters have default value
9126 if (actual_pars != formal_pars) {
9127 p_tis->error("Too %s parameters: %lu was expected "
9128 "instead of %lu", actual_pars < formal_pars ? "few" : "many",
9129 (unsigned long) formal_pars, (unsigned long) actual_pars);
9130 error_flag = true;
9131 }
9132 } else {
9133 // some parameters have default value
9134 if (actual_pars < min_nof_pars) {
9135 p_tis->error("Too few parameters: at least %lu "
9136 "was expected instead of %lu",
9137 (unsigned long) min_nof_pars, (unsigned long) actual_pars);
9138 error_flag = true;
9139 } else if (actual_pars > formal_pars) {
9140 p_tis->error("Too many parameters: at most %lu "
9141 "was expected instead of %lu",
9142 (unsigned long) formal_pars, (unsigned long) actual_pars);
9143 error_flag = true;
9144 }
9145 }
9146
9147 // Do not check actual parameters in excess of the formal ones
9148 size_t upper_limit = actual_pars < formal_pars ? actual_pars : formal_pars;
9149 for (size_t i = 0; i < upper_limit; i++) {
9150 TemplateInstance *ti = p_tis->get_ti_byIndex(i);
9151
9152 // the formal parameter for the current actual parameter
9153 FormalPar *fp = pars_v[i];
9154 Error_Context cntxt(ti, "In parameter #%lu for `%s'",
9155 (unsigned long) (i + 1), fp->get_id().get_dispname().c_str());
9156 if (!ti->get_Type() && !ti->get_DerivedRef() && ti->get_Template()
9157 ->get_templatetype() == Template::TEMPLATE_NOTUSED) {
9158 if (fp->has_defval()) {
9159 ActualPar *defval = fp->get_defval();
9160 p_aplist->add(new ActualPar(defval));
9161 if (defval->is_erroneous()) error_flag = true;
9162 } else {
9163 ti->error("Not used symbol (`-') cannot be used for parameter "
9164 "that does not have default value");
9165 p_aplist->add(new ActualPar());
9166 error_flag = true;
9167 }
9168 } else if (!ti->get_Type() && !ti->get_DerivedRef() && ti->get_Template()
9169 ->get_templatetype() == Template::TEMPLATE_ERROR) {
9170 ti->error("Parameter not specified");
9171 } else {
9172 ActualPar *ap = fp->chk_actual_par(ti, Type::EXPECTED_DYNAMIC_VALUE);
9173 p_aplist->add(ap);
9174 if (ap->is_erroneous()) error_flag = true;
9175 }
9176 }
9177
9178 // The rest of formal parameters have no corresponding actual parameters.
9179 // Create actual parameters for them based on their default values
9180 // (which must exist).
9181 for (size_t i = upper_limit; i < formal_pars; i++) {
9182 FormalPar *fp = pars_v[i];
9183 if (fp->has_defval()) {
9184 ActualPar *defval = fp->get_defval();
9185 p_aplist->add(new ActualPar(defval));
9186 if (defval->is_erroneous()) error_flag = true;
9187 } else {
9188 p_aplist->add(new ActualPar()); // erroneous
9189 error_flag = true;
9190 }
9191 }
9192 return error_flag;
9193 }
9194
9195 bool FormalParList::chk_activate_argument(ActualParList *p_aplist,
9196 const char* p_description)
9197 {
9198 bool ret_val = true;
9199 for(size_t i = 0; i < p_aplist->get_nof_pars(); i++) {
9200 ActualPar *t_ap = p_aplist->get_par(i);
9201 if(t_ap->get_selection() != ActualPar::AP_REF) continue;
9202 FormalPar *t_fp = pars_v[i];
9203 switch(t_fp->get_asstype()) {
9204 case Common::Assignment::A_PAR_VAL_OUT:
9205 case Common::Assignment::A_PAR_VAL_INOUT:
9206 case Common::Assignment::A_PAR_TEMPL_OUT:
9207 case Common::Assignment::A_PAR_TEMPL_INOUT:
9208 case Common::Assignment::A_PAR_TIMER:
9209 //the checking shall be performed for these parameter types
9210 break;
9211 case Common::Assignment::A_PAR_PORT:
9212 // port parameters are always correct because ports can be defined
9213 // only in component types
9214 continue;
9215 default:
9216 FATAL_ERROR("FormalParList::chk_activate_argument()");
9217 }
9218 Ref_base *t_ref = t_ap->get_Ref();
9219 Common::Assignment *t_par_ass = t_ref->get_refd_assignment();
9220 if(!t_par_ass) FATAL_ERROR("FormalParList::chk_activate_argument()");
9221 switch (t_par_ass->get_asstype()) {
9222 case Common::Assignment::A_VAR:
9223 case Common::Assignment::A_VAR_TEMPLATE:
9224 case Common::Assignment::A_TIMER:
9225 // it is not allowed to pass references of local variables or timers
9226 if (t_par_ass->is_local()) {
9227 t_ref->error("Parameter #%lu of %s refers to %s, which is a local "
9228 "definition within a statement block and may have shorter "
9229 "lifespan than the activated default. Only references to "
9230 "variables and timers defined in the component type can be passed "
9231 "to activated defaults", (unsigned long) (i + 1), p_description,
9232 t_par_ass->get_description().c_str());
9233 ret_val = false;
9234 }
9235 break;
9236 case Common::Assignment::A_PAR_VAL_IN:
9237 case Common::Assignment::A_PAR_VAL_OUT:
9238 case Common::Assignment::A_PAR_VAL_INOUT:
9239 case Common::Assignment::A_PAR_TEMPL_IN:
9240 case Common::Assignment::A_PAR_TEMPL_OUT:
9241 case Common::Assignment::A_PAR_TEMPL_INOUT:
9242 case Common::Assignment::A_PAR_TIMER: {
9243 // it is not allowed to pass references pointing to formal parameters
9244 // except for activate() statements within testcases
9245 // note: all defaults are deactivated at the end of the testcase
9246 FormalPar *t_refd_fp = dynamic_cast<FormalPar*>(t_par_ass);
9247 if (!t_refd_fp) FATAL_ERROR("FormalParList::chk_activate_argument()");
9248 FormalParList *t_fpl = t_refd_fp->get_my_parlist();
9249 if (!t_fpl || !t_fpl->my_def)
9250 FATAL_ERROR("FormalParList::chk_activate_argument()");
9251 if (t_fpl->my_def->get_asstype() != Common::Assignment::A_TESTCASE) {
9252 t_ref->error("Parameter #%lu of %s refers to %s, which may have "
9253 "shorter lifespan than the activated default. Only references to "
9254 "variables and timers defined in the component type can be passed "
9255 "to activated defaults", (unsigned long) (i + 1), p_description,
9256 t_par_ass->get_description().c_str());
9257 ret_val = false;
9258 } }
9259 default:
9260 break;
9261 }
9262 }
9263 return ret_val;
9264 }
9265
9266 char *FormalParList::generate_code(char *str, size_t display_unused /* = 0 */)
9267 {
9268 for (size_t i = 0; i < pars_v.size(); i++) {
9269 if (i > 0) str = mputstr(str, ", ");
9270 str = pars_v[i]->generate_code_fpar(str, i < display_unused);
9271 }
9272 return str;
9273 }
9274
9275 char* FormalParList::generate_code_defval(char* str)
9276 {
9277 for (size_t i = 0; i < pars_v.size(); i++) {
9278 str = pars_v[i]->generate_code_defval(str);
9279 }
9280 return str;
9281 }
9282
9283 void FormalParList::generate_code_defval(output_struct *target)
9284 {
9285 for (size_t i = 0; i < pars_v.size(); i++) {
9286 pars_v[i]->generate_code_defval(target);
9287 }
9288 }
9289
9290 char *FormalParList::generate_code_actual_parlist(char *str,
9291 const char *p_prefix)
9292 {
9293 for (size_t i = 0; i < pars_v.size(); i++) {
9294 if (i > 0) str = mputstr(str, ", ");
9295 str = mputstr(str, p_prefix);
9296 str = mputstr(str, pars_v[i]->get_id().get_name().c_str());
9297 }
9298 return str;
9299 }
9300
9301 char *FormalParList::generate_code_object(char *str, const char *p_prefix, char refch)
9302 {
9303 for (size_t i = 0; i < pars_v.size(); i++)
9304 str = pars_v[i]->generate_code_object(str, p_prefix, refch);
9305 return str;
9306 }
9307
9308 char *FormalParList::generate_shadow_objects(char *str) const
9309 {
9310 for (size_t i = 0; i < pars_v.size(); i++)
9311 str = pars_v[i]->generate_shadow_object(str);
9312 return str;
9313 }
9314
9315 char *FormalParList::generate_code_set_unbound(char *str) const
9316 {
9317 if (enable_set_bound_out_param) return str;
9318 for (size_t i = 0; i < pars_v.size(); i++)
9319 str = pars_v[i]->generate_code_set_unbound(str);
9320 return str;
9321 }
9322
9323
9324 void FormalParList::dump(unsigned level) const
9325 {
9326 size_t nof_pars = pars_v.size();
9327 DEBUG(level, "formal parameters: %lu pcs.", (unsigned long) nof_pars);
9328 for(size_t i = 0; i < nof_pars; i++) pars_v[i]->dump(level + 1);
9329 }
9330
9331 // =================================
9332 // ===== ActualPar
9333 // =================================
9334
9335 ActualPar::ActualPar(Value *v)
9336 : Node(), selection(AP_VALUE), my_scope(0), gen_restriction_check(TR_NONE),
9337 gen_post_restriction_check(TR_NONE)
9338 {
9339 if (!v) FATAL_ERROR("ActualPar::ActualPar()");
9340 val = v;
9341 }
9342
9343 ActualPar::ActualPar(TemplateInstance *t)
9344 : Node(), selection(AP_TEMPLATE), my_scope(0),
9345 gen_restriction_check(TR_NONE), gen_post_restriction_check(TR_NONE)
9346 {
9347 if (!t) FATAL_ERROR("ActualPar::ActualPar()");
9348 temp = t;
9349 }
9350
9351 ActualPar::ActualPar(Ref_base *r)
9352 : Node(), selection(AP_REF), my_scope(0), gen_restriction_check(TR_NONE),
9353 gen_post_restriction_check(TR_NONE)
9354 {
9355 if (!r) FATAL_ERROR("ActualPar::ActualPar()");
9356 ref = r;
9357 }
9358
9359 ActualPar::ActualPar(ActualPar *a)
9360 : Node(), selection(AP_DEFAULT), my_scope(0),
9361 gen_restriction_check(TR_NONE), gen_post_restriction_check(TR_NONE)
9362 {
9363 if (!a) FATAL_ERROR("ActualPar::ActualPar()");
9364 act = a;
9365 }
9366
9367 ActualPar::~ActualPar()
9368 {
9369 switch(selection) {
9370 case AP_ERROR:
9371 break;
9372 case AP_VALUE:
9373 delete val;
9374 break;
9375 case AP_TEMPLATE:
9376 delete temp;
9377 break;
9378 case AP_REF:
9379 delete ref;
9380 break;
9381 case AP_DEFAULT:
9382 break; // nothing to do with act
9383 default:
9384 FATAL_ERROR("ActualPar::~ActualPar()");
9385 }
9386 }
9387
9388 ActualPar *ActualPar::clone() const
9389 {
9390 FATAL_ERROR("ActualPar::clone");
9391 }
9392
9393 void ActualPar::set_fullname(const string& p_fullname)
9394 {
9395 Node::set_fullname(p_fullname);
9396 switch(selection) {
9397 case AP_ERROR:
9398 break;
9399 case AP_VALUE:
9400 val->set_fullname(p_fullname);
9401 break;
9402 case AP_TEMPLATE:
9403 temp->set_fullname(p_fullname);
9404 break;
9405 case AP_REF:
9406 ref->set_fullname(p_fullname);
9407 break;
9408 case AP_DEFAULT:
9409 break;
9410 default:
9411 FATAL_ERROR("ActualPar::set_fullname()");
9412 }
9413 }
9414
9415 void ActualPar::set_my_scope(Scope *p_scope)
9416 {
9417 my_scope = p_scope;
9418 switch(selection) {
9419 case AP_ERROR:
9420 break;
9421 case AP_VALUE:
9422 val->set_my_scope(p_scope);
9423 break;
9424 case AP_TEMPLATE:
9425 temp->set_my_scope(p_scope);
9426 break;
9427 case AP_REF:
9428 ref->set_my_scope(p_scope);
9429 break;
9430 case AP_DEFAULT:
9431 switch (act->selection) {
9432 case AP_REF:
9433 ref->set_my_scope(p_scope);
9434 break;
9435 case AP_VALUE:
9436 break;
9437 case AP_TEMPLATE:
9438 break;
9439 default:
9440 FATAL_ERROR("ActualPar::set_my_scope()");
9441 }
9442 break;
9443 default:
9444 FATAL_ERROR("ActualPar::set_my_scope()");
9445 }
9446 }
9447
9448 Value *ActualPar::get_Value() const
9449 {
9450 if (selection != AP_VALUE) FATAL_ERROR("ActualPar::get_Value()");
9451 return val;
9452 }
9453
9454 TemplateInstance *ActualPar::get_TemplateInstance() const
9455 {
9456 if (selection != AP_TEMPLATE)
9457 FATAL_ERROR("ActualPar::get_TemplateInstance()");
9458 return temp;
9459 }
9460
9461 Ref_base *ActualPar::get_Ref() const
9462 {
9463 if (selection != AP_REF) FATAL_ERROR("ActualPar::get_Ref()");
9464 return ref;
9465 }
9466
9467 ActualPar *ActualPar::get_ActualPar() const
9468 {
9469 if (selection != AP_DEFAULT) FATAL_ERROR("ActualPar::get_ActualPar()");
9470 return act;
9471 }
9472
9473 void ActualPar::chk_recursions(ReferenceChain& refch)
9474 {
9475 switch (selection) {
9476 case AP_VALUE:
9477 refch.mark_state();
9478 val->chk_recursions(refch);
9479 refch.prev_state();
9480 break;
9481 case AP_TEMPLATE: {
9482 Ref_base *derived_ref = temp->get_DerivedRef();
9483 if (derived_ref) {
9484 ActualParList *parlist = derived_ref->get_parlist();
9485 if (parlist) {
9486 refch.mark_state();
9487 parlist->chk_recursions(refch);
9488 refch.prev_state();
9489 }
9490 }
9491
9492 Ttcn::Def_Template* defTemp = temp->get_Referenced_Base_Template();
9493 if (defTemp) {
9494 refch.mark_state();
9495 refch.add(defTemp->get_fullname());
9496 refch.prev_state();
9497 }
9498 refch.mark_state();
9499 temp->get_Template()->chk_recursions(refch);
9500 refch.prev_state();
9501 }
9502 default:
9503 break;
9504 }
9505 }
9506
9507 bool ActualPar::has_single_expr()
9508 {
9509 switch (selection) {
9510 case AP_VALUE:
9511 return val->has_single_expr();
9512 case AP_TEMPLATE:
9513 if (gen_restriction_check!=TR_NONE ||
9514 gen_post_restriction_check!=TR_NONE) return false;
9515 return temp->has_single_expr();
9516 case AP_REF:
9517 if (gen_restriction_check!=TR_NONE ||
9518 gen_post_restriction_check!=TR_NONE) return false;
9519 if (use_runtime_2 && ref->get_subrefs() != NULL) {
9520 FieldOrArrayRefs* subrefs = ref->get_subrefs();
9521 for (size_t i = 0; i < subrefs->get_nof_refs(); ++i) {
9522 if (FieldOrArrayRef::ARRAY_REF == subrefs->get_ref(i)->get_type()) {
9523 return false;
9524 }
9525 }
9526 }
9527 return ref->has_single_expr();
9528 case AP_DEFAULT:
9529 return true;
9530 default:
9531 FATAL_ERROR("ActualPar::has_single_expr()");
9532 return false;
9533 }
9534 }
9535
9536 void ActualPar::set_code_section(
9537 GovernedSimple::code_section_t p_code_section)
9538 {
9539 switch (selection) {
9540 case AP_VALUE:
9541 val->set_code_section(p_code_section);
9542 break;
9543 case AP_TEMPLATE:
9544 temp->set_code_section(p_code_section);
9545 break;
9546 case AP_REF:
9547 ref->set_code_section(p_code_section);
9548 default:
9549 break;
9550 }
9551 }
9552
9553 void ActualPar::generate_code(expression_struct *expr, bool copy_needed, bool lazy_param, bool used_as_lvalue) const
9554 {
9555 switch (selection) {
9556 case AP_VALUE:
9557 if (lazy_param) { // copy_needed doesn't matter in this case
9558 LazyParamData::init(used_as_lvalue);
9559 LazyParamData::generate_code(expr, val, my_scope);
9560 LazyParamData::clean();
9561 if (val->get_valuetype() == Value::V_REFD) {
9562 // check if the reference is a parameter, mark it as used if it is
9563 Reference* ref = dynamic_cast<Reference*>(val->get_reference());
9564 if (ref != NULL) {
9565 ref->ref_usage_found();
9566 }
9567 }
9568 } else {
9569 if (copy_needed) expr->expr = mputprintf(expr->expr, "%s(",
9570 val->get_my_governor()->get_genname_value(my_scope).c_str());
9571 if (use_runtime_2 && TypeConv::needs_conv_refd(val)) {
9572 // Generate everything to preamble to be able to tackle the wrapper
9573 // constructor call. TODO: Reduce the number of temporaries created.
9574 const string& tmp_id = val->get_temporary_id();
9575 const char *tmp_id_str = tmp_id.c_str();
9576 expr->preamble = mputprintf(expr->preamble, "%s %s;\n",
9577 val->get_my_governor()->get_genname_value(my_scope).c_str(),
9578 tmp_id_str);
9579 expr->preamble = TypeConv::gen_conv_code_refd(expr->preamble,
9580 tmp_id_str, val);
9581 expr->expr = mputstr(expr->expr, tmp_id_str);
9582 } else val->generate_code_expr(expr);
9583 if (copy_needed) expr->expr = mputc(expr->expr, ')');
9584 }
9585 break;
9586 case AP_TEMPLATE:
9587 if (lazy_param) { // copy_needed doesn't matter in this case
9588 LazyParamData::init(used_as_lvalue);
9589 LazyParamData::generate_code(expr, temp, gen_restriction_check, my_scope);
9590 LazyParamData::clean();
9591 if (temp->get_DerivedRef() != NULL ||
9592 temp->get_Template()->get_templatetype() == Template::TEMPLATE_REFD) {
9593 // check if the reference is a parameter, mark it as used if it is
9594 Reference* ref = dynamic_cast<Reference*>(temp->get_DerivedRef() != NULL ?
9595 temp->get_DerivedRef() : temp->get_Template()->get_reference());
9596 if (ref != NULL) {
9597 ref->ref_usage_found();
9598 }
9599 }
9600 } else {
9601 if (copy_needed)
9602 expr->expr = mputprintf(expr->expr, "%s(", temp->get_Template()
9603 ->get_my_governor()->get_genname_template(my_scope).c_str());
9604 if (use_runtime_2 && TypeConv::needs_conv_refd(temp->get_Template())) {
9605 const string& tmp_id = temp->get_Template()->get_temporary_id();
9606 const char *tmp_id_str = tmp_id.c_str();
9607 expr->preamble = mputprintf(expr->preamble, "%s %s;\n",
9608 temp->get_Template()->get_my_governor()
9609 ->get_genname_template(my_scope).c_str(), tmp_id_str);
9610 expr->preamble = TypeConv::gen_conv_code_refd(expr->preamble,
9611 tmp_id_str, temp->get_Template());
9612 // Not incorporated into gen_conv_code() yet.
9613 if (gen_restriction_check != TR_NONE)
9614 expr->preamble = Template::generate_restriction_check_code(
9615 expr->preamble, tmp_id_str, gen_restriction_check);
9616 expr->expr = mputstr(expr->expr, tmp_id_str);
9617 } else temp->generate_code(expr, gen_restriction_check);
9618 if (copy_needed) expr->expr = mputc(expr->expr, ')');
9619 }
9620 break;
9621 case AP_REF:
9622 if (lazy_param) FATAL_ERROR("ActualPar::generate_code()"); // syntax error should have already happened
9623 if (copy_needed) FATAL_ERROR("ActualPar::generate_code()");
9624 if (gen_restriction_check != TR_NONE ||
9625 gen_post_restriction_check != TR_NONE) {
9626 // generate runtime check for restricted templates
9627 // code for reference + restriction check
9628 Common::Assignment *ass = ref->get_refd_assignment();
9629 const string& tmp_id= my_scope->get_scope_mod_gen()->get_temporary_id();
9630 const char *tmp_id_str = tmp_id.c_str();
9631 expression_struct ref_expr;
9632 Code::init_expr(&ref_expr);
9633 ref->generate_code_const_ref(&ref_expr);
9634 ref_expr.preamble = mputprintf(ref_expr.preamble, "%s& %s = %s;\n",
9635 ass->get_Type()->get_genname_template(ref->get_my_scope()).c_str(),
9636 tmp_id_str, ref_expr.expr);
9637 if (gen_restriction_check != TR_NONE) {
9638 ref_expr.preamble = Template::generate_restriction_check_code(
9639 ref_expr.preamble, tmp_id_str, gen_restriction_check);
9640 }
9641 if (gen_post_restriction_check != TR_NONE) {
9642 ref_expr.postamble = Template::generate_restriction_check_code(
9643 ref_expr.postamble, tmp_id_str, gen_post_restriction_check);
9644 }
9645 // copy content of ref_expr to expr
9646 expr->preamble = mputstr(expr->preamble, ref_expr.preamble);
9647 expr->expr = mputprintf(expr->expr, "%s", tmp_id_str);
9648 expr->postamble = mputstr(expr->postamble, ref_expr.postamble);
9649 Code::free_expr(&ref_expr);
9650 } else {
9651 ref->generate_code(expr);
9652 }
9653 break;
9654 case AP_DEFAULT:
9655 if (copy_needed) FATAL_ERROR("ActualPar::generate_code()");
9656 switch (act->selection) {
9657 case AP_REF:
9658 if (lazy_param) {
9659 LazyParamData::generate_code_ap_default_ref(expr, act->ref, my_scope);
9660 } else {
9661 act->ref->generate_code(expr);
9662 }
9663 break;
9664 case AP_VALUE:
9665 if (lazy_param) {
9666 LazyParamData::generate_code_ap_default_value(expr, act->val, my_scope);
9667 } else {
9668 expr->expr = mputstr(expr->expr, act->val->get_genname_own(my_scope).c_str());
9669 }
9670 break;
9671 case AP_TEMPLATE:
9672 if (lazy_param) {
9673 LazyParamData::generate_code_ap_default_ti(expr, act->temp, my_scope);
9674 } else {
9675 expr->expr = mputstr(expr->expr, act->temp->get_Template()->get_genname_own(my_scope).c_str());
9676 }
9677 break;
9678 default:
9679 FATAL_ERROR("ActualPar::generate_code()");
9680 }
9681 break;
9682 default:
9683 FATAL_ERROR("ActualPar::generate_code()");
9684 }
9685 }
9686
9687 char *ActualPar::rearrange_init_code(char *str, Common::Module* usage_mod)
9688 {
9689 switch (selection) {
9690 case AP_VALUE:
9691 str = val->rearrange_init_code(str, usage_mod);
9692 break;
9693 case AP_TEMPLATE:
9694 str = temp->rearrange_init_code(str, usage_mod);
9695 case AP_REF:
9696 break;
9697 case AP_DEFAULT:
9698 str = act->rearrange_init_code_defval(str, usage_mod);
9699 break;
9700 default:
9701 FATAL_ERROR("ActualPar::rearrange_init_code()");
9702 }
9703 return str;
9704 }
9705
9706 char *ActualPar::rearrange_init_code_defval(char *str, Common::Module* usage_mod)
9707 {
9708 switch (selection) {
9709 case AP_VALUE:
9710 if (val->get_my_scope()->get_scope_mod_gen() == usage_mod) {
9711 str = val->generate_code_init(str, val->get_lhs_name().c_str());
9712 }
9713 break;
9714 case AP_TEMPLATE: {
9715 str = temp->rearrange_init_code(str, usage_mod);
9716 Template *t = temp->get_Template();
9717 if (t->get_my_scope()->get_scope_mod_gen() == usage_mod) {
9718 Ref_base *dref = temp->get_DerivedRef();
9719 if (dref) {
9720 expression_struct expr;
9721 Code::init_expr(&expr);
9722 expr.expr = mputprintf(expr.expr, "%s = ", t->get_lhs_name().c_str());
9723 dref->generate_code(&expr);
9724 str = Code::merge_free_expr(str, &expr, false);
9725 }
9726 str = t->generate_code_init(str, t->get_lhs_name().c_str());
9727 }
9728 break; }
9729 default:
9730 FATAL_ERROR("ActualPar::rearrange_init_code_defval()");
9731 }
9732 return str;
9733 }
9734
9735 void ActualPar::append_stringRepr(string& str) const
9736 {
9737 switch (selection) {
9738 case AP_VALUE:
9739 str += val->get_stringRepr();
9740 break;
9741 case AP_TEMPLATE:
9742 temp->append_stringRepr(str);
9743 break;
9744 case AP_REF:
9745 str += ref->get_dispname();
9746 break;
9747 case AP_DEFAULT:
9748 str += '-';
9749 break;
9750 default:
9751 str += "<erroneous actual parameter>";
9752 }
9753 }
9754
9755 void ActualPar::dump(unsigned level) const
9756 {
9757 switch (selection) {
9758 case AP_VALUE:
9759 DEBUG(level, "actual parameter: value");
9760 val->dump(level + 1);
9761 break;
9762 case AP_TEMPLATE:
9763 DEBUG(level, "actual parameter: template");
9764 temp->dump(level + 1);
9765 break;
9766 case AP_REF:
9767 DEBUG(level, "actual parameter: reference");
9768 ref->dump(level + 1);
9769 break;
9770 case AP_DEFAULT:
9771 DEBUG(level, "actual parameter: default");
9772 break;
9773 default:
9774 DEBUG(level, "actual parameter: erroneous");
9775 }
9776 }
9777
9778 // =================================
9779 // ===== ActualParList
9780 // =================================
9781
9782 ActualParList::ActualParList(const ActualParList& p)
9783 : Node(p)
9784 {
9785 size_t nof_pars = p.params.size();
9786 for (size_t i = 0; i < nof_pars; i++) params.add(p.params[i]->clone());
9787 }
9788
9789 ActualParList::~ActualParList()
9790 {
9791 size_t nof_pars = params.size();
9792 for (size_t i = 0; i < nof_pars; i++) delete params[i];
9793 params.clear();
9794 }
9795
9796 ActualParList *ActualParList::clone() const
9797 {
9798 return new ActualParList(*this);
9799 }
9800
9801 void ActualParList::set_fullname(const string& p_fullname)
9802 {
9803 Node::set_fullname(p_fullname);
9804 size_t nof_pars = params.size();
9805 for(size_t i = 0; i < nof_pars; i++)
9806 params[i]->set_fullname(p_fullname +
9807 ".<parameter" + Int2string(i + 1) + ">");
9808 }
9809
9810 void ActualParList::set_my_scope(Scope *p_scope)
9811 {
9812 size_t nof_pars = params.size();
9813 for (size_t i = 0; i < nof_pars; i++) params[i]->set_my_scope(p_scope);
9814 }
9815
9816 void ActualParList::chk_recursions(ReferenceChain& refch)
9817 {
9818 size_t nof_pars = params.size();
9819 for (size_t i = 0; i < nof_pars; i++)
9820 params[i]->chk_recursions(refch);
9821 }
9822
9823 void ActualParList::generate_code_noalias(expression_struct *expr, FormalParList *p_fpl)
9824 {
9825 size_t nof_pars = params.size();
9826 for (size_t i = 0; i < nof_pars; i++) {
9827 if (i > 0) expr->expr = mputstr(expr->expr, ", ");
9828 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());
9829 }
9830 }
9831
9832 void ActualParList::generate_code_alias(expression_struct *expr,
9833 FormalParList *p_fpl, Type *p_comptype, bool p_compself)
9834 {
9835 size_t nof_pars = params.size();
9836 // collect all value and template definitions that are passed by reference
9837 map<Common::Assignment*, void> value_refs, template_refs;
9838 for (size_t i = 0; i < nof_pars; i++) {
9839 ActualPar *par = params[i];
9840 if (par->get_selection() == ActualPar::AP_DEFAULT)
9841 par = par->get_ActualPar();
9842 if (par->get_selection() == ActualPar::AP_REF) {
9843 Common::Assignment *ass = par->get_Ref()->get_refd_assignment();
9844 switch (ass->get_asstype()) {
9845 case Common::Assignment::A_VAR:
9846 case Common::Assignment::A_PAR_VAL_IN:
9847 case Common::Assignment::A_PAR_VAL_OUT:
9848 case Common::Assignment::A_PAR_VAL_INOUT:
9849 if (!value_refs.has_key(ass)) value_refs.add(ass, 0);
9850 break;
9851 case Common::Assignment::A_VAR_TEMPLATE:
9852 case Common::Assignment::A_PAR_TEMPL_IN:
9853 case Common::Assignment::A_PAR_TEMPL_OUT:
9854 case Common::Assignment::A_PAR_TEMPL_INOUT:
9855 if (!template_refs.has_key(ass)) template_refs.add(ass, 0);
9856 default:
9857 break;
9858 }
9859 }
9860 }
9861 // walk through the parameter list and generate the code
9862 // add an extra copy constructor call to the referenced value and template
9863 // parameters if the referred definition is also passed by reference to
9864 // another parameter
9865 for (size_t i = 0; i < nof_pars; i++) {
9866 if (i > 0) expr->expr = mputstr(expr->expr, ", ");
9867 ActualPar *par = params[i];
9868 bool copy_needed = false;
9869 // the copy constructor call is not needed if the parameter is copied
9870 // into a shadow object in the body of the called function
9871 if (!p_fpl || !p_fpl->get_fp_byIndex(i)->get_used_as_lvalue()) {
9872 switch (par->get_selection()) {
9873 case ActualPar::AP_VALUE: {
9874 Value *v = par->get_Value();
9875 if (v->get_valuetype() == Value::V_REFD) {
9876 Common::Assignment *t_ass =
9877 v->get_reference()->get_refd_assignment();
9878 if (value_refs.has_key(t_ass)) {
9879 // a reference to the same variable is also passed to the called
9880 // definition
9881 copy_needed = true;
9882 } else if (p_comptype || p_compself) {
9883 // the called definition has a 'runs on' clause so it can access
9884 // component variables
9885 switch (t_ass->get_asstype()) {
9886 case Common::Assignment::A_PAR_VAL_OUT:
9887 case Common::Assignment::A_PAR_VAL_INOUT:
9888 // the parameter may be an alias of a component variable
9889 copy_needed = true;
9890 break;
9891 case Common::Assignment::A_VAR:
9892 // copy is needed if t_ass is a component variable that is
9893 // visible by the called definition
9894 if (!t_ass->is_local()) copy_needed = true;
9895 /** \todo component type compatibility: check whether t_ass is
9896 * visible from p_comptype (otherwise copy is not needed) */
9897 default:
9898 break;
9899 }
9900 }
9901 }
9902 break; }
9903 case ActualPar::AP_TEMPLATE: {
9904 TemplateInstance *ti = par->get_TemplateInstance();
9905 if (!ti->get_DerivedRef()) {
9906 Template *t = ti->get_Template();
9907 if (t->get_templatetype() == Template::TEMPLATE_REFD) {
9908 Common::Assignment *t_ass =
9909 t->get_reference()->get_refd_assignment();
9910 if (template_refs.has_key(t_ass)) {
9911 // a reference to the same variable is also passed to the called
9912 // definition
9913 copy_needed = true;
9914 } else if (p_comptype || p_compself) {
9915 // the called definition has a 'runs on' clause so it can access
9916 // component variables
9917 switch (t_ass->get_asstype()) {
9918 case Common::Assignment::A_PAR_TEMPL_OUT:
9919 case Common::Assignment::A_PAR_TEMPL_INOUT:
9920 // the parameter may be an alias of a component variable
9921 copy_needed = true;
9922 break;
9923 case Common::Assignment::A_VAR_TEMPLATE:
9924 // copy is needed if t_ass is a component variable that is
9925 // visible by the called definition
9926 if (!t_ass->is_local()) copy_needed = true;
9927 /** \todo component type compatibility: check whether t_ass is
9928 * visible from p_comptype (otherwise copy is not needed) */
9929 default:
9930 break;
9931 }
9932 }
9933 }
9934 } }
9935 default:
9936 break;
9937 }
9938 }
9939
9940 if (use_runtime_2 && ActualPar::AP_REF == par->get_selection()) {
9941 // if the parameter references an element of a record of/set of, then
9942 // the record of object needs to know, so it doesn't delete the referenced
9943 // element
9944 Ref_base* ref = par->get_Ref();
9945 FieldOrArrayRefs* subrefs = ref->get_subrefs();
9946 if (subrefs != NULL) {
9947 Common::Assignment* ass = ref->get_refd_assignment();
9948 size_t ref_i;
9949 for (ref_i = 0; ref_i < subrefs->get_nof_refs(); ++ref_i) {
9950 FieldOrArrayRef* subref = subrefs->get_ref(ref_i);
9951 if (FieldOrArrayRef::ARRAY_REF == subref->get_type()) {
9952 // set the referenced index in each array in the subrefs
9953 expression_struct array_expr;
9954 Code::init_expr(&array_expr);
9955 // the array object's name contains the reference, followed by
9956 // the subrefs before the current array ref
9957 array_expr.expr = mcopystr(LazyParamData::in_lazy() ?
9958 LazyParamData::add_ref_genname(ass, ref->get_my_scope()).c_str() :
9959 ass->get_genname_from_scope(ref->get_my_scope()).c_str());
9960 if (ref_i > 0) {
9961 subrefs->generate_code(&array_expr, ass, ref_i);
9962 }
9963 expression_struct index_expr;
9964 Code::init_expr(&index_expr);
9965 subrefs->get_ref(ref_i)->get_val()->generate_code_expr(&index_expr);
9966 // insert any preambles the array object or the index might have
9967 if (array_expr.preamble != NULL) {
9968 expr->preamble = mputstr(expr->preamble, array_expr.preamble);
9969 expr->postamble = mputstr(expr->postamble, array_expr.preamble);
9970 }
9971 if (index_expr.preamble != NULL) {
9972 expr->preamble = mputstr(expr->preamble, index_expr.preamble);
9973 expr->postamble = mputstr(expr->postamble, index_expr.preamble);
9974 }
9975 // let the array object know that the index is referenced before
9976 // calling the function, and let it know that it's now longer
9977 // referenced after the function call (this is done with the help
9978 // of the RefdIndexHandler's constructor and destructor)
9979 string tmp_id = ref->get_my_scope()->get_scope_mod_gen()->get_temporary_id();
9980 expr->preamble = mputprintf(expr->preamble,
9981 "RefdIndexHandler %s(&%s, %s);\n",
9982 tmp_id.c_str(), array_expr.expr, index_expr.expr);
9983 // insert any postambles the array object or the index might have
9984 if (array_expr.postamble != NULL) {
9985 expr->preamble = mputstr(expr->preamble, array_expr.postamble);
9986 expr->postamble = mputstr(expr->postamble, array_expr.postamble);
9987 }
9988 if (index_expr.postamble != NULL) {
9989 expr->preamble = mputstr(expr->preamble, index_expr.postamble);
9990 expr->postamble = mputstr(expr->postamble, index_expr.postamble);
9991 }
9992 Code::free_expr(&array_expr);
9993 Code::free_expr(&index_expr);
9994 } // if (FieldOrArrayRef::ARRAY_REF == subref->get_type())
9995 } // for cycle
9996 } // if (subrefs != NULL)
9997 } // if (ActualPar::AP_REF == par->get_selection())
9998
9999 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());
10000 }
10001 value_refs.clear();
10002 template_refs.clear();
10003 }
10004
10005 char *ActualParList::rearrange_init_code(char *str, Common::Module* usage_mod)
10006 {
10007 for (size_t i = 0; i < params.size(); i++)
10008 str = params[i]->rearrange_init_code(str, usage_mod);
10009 return str;
10010 }
10011
10012 void ActualParList::dump(unsigned level) const
10013 {
10014 DEBUG(level, "actual parameter list: %lu parameters",
10015 (unsigned long) params.size());
10016 for (size_t i = 0; i < params.size(); i++)
10017 params[i]->dump(level + 1);
10018 }
10019 }
This page took 0.314765 seconds and 5 git commands to generate.