85568e2b974cae248384114f4fa2a41d63ffb1a7
[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* str = NULL;
2916 for (size_t i = 0; i < asss->get_nof_asss(); ++i) {
2917 Def_Type* def = dynamic_cast<Def_Type*>(asss->get_ass_byIndex(i));
2918 if (def != NULL) {
2919 Type* t = def->get_Type();
2920 if (!t->is_ref() && t->get_typetype() != Type::T_COMPONENT) {
2921 // don't generate code for subtypes
2922 if (t->get_typetype() != Type::T_SIGNATURE) {
2923 str = mputprintf(str,
2924 " %sif (!strcmp(p_var.type_name, \"%s\")) {\n"
2925 " ((const %s*)p_var.value)->log();\n"
2926 " }\n"
2927 , (str != NULL) ? "else " : ""
2928 , t->get_dispname().c_str(), t->get_genname_value(this).c_str());
2929 }
2930 if (t->get_typetype() != Type::T_PORT) {
2931 str = mputprintf(str,
2932 " %sif (!strcmp(p_var.type_name, \"%s template\")) {\n"
2933 " ((const %s_template*)p_var.value)->log();\n"
2934 " }\n"
2935 , (str != NULL) ? "else " : ""
2936 , t->get_dispname().c_str(), t->get_genname_value(this).c_str());
2937 }
2938 }
2939 }
2940 }
2941 if (str != NULL) {
2942 // don't generate an empty printing function
2943 output->header.class_defs = mputprintf(output->header.class_defs,
2944 "/* Debugger printing function for types declared in this module */\n\n"
2945 "extern CHARSTRING print_var_%s(const TTCN3_Debugger::variable_t& p_var);\n",
2946 get_modid().get_ttcnname().c_str());
2947 output->source.global_vars = mputprintf(output->source.global_vars,
2948 "\n/* Debugger printing function for types declared in this module */\n"
2949 "CHARSTRING print_var_%s(const TTCN3_Debugger::variable_t& p_var)\n"
2950 "{\n"
2951 " TTCN_Logger::begin_event_log2str();\n"
2952 "%s"
2953 " else {\n"
2954 " TTCN_Logger::log_event_str(\"<unrecognized value or template>\");\n"
2955 " }\n"
2956 " return TTCN_Logger::end_event_log2str();\n"
2957 "}\n", get_modid().get_ttcnname().c_str(), str);
2958 }
2959 }
2960
2961 // =================================
2962 // ===== Definition
2963 // =================================
2964
2965 string Definition::get_genname() const
2966 {
2967 if (!genname.empty()) return genname;
2968 else return id->get_name();
2969 }
2970
2971 namedbool Definition::has_implicit_omit_attr() const {
2972 if (w_attrib_path) {
2973 const vector<SingleWithAttrib>& real_attribs =
2974 w_attrib_path->get_real_attrib();
2975 for (size_t in = real_attribs.size(); in > 0; in--) {
2976 if (SingleWithAttrib::AT_OPTIONAL ==
2977 real_attribs[in-1]->get_attribKeyword()) {
2978 if ("implicit omit" ==
2979 real_attribs[in-1]->get_attribSpec().get_spec()) {
2980 return IMPLICIT_OMIT;
2981 } else if ("explicit omit" ==
2982 real_attribs[in-1]->get_attribSpec().get_spec()) {
2983 return NOT_IMPLICIT_OMIT;
2984 } // error reporting for other values is in chk_global_attrib
2985 }
2986 }
2987 }
2988 return NOT_IMPLICIT_OMIT;
2989 }
2990
2991 Definition::~Definition()
2992 {
2993 delete w_attrib_path;
2994 delete erroneous_attrs;
2995 }
2996
2997 void Definition::set_fullname(const string& p_fullname)
2998 {
2999 Common::Assignment::set_fullname(p_fullname);
3000 if (w_attrib_path) w_attrib_path->set_fullname(p_fullname + ".<attribpath>");
3001 if (erroneous_attrs) erroneous_attrs->set_fullname(p_fullname+".<erroneous_attributes>");
3002 }
3003
3004 bool Definition::is_local() const
3005 {
3006 if (!my_scope) FATAL_ERROR("Definition::is_local()");
3007 for (Scope *scope = my_scope; scope; scope = scope->get_parent_scope()) {
3008 if (dynamic_cast<StatementBlock*>(scope)) return true;
3009 }
3010 return false;
3011 }
3012
3013 bool Definition::chk_identical(Definition *)
3014 {
3015 FATAL_ERROR("Definition::chk_identical()");
3016 return false;
3017 }
3018
3019 void Definition::chk_erroneous_attr()
3020 {
3021 if (!w_attrib_path) return;
3022 const Ttcn::MultiWithAttrib* attribs = w_attrib_path->get_local_attrib();
3023 if (!attribs) return;
3024 for (size_t i = 0; i < attribs->get_nof_elements(); i++) {
3025 const Ttcn::SingleWithAttrib* act_attr = attribs->get_element(i);
3026 if (act_attr->get_attribKeyword()==Ttcn::SingleWithAttrib::AT_ERRONEOUS) {
3027 if (!use_runtime_2) {
3028 error("`erroneous' attributes can be used only with the Function Test Runtime");
3029 note("If you need negative testing use the -R flag when generating the makefile");
3030 return;
3031 }
3032 size_t nof_qualifiers = act_attr->get_attribQualifiers() ? act_attr->get_attribQualifiers()->get_nof_qualifiers() : 0;
3033 dynamic_array<Type*> refd_type_array(nof_qualifiers); // only the qualifiers pointing to existing fields will be added to erroneous_attrs objects
3034 if (nof_qualifiers==0) {
3035 act_attr->error("At least one qualifier must be specified for the `erroneous' attribute");
3036 } else {
3037 // check if qualifiers point to existing fields
3038 for (size_t qi=0; qi<nof_qualifiers; qi++) {
3039 Qualifier* act_qual = const_cast<Qualifier*>(act_attr->get_attribQualifiers()->get_qualifier(qi));
3040 act_qual->set_my_scope(get_my_scope());
3041 Type* field_type = get_Type()->get_field_type(act_qual, Type::EXPECTED_CONSTANT);
3042 if (field_type) {
3043 dynamic_array<size_t> subrefs_array;
3044 dynamic_array<Type*> type_array;
3045 bool valid_indexes = get_Type()->get_subrefs_as_array(act_qual, subrefs_array, type_array);
3046 if (!valid_indexes) field_type = NULL;
3047 if (act_qual->refers_to_string_element()) {
3048 act_qual->error("Reference to a string element cannot be used in this context");
3049 field_type = NULL;
3050 }
3051 }
3052 refd_type_array.add(field_type);
3053 }
3054 }
3055 // parse the attr. spec.
3056 ErroneousAttributeSpec* err_attr_spec = ttcn3_parse_erroneous_attr_spec_string(
3057 act_attr->get_attribSpec().get_spec().c_str(), act_attr->get_attribSpec());
3058 if (err_attr_spec) {
3059 if (!erroneous_attrs) erroneous_attrs = new ErroneousAttributes(get_Type());
3060 // attr.spec will be owned by erroneous_attrs object
3061 erroneous_attrs->add_spec(err_attr_spec);
3062 err_attr_spec->set_fullname(get_fullname());
3063 err_attr_spec->set_my_scope(get_my_scope());
3064 err_attr_spec->chk();
3065 // create qualifier - err.attr.spec. pairs
3066 for (size_t qi=0; qi<nof_qualifiers; qi++) {
3067 if (refd_type_array[qi] && (err_attr_spec->get_indicator()!=ErroneousAttributeSpec::I_INVALID)) {
3068 erroneous_attrs->add_pair(act_attr->get_attribQualifiers()->get_qualifier(qi), err_attr_spec);
3069 }
3070 }
3071 }
3072 }
3073 }
3074 if (erroneous_attrs) erroneous_attrs->chk();
3075 }
3076
3077 char* Definition::generate_code_str(char *str)
3078 {
3079 FATAL_ERROR("Definition::generate_code_str()");
3080 return str;
3081 }
3082
3083 void Definition::ilt_generate_code(ILT *)
3084 {
3085 FATAL_ERROR("Definition::ilt_generate_code()");
3086 }
3087
3088 char *Definition::generate_code_init_comp(char *str, Definition *)
3089 {
3090 FATAL_ERROR("Definition::generate_code_init_comp()");
3091 return str;
3092 }
3093
3094 void Definition::set_with_attr(MultiWithAttrib* p_attrib)
3095 {
3096 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
3097 w_attrib_path->set_with_attr(p_attrib);
3098 }
3099
3100 WithAttribPath* Definition::get_attrib_path()
3101 {
3102 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
3103 return w_attrib_path;
3104 }
3105
3106 void Definition::set_parent_path(WithAttribPath* p_path)
3107 {
3108 if (!w_attrib_path) w_attrib_path = new WithAttribPath();
3109 w_attrib_path->set_parent(p_path);
3110 }
3111
3112 void Definition::set_parent_group(Group* p_group)
3113 {
3114 if(parentgroup) // there would be a leak!
3115 FATAL_ERROR("Definition::set_parent_group()");
3116 parentgroup = p_group;
3117 }
3118
3119 Group* Definition::get_parent_group()
3120 {
3121 return parentgroup;
3122 }
3123
3124 void Definition::dump_internal(unsigned level) const
3125 {
3126 DEBUG(level, "Move along, nothing to see here");
3127 }
3128
3129 void Definition::dump(unsigned level) const
3130 {
3131 dump_internal(level);
3132 if (w_attrib_path) {
3133 MultiWithAttrib *attrib = w_attrib_path->get_with_attr();
3134 if (attrib) {
3135 DEBUG(level + 1, "Definition Attributes:");
3136 attrib->dump(level + 2);
3137 }
3138 }
3139 if (erroneous_attrs) erroneous_attrs->dump(level+1);
3140 }
3141
3142 // =================================
3143 // ===== Def_Type
3144 // =================================
3145
3146 Def_Type::Def_Type(Identifier *p_id, Type *p_type)
3147 : Definition(A_TYPE, p_id), type(p_type)
3148 {
3149 if(!p_type) FATAL_ERROR("Ttcn::Def_Type::Def_Type()");
3150 type->set_ownertype(Type::OT_TYPE_DEF, this);
3151 }
3152
3153 Def_Type::~Def_Type()
3154 {
3155 delete type;
3156 }
3157
3158 Def_Type *Def_Type::clone() const
3159 {
3160 FATAL_ERROR("Def_Type::clone");
3161 }
3162
3163 void Def_Type::set_fullname(const string& p_fullname)
3164 {
3165 Definition::set_fullname(p_fullname);
3166 type->set_fullname(p_fullname);
3167 }
3168
3169 void Def_Type::set_my_scope(Scope *p_scope)
3170 {
3171 bridgeScope.set_parent_scope(p_scope);
3172 bridgeScope.set_scopeMacro_name(id->get_dispname());
3173
3174 Definition::set_my_scope(&bridgeScope);
3175 type->set_my_scope(&bridgeScope);
3176
3177 }
3178
3179 Setting *Def_Type::get_Setting()
3180 {
3181 return get_Type();
3182 }
3183
3184 Type *Def_Type::get_Type()
3185 {
3186 chk();
3187 return type;
3188 }
3189
3190 void Def_Type::chk()
3191 {
3192 if (checked) return;
3193 checked = true;
3194 Error_Context cntxt(this, "In %s definition `%s'",
3195 type->get_typetype() == Type::T_SIGNATURE ? "signature" : "type",
3196 id->get_dispname().c_str());
3197 type->set_genname(get_genname());
3198 if (!semantic_check_only && type->get_typetype() == Type::T_COMPONENT) {
3199 // the prefix of embedded definitions must be set before the checking
3200 type->get_CompBody()->set_genname(get_genname() + "_component_");
3201 }
3202
3203 while (w_attrib_path) { // not a loop, but we can _break_ out of it
3204 w_attrib_path->chk_global_attrib();
3205 w_attrib_path->chk_no_qualif();
3206 if (type->get_typetype() != Type::T_ANYTYPE) break;
3207 // This is the anytype; it must be empty (we're about to add the fields)
3208 if (type->get_nof_comps() > 0) FATAL_ERROR("Def_Type::chk");
3209
3210 Ttcn::ExtensionAttributes *extattrs = parse_extattributes(w_attrib_path);
3211 if (extattrs == 0) break; // NULL means parsing error
3212
3213 size_t num_atrs = extattrs->size();
3214 for (size_t k = 0; k < num_atrs; ++k) {
3215 ExtensionAttribute &ea = extattrs->get(k);
3216 switch (ea.get_type()) {
3217 case ExtensionAttribute::ANYTYPELIST: {
3218 Types *anytypes = ea.get_types();
3219 // List of types to be converted into fields for the anytype.
3220 // Make sure scope is set on all types in the list.
3221 anytypes->set_my_scope(get_my_scope());
3222
3223 // Convert the list of types into field names for the anytype
3224 for (size_t i=0; i < anytypes->get_nof_types(); ++i) {
3225 Type *t = anytypes->extract_type_byIndex(i);
3226 // we are now the owner of the Type.
3227 if (t->get_typetype()==Type::T_ERROR) { // should we give up?
3228 delete t;
3229 continue;
3230 }
3231
3232 string field_name;
3233 const char* btn = Type::get_typename_builtin(t->get_typetype());
3234 if (btn) {
3235 field_name = btn;
3236 }
3237 else if (t->get_typetype() == Type::T_REFD) {
3238 // Extract the identifier
3239 Common::Reference *ref = t->get_Reference();
3240 Ttcn::Reference *tref = dynamic_cast<Ttcn::Reference*>(ref);
3241 if (!tref) FATAL_ERROR("Def_Type::chk, wrong kind of reference");
3242 const Common::Identifier *modid = tref->get_modid();
3243 if (modid) {
3244 ea.error("Qualified name '%s' cannot be added to the anytype",
3245 tref->get_dispname().c_str());
3246 delete t;
3247 continue;
3248 }
3249 field_name = tref->get_id()->get_ttcnname();
3250 }
3251 else {
3252 // Can't happen here
3253 FATAL_ERROR("Unexpected type %d", t->get_typetype());
3254 }
3255
3256 const string& at_field = anytype_field(field_name);
3257 Identifier *field_id = new Identifier(Identifier::ID_TTCN, at_field);
3258 CompField *cf = new CompField(field_id, t, false, 0);
3259 cf->set_location(ea);
3260 cf->set_fullname(get_fullname());
3261 type->add_comp(cf);
3262 } // next i
3263 delete anytypes;
3264 break; }
3265 default:
3266 w_attrib_path->get_with_attr()->error("Type def can only have anytype");
3267 break;
3268 } // switch
3269 } // next attribute
3270
3271 delete extattrs;
3272 break; // do not loop
3273 }
3274
3275 // Now we can check the type
3276 type->chk();
3277 type->chk_constructor_name(*id);
3278 if (id->get_ttcnname() == "address") type->chk_address();
3279 ReferenceChain refch(type, "While checking embedded recursions");
3280 type->chk_recursions(refch);
3281
3282 if (type->get_typetype()==Type::T_FUNCTION
3283 ||type->get_typetype()==Type::T_ALTSTEP
3284 ||type->get_typetype()==Type::T_TESTCASE) {
3285 // TR 922. This is a function/altstep/testcase reference.
3286 // Set this definition as the definition for the formal parameters.
3287 type->get_fat_parameters()->set_my_def(this);
3288 }
3289 }
3290
3291 void Def_Type::generate_code(output_struct *target, bool)
3292 {
3293 type->generate_code(target);
3294 if (type->get_typetype() == Type::T_COMPONENT) {
3295 // the C++ equivalents of embedded component element definitions must be
3296 // generated from outside Type::generate_code() because the function can
3297 // call itself recursively and create invalid (overlapped) initializer
3298 // sequences
3299 type->get_CompBody()->generate_code(target);
3300 }
3301 }
3302
3303 void Def_Type::generate_code(CodeGenHelper& cgh) {
3304 type->generate_code(cgh.get_outputstruct(get_Type()));
3305 if (type->get_typetype() == Type::T_COMPONENT) {
3306 // the C++ equivalents of embedded component element definitions must be
3307 // generated from outside Type::generate_code() because the function can
3308 // call itself recursively and create invalid (overlapped) initializer
3309 // sequences
3310 type->get_CompBody()->generate_code(cgh.get_current_outputstruct());
3311 }
3312 cgh.finalize_generation(get_Type());
3313 }
3314
3315
3316 void Def_Type::dump_internal(unsigned level) const
3317 {
3318 DEBUG(level, "Type def: %s @ %p", id->get_dispname().c_str(), (const void*)this);
3319 type->dump(level + 1);
3320 }
3321
3322 void Def_Type::set_with_attr(MultiWithAttrib* p_attrib)
3323 {
3324 if (!w_attrib_path) {
3325 w_attrib_path = new WithAttribPath();
3326 type->set_parent_path(w_attrib_path);
3327 }
3328 type->set_with_attr(p_attrib);
3329 }
3330
3331 WithAttribPath* Def_Type::get_attrib_path()
3332 {
3333 if (!w_attrib_path) {
3334 w_attrib_path = new WithAttribPath();
3335 type->set_parent_path(w_attrib_path);
3336 }
3337 return w_attrib_path;
3338 }
3339
3340 void Def_Type::set_parent_path(WithAttribPath* p_path)
3341 {
3342 if (!w_attrib_path) {
3343 w_attrib_path = new WithAttribPath();
3344 type->set_parent_path(w_attrib_path);
3345 }
3346 w_attrib_path->set_parent(p_path);
3347 }
3348
3349 // =================================
3350 // ===== Def_Const
3351 // =================================
3352
3353 Def_Const::Def_Const(Identifier *p_id, Type *p_type, Value *p_value)
3354 : Definition(A_CONST, p_id)
3355 {
3356 if (!p_type || !p_value) FATAL_ERROR("Ttcn::Def_Const::Def_Const()");
3357 type=p_type;
3358 type->set_ownertype(Type::OT_CONST_DEF, this);
3359 value=p_value;
3360 value_under_check=false;
3361 }
3362
3363 Def_Const::~Def_Const()
3364 {
3365 delete type;
3366 delete value;
3367 }
3368
3369 Def_Const *Def_Const::clone() const
3370 {
3371 FATAL_ERROR("Def_Const::clone");
3372 }
3373
3374 void Def_Const::set_fullname(const string& p_fullname)
3375 {
3376 Definition::set_fullname(p_fullname);
3377 type->set_fullname(p_fullname + ".<type>");
3378 value->set_fullname(p_fullname);
3379 }
3380
3381 void Def_Const::set_my_scope(Scope *p_scope)
3382 {
3383 Definition::set_my_scope(p_scope);
3384 type->set_my_scope(p_scope);
3385 value->set_my_scope(p_scope);
3386 }
3387
3388 Setting *Def_Const::get_Setting()
3389 {
3390 return get_Value();
3391 }
3392
3393 Type *Def_Const::get_Type()
3394 {
3395 chk();
3396 return type;
3397 }
3398
3399
3400 Value *Def_Const::get_Value()
3401 {
3402 chk();
3403 return value;
3404 }
3405
3406 void Def_Const::chk()
3407 {
3408 if(checked) {
3409 if (value_under_check) {
3410 error("Circular reference in constant definition `%s'",
3411 id->get_dispname().c_str());
3412 value_under_check = false; // only report the error once for this definition
3413 }
3414 return;
3415 }
3416 Error_Context cntxt(this, "In constant definition `%s'",
3417 id->get_dispname().c_str());
3418 type->set_genname(_T_, get_genname());
3419 type->chk();
3420 value->set_my_governor(type);
3421 type->chk_this_value_ref(value);
3422 checked=true;
3423 if (w_attrib_path) {
3424 w_attrib_path->chk_global_attrib(true);
3425 switch (type->get_type_refd_last()->get_typetype_ttcn3()) {
3426 case Type::T_SEQ_T:
3427 case Type::T_SET_T:
3428 case Type::T_CHOICE_T:
3429 // These types may have qualified attributes
3430 break;
3431 case Type::T_SEQOF: case Type::T_SETOF:
3432 break;
3433 default:
3434 w_attrib_path->chk_no_qualif();
3435 break;
3436 }
3437 }
3438 Type *t = type->get_type_refd_last();
3439 switch (t->get_typetype()) {
3440 case Type::T_PORT:
3441 error("Constant cannot be defined for port type `%s'",
3442 t->get_fullname().c_str());
3443 break;
3444 case Type::T_SIGNATURE:
3445 error("Constant cannot be defined for signature `%s'",
3446 t->get_fullname().c_str());
3447 break;
3448 default:
3449 value_under_check = true;
3450 type->chk_this_value(value, 0, Type::EXPECTED_CONSTANT, INCOMPLETE_ALLOWED,
3451 OMIT_NOT_ALLOWED, SUB_CHK, has_implicit_omit_attr());
3452 value_under_check = false;
3453 chk_erroneous_attr();
3454 if (erroneous_attrs) value->set_err_descr(erroneous_attrs->get_err_descr());
3455 {
3456 ReferenceChain refch(type, "While checking embedded recursions");
3457 value->chk_recursions(refch);
3458 }
3459 break;
3460 }
3461 if (!semantic_check_only) {
3462 value->set_genname_prefix("const_");
3463 value->set_genname_recursive(get_genname());
3464 value->set_code_section(GovernedSimple::CS_PRE_INIT);
3465 }
3466 }
3467
3468 bool Def_Const::chk_identical(Definition *p_def)
3469 {
3470 chk();
3471 p_def->chk();
3472 if (p_def->get_asstype() != A_CONST) {
3473 const char *dispname_str = id->get_dispname().c_str();
3474 error("Local definition `%s' is a constant, but the definition "
3475 "inherited from component type `%s' is a %s", dispname_str,
3476 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
3477 p_def->note("The inherited definition of `%s' is here", dispname_str);
3478 return false;
3479 }
3480 Def_Const *p_def_const = dynamic_cast<Def_Const*>(p_def);
3481 if (!p_def_const) FATAL_ERROR("Def_Const::chk_identical()");
3482 if (!type->is_identical(p_def_const->type)) {
3483 const char *dispname_str = id->get_dispname().c_str();
3484 type->error("Local constant `%s' has type `%s', but the constant "
3485 "inherited from component type `%s' has type `%s'", dispname_str,
3486 type->get_typename().c_str(),
3487 p_def_const->get_my_scope()->get_fullname().c_str(),
3488 p_def_const->type->get_typename().c_str());
3489 p_def_const->note("The inherited constant `%s' is here", dispname_str);
3490 return false;
3491 } else if (!(*value == *p_def_const->value)) {
3492 const char *dispname_str = id->get_dispname().c_str();
3493 value->error("Local constant `%s' and the constant inherited from "
3494 "component type `%s' have different values", dispname_str,
3495 p_def_const->get_my_scope()->get_fullname().c_str());
3496 p_def_const->note("The inherited constant `%s' is here", dispname_str);
3497 return false;
3498 } else return true;
3499 }
3500
3501 void Def_Const::generate_code(output_struct *target, bool)
3502 {
3503 type->generate_code(target);
3504 const_def cdef;
3505 Code::init_cdef(&cdef);
3506 type->generate_code_object(&cdef, value);
3507 cdef.init = update_location_object(cdef.init);
3508 cdef.init = value->generate_code_init(cdef.init,
3509 value->get_lhs_name().c_str());
3510 Code::merge_cdef(target, &cdef);
3511 Code::free_cdef(&cdef);
3512 }
3513
3514 void Def_Const::generate_code(Common::CodeGenHelper& cgh) {
3515 // constant definitions always go to its containing module
3516 generate_code(cgh.get_current_outputstruct());
3517 }
3518
3519 char *Def_Const::generate_code_str(char *str)
3520 {
3521 const string& t_genname = get_genname();
3522 const char *genname_str = t_genname.c_str();
3523 if (value->has_single_expr()) {
3524 // the value can be represented by a single C++ expression
3525 // the object is initialized by the constructor
3526 str = mputprintf(str, "%s %s(%s);\n",
3527 type->get_genname_value(my_scope).c_str(), genname_str,
3528 value->get_single_expr().c_str());
3529 } else {
3530 // use the default constructor
3531 str = mputprintf(str, "%s %s;\n",
3532 type->get_genname_value(my_scope).c_str(), genname_str);
3533 // the value is assigned using subsequent statements
3534 str = value->generate_code_init(str, genname_str);
3535 }
3536 if (debugger_active) {
3537 str = generate_code_debugger_add_var(str, this);
3538 }
3539 return str;
3540 }
3541
3542 void Def_Const::ilt_generate_code(ILT *ilt)
3543 {
3544 const string& t_genname = get_genname();
3545 const char *genname_str = t_genname.c_str();
3546 char*& def=ilt->get_out_def();
3547 char*& init=ilt->get_out_branches();
3548 def = mputprintf(def, "%s %s;\n", type->get_genname_value(my_scope).c_str(),
3549 genname_str);
3550 init = value->generate_code_init(init, genname_str);
3551 }
3552
3553 char *Def_Const::generate_code_init_comp(char *str, Definition *)
3554 {
3555 /* This function actually does nothing as \a this and \a base_defn are
3556 * exactly the same. */
3557 return str;
3558 }
3559
3560 void Def_Const::dump_internal(unsigned level) const
3561 {
3562 DEBUG(level, "Constant: %s @%p", id->get_dispname().c_str(), (const void*)this);
3563 type->dump(level + 1);
3564 value->dump(level + 1);
3565 }
3566
3567 // =================================
3568 // ===== Def_ExtConst
3569 // =================================
3570
3571 Def_ExtConst::Def_ExtConst(Identifier *p_id, Type *p_type)
3572 : Definition(A_EXT_CONST, p_id)
3573 {
3574 if (!p_type) FATAL_ERROR("Ttcn::Def_ExtConst::Def_ExtConst()");
3575 type = p_type;
3576 type->set_ownertype(Type::OT_CONST_DEF, this);
3577 usage_found = false;
3578 }
3579
3580 Def_ExtConst::~Def_ExtConst()
3581 {
3582 delete type;
3583 }
3584
3585 Def_ExtConst *Def_ExtConst::clone() const
3586 {
3587 FATAL_ERROR("Def_ExtConst::clone");
3588 }
3589
3590 void Def_ExtConst::set_fullname(const string& p_fullname)
3591 {
3592 Definition::set_fullname(p_fullname);
3593 type->set_fullname(p_fullname + ".<type>");
3594 }
3595
3596 void Def_ExtConst::set_my_scope(Scope *p_scope)
3597 {
3598 Definition::set_my_scope(p_scope);
3599 type->set_my_scope(p_scope);
3600 }
3601
3602 Type *Def_ExtConst::get_Type()
3603 {
3604 chk();
3605 return type;
3606 }
3607
3608 void Def_ExtConst::chk()
3609 {
3610 if(checked) return;
3611 Error_Context cntxt(this, "In external constant definition `%s'",
3612 id->get_dispname().c_str());
3613 type->set_genname(_T_, get_genname());
3614 type->chk();
3615 checked=true;
3616 Type *t = type->get_type_refd_last();
3617 switch (t->get_typetype()) {
3618 case Type::T_PORT:
3619 error("External constant cannot be defined for port type `%s'",
3620 t->get_fullname().c_str());
3621 break;
3622 case Type::T_SIGNATURE:
3623 error("External constant cannot be defined for signature `%s'",
3624 t->get_fullname().c_str());
3625 break;
3626 default:
3627 break;
3628 }
3629 if (w_attrib_path) {
3630 w_attrib_path->chk_global_attrib();
3631 switch (type->get_type_refd_last()->get_typetype()) {
3632 case Type::T_SEQ_T:
3633 case Type::T_SET_T:
3634 case Type::T_CHOICE_T:
3635 // These types may have qualified attributes
3636 break;
3637 case Type::T_SEQOF: case Type::T_SETOF:
3638 break;
3639 default:
3640 w_attrib_path->chk_no_qualif();
3641 break;
3642 }
3643 }
3644 }
3645
3646 void Def_ExtConst::generate_code(output_struct *target, bool)
3647 {
3648 type->generate_code(target);
3649 target->header.global_vars = mputprintf(target->header.global_vars,
3650 "extern const %s& %s;\n", type->get_genname_value(my_scope).c_str(),
3651 get_genname().c_str());
3652 }
3653
3654 void Def_ExtConst::generate_code(Common::CodeGenHelper& cgh) {
3655 // constant definitions always go to its containing module
3656 generate_code(cgh.get_current_outputstruct());
3657 }
3658
3659 void Def_ExtConst::dump_internal(unsigned level) const
3660 {
3661 DEBUG(level, "External constant: %s @ %p", id->get_dispname().c_str(), (const void*)this);
3662 type->dump(level + 1);
3663 }
3664
3665 // =================================
3666 // ===== Def_Modulepar
3667 // =================================
3668
3669 Def_Modulepar::Def_Modulepar(Identifier *p_id, Type *p_type, Value *p_defval)
3670 : Definition(A_MODULEPAR, p_id)
3671 {
3672 if (!p_type) FATAL_ERROR("Ttcn::Def_Modulepar::Def_Modulepar()");
3673 type = p_type;
3674 type->set_ownertype(Type::OT_MODPAR_DEF, this);
3675 def_value = p_defval;
3676 }
3677
3678 Def_Modulepar::~Def_Modulepar()
3679 {
3680 delete type;
3681 delete def_value;
3682 }
3683
3684 Def_Modulepar* Def_Modulepar::clone() const
3685 {
3686 FATAL_ERROR("Def_Modulepar::clone");
3687 }
3688
3689 void Def_Modulepar::set_fullname(const string& p_fullname)
3690 {
3691 Definition::set_fullname(p_fullname);
3692 type->set_fullname(p_fullname + ".<type>");
3693 if (def_value) def_value->set_fullname(p_fullname + ".<default_value>");
3694 }
3695
3696 void Def_Modulepar::set_my_scope(Scope *p_scope)
3697 {
3698 Definition::set_my_scope(p_scope);
3699 type->set_my_scope(p_scope);
3700 if (def_value) def_value->set_my_scope(p_scope);
3701 }
3702
3703 Type *Def_Modulepar::get_Type()
3704 {
3705 chk();
3706 return type;
3707 }
3708
3709 void Def_Modulepar::chk()
3710 {
3711 if(checked) return;
3712 Error_Context cntxt(this, "In module parameter definition `%s'",
3713 id->get_dispname().c_str());
3714 type->set_genname(_T_, get_genname());
3715 type->chk();
3716 if (w_attrib_path) {
3717 w_attrib_path->chk_global_attrib();
3718 switch (type->get_type_refd_last()->get_typetype()) {
3719 case Type::T_SEQ_T:
3720 case Type::T_SET_T:
3721 case Type::T_CHOICE_T:
3722 // These types may have qualified attributes
3723 break;
3724 case Type::T_SEQOF: case Type::T_SETOF:
3725 break;
3726 default:
3727 w_attrib_path->chk_no_qualif();
3728 break;
3729 }
3730 }
3731 map<Type*,void> type_chain;
3732 map<Type::typetype_t, void> not_allowed;
3733 not_allowed.add(Type::T_PORT, 0);
3734 Type *t = type->get_type_refd_last();
3735 // if the type is valid the original will be returned
3736 Type::typetype_t tt = t->search_for_not_allowed_type(type_chain, not_allowed);
3737 type_chain.clear();
3738 not_allowed.clear();
3739 switch (tt) {
3740 case Type::T_PORT:
3741 error("Type of module parameter cannot be or embed port type `%s'",
3742 t->get_fullname().c_str());
3743 break;
3744 case Type::T_SIGNATURE:
3745 error("Type of module parameter cannot be signature `%s'",
3746 t->get_fullname().c_str());
3747 break;
3748 case Type::T_FUNCTION:
3749 case Type::T_ALTSTEP:
3750 case Type::T_TESTCASE:
3751 if (t->get_fat_runs_on_self()) {
3752 error("Type of module parameter cannot be of function reference type"
3753 " `%s' which has runs on self clause", t->get_fullname().c_str());
3754 }
3755 break;
3756 default:
3757 #if defined(MINGW)
3758 checked = true;
3759 #else
3760 if (def_value) {
3761 Error_Context cntxt2(def_value, "In default value");
3762 def_value->set_my_governor(type);
3763 type->chk_this_value_ref(def_value);
3764 checked = true;
3765 type->chk_this_value(def_value, 0, Type::EXPECTED_CONSTANT, INCOMPLETE_ALLOWED,
3766 OMIT_NOT_ALLOWED, SUB_CHK, has_implicit_omit_attr());
3767 if (!semantic_check_only) {
3768 def_value->set_genname_prefix("modulepar_");
3769 def_value->set_genname_recursive(get_genname());
3770 def_value->set_code_section(GovernedSimple::CS_PRE_INIT);
3771 }
3772 } else checked = true;
3773 #endif
3774 break;
3775 }
3776 }
3777
3778 void Def_Modulepar::generate_code(output_struct *target, bool)
3779 {
3780 type->generate_code(target);
3781 const_def cdef;
3782 Code::init_cdef(&cdef);
3783 const string& t_genname = get_genname();
3784 const char *name = t_genname.c_str();
3785 type->generate_code_object(&cdef, my_scope, t_genname, "modulepar_", false);
3786 if (def_value) {
3787 cdef.init = update_location_object(cdef.init);
3788 cdef.init = def_value->generate_code_init(cdef.init, def_value->get_lhs_name().c_str());
3789 }
3790 Code::merge_cdef(target, &cdef);
3791 Code::free_cdef(&cdef);
3792
3793 if (has_implicit_omit_attr()) {
3794 target->functions.post_init = mputprintf(target->functions.post_init,
3795 "modulepar_%s.set_implicit_omit();\n", name);
3796 }
3797
3798 const char *dispname = id->get_dispname().c_str();
3799 target->functions.set_param = mputprintf(target->functions.set_param,
3800 "if (!strcmp(par_name, \"%s\")) {\n"
3801 "modulepar_%s.set_param(param);\n"
3802 "return TRUE;\n"
3803 "} else ", dispname, name);
3804 target->functions.get_param = mputprintf(target->functions.get_param,
3805 "if (!strcmp(par_name, \"%s\")) {\n"
3806 "return modulepar_%s.get_param(param_name);\n"
3807 "} else ", dispname, name);
3808
3809 if (target->functions.log_param) {
3810 // this is not the first modulepar
3811 target->functions.log_param = mputprintf(target->functions.log_param,
3812 "TTCN_Logger::log_event_str(\", %s := \");\n", dispname);
3813 } else {
3814 // this is the first modulepar
3815 target->functions.log_param = mputprintf(target->functions.log_param,
3816 "TTCN_Logger::log_event_str(\"%s := \");\n", dispname);
3817 }
3818 target->functions.log_param = mputprintf(target->functions.log_param,
3819 "%s.log();\n", name);
3820 }
3821
3822 void Def_Modulepar::generate_code(Common::CodeGenHelper& cgh) {
3823 // module parameter definitions always go to its containing module
3824 generate_code(cgh.get_current_outputstruct());
3825 }
3826
3827 void Def_Modulepar::dump_internal(unsigned level) const
3828 {
3829 DEBUG(level, "Module parameter: %s @ %p", id->get_dispname().c_str(), (const void*)this);
3830 type->dump(level + 1);
3831 if (def_value) def_value->dump(level + 1);
3832 else DEBUG(level + 1, "No default value");
3833 }
3834
3835 // =================================
3836 // ===== Def_Modulepar_Template
3837 // =================================
3838
3839 Def_Modulepar_Template::Def_Modulepar_Template(Identifier *p_id, Type *p_type, Template *p_deftmpl)
3840 : Definition(A_MODULEPAR_TEMP, p_id)
3841 {
3842 if (!p_type) FATAL_ERROR("Ttcn::Def_Modulepar_Template::Def_Modulepar_Template()");
3843 type = p_type;
3844 type->set_ownertype(Type::OT_MODPAR_DEF, this);
3845 def_template = p_deftmpl;
3846 }
3847
3848 Def_Modulepar_Template::~Def_Modulepar_Template()
3849 {
3850 delete type;
3851 delete def_template;
3852 }
3853
3854 Def_Modulepar_Template* Def_Modulepar_Template::clone() const
3855 {
3856 FATAL_ERROR("Def_Modulepar_Template::clone");
3857 }
3858
3859 void Def_Modulepar_Template::set_fullname(const string& p_fullname)
3860 {
3861 Definition::set_fullname(p_fullname);
3862 type->set_fullname(p_fullname + ".<type>");
3863 if (def_template) def_template->set_fullname(p_fullname + ".<default_template>");
3864 }
3865
3866 void Def_Modulepar_Template::set_my_scope(Scope *p_scope)
3867 {
3868 Definition::set_my_scope(p_scope);
3869 type->set_my_scope(p_scope);
3870 if (def_template) def_template->set_my_scope(p_scope);
3871 }
3872
3873 Type *Def_Modulepar_Template::get_Type()
3874 {
3875 chk();
3876 return type;
3877 }
3878
3879 void Def_Modulepar_Template::chk()
3880 {
3881 if(checked) return;
3882 Error_Context cntxt(this, "In template module parameter definition `%s'",
3883 id->get_dispname().c_str());
3884 if (w_attrib_path) {
3885 w_attrib_path->chk_global_attrib();
3886 switch (type->get_type_refd_last()->get_typetype()) {
3887 case Type::T_SEQ_T:
3888 case Type::T_SET_T:
3889 case Type::T_CHOICE_T:
3890 // These types may have qualified attributes
3891 break;
3892 case Type::T_SEQOF: case Type::T_SETOF:
3893 break;
3894 default:
3895 w_attrib_path->chk_no_qualif();
3896 break;
3897 }
3898 }
3899 type->set_genname(_T_, get_genname());
3900 type->chk();
3901 Type *t = type->get_type_refd_last();
3902 switch (t->get_typetype()) {
3903 case Type::T_PORT:
3904 error("Type of template module parameter cannot be port type `%s'",
3905 t->get_fullname().c_str());
3906 break;
3907 case Type::T_SIGNATURE:
3908 error("Type of template module parameter cannot be signature `%s'",
3909 t->get_fullname().c_str());
3910 break;
3911 case Type::T_FUNCTION:
3912 case Type::T_ALTSTEP:
3913 case Type::T_TESTCASE:
3914 if (t->get_fat_runs_on_self()) {
3915 error("Type of template module parameter cannot be of function reference type"
3916 " `%s' which has runs on self clause", t->get_fullname().c_str());
3917 }
3918 break;
3919 default:
3920 if (has_implicit_omit_attr()) {
3921 error("Implicit omit not supported for template module parameters");
3922 }
3923 #if defined(MINGW)
3924 checked = true;
3925 #else
3926 if (def_template) {
3927 Error_Context cntxt2(def_template, "In default template");
3928 def_template->set_my_governor(type);
3929 def_template->flatten(false);
3930 if (def_template->get_templatetype() == Template::CSTR_PATTERN &&
3931 type->get_type_refd_last()->get_typetype() == Type::T_USTR) {
3932 def_template->set_templatetype(Template::USTR_PATTERN);
3933 def_template->get_ustr_pattern()->set_pattern_type(
3934 PatternString::USTR_PATTERN);
3935 }
3936 type->chk_this_template_ref(def_template);
3937 checked = true;
3938 type->chk_this_template_generic(def_template, INCOMPLETE_ALLOWED,
3939 OMIT_ALLOWED, ANY_OR_OMIT_ALLOWED, SUB_CHK, has_implicit_omit_attr() ? IMPLICIT_OMIT : NOT_IMPLICIT_OMIT, 0);
3940 if (!semantic_check_only) {
3941 def_template->set_genname_prefix("modulepar_");
3942 def_template->set_genname_recursive(get_genname());
3943 def_template->set_code_section(GovernedSimple::CS_PRE_INIT);
3944 }
3945 } else checked = true;
3946 #endif
3947 break;
3948 }
3949 }
3950
3951 void Def_Modulepar_Template::generate_code(output_struct *target, bool)
3952 {
3953 type->generate_code(target);
3954 const_def cdef;
3955 Code::init_cdef(&cdef);
3956 const string& t_genname = get_genname();
3957 const char *name = t_genname.c_str();
3958 type->generate_code_object(&cdef, my_scope, t_genname, "modulepar_", true);
3959 if (def_template) {
3960 cdef.init = update_location_object(cdef.init);
3961 cdef.init = def_template->generate_code_init(cdef.init, def_template->get_lhs_name().c_str());
3962 }
3963 Code::merge_cdef(target, &cdef);
3964 Code::free_cdef(&cdef);
3965
3966 if (has_implicit_omit_attr()) {
3967 FATAL_ERROR("Def_Modulepar_Template::generate_code()");
3968 }
3969
3970 const char *dispname = id->get_dispname().c_str();
3971 target->functions.set_param = mputprintf(target->functions.set_param,
3972 "if (!strcmp(par_name, \"%s\")) {\n"
3973 "modulepar_%s.set_param(param);\n"
3974 "return TRUE;\n"
3975 "} else ", dispname, name);
3976 target->functions.get_param = mputprintf(target->functions.get_param,
3977 "if (!strcmp(par_name, \"%s\")) {\n"
3978 "return modulepar_%s.get_param(param_name);\n"
3979 "} else ", dispname, name);
3980
3981 if (target->functions.log_param) {
3982 // this is not the first modulepar
3983 target->functions.log_param = mputprintf(target->functions.log_param,
3984 "TTCN_Logger::log_event_str(\", %s := \");\n", dispname);
3985 } else {
3986 // this is the first modulepar
3987 target->functions.log_param = mputprintf(target->functions.log_param,
3988 "TTCN_Logger::log_event_str(\"%s := \");\n", dispname);
3989 }
3990 target->functions.log_param = mputprintf(target->functions.log_param,
3991 "%s.log();\n", name);
3992 }
3993
3994 void Def_Modulepar_Template::generate_code(Common::CodeGenHelper& cgh) {
3995 // module parameter definitions always go to its containing module
3996 generate_code(cgh.get_current_outputstruct());
3997 }
3998
3999 void Def_Modulepar_Template::dump_internal(unsigned level) const
4000 {
4001 DEBUG(level, "Module parameter: %s @ %p", id->get_dispname().c_str(), (const void*)this);
4002 type->dump(level + 1);
4003 if (def_template) def_template->dump(level + 1);
4004 else DEBUG(level + 1, "No default template");
4005 }
4006
4007 // =================================
4008 // ===== Def_Template
4009 // =================================
4010
4011 Def_Template::Def_Template(template_restriction_t p_template_restriction,
4012 Identifier *p_id, Type *p_type, FormalParList *p_fpl,
4013 Reference *p_derived_ref, Template *p_body)
4014 : Definition(A_TEMPLATE, p_id), type(p_type), fp_list(p_fpl),
4015 derived_ref(p_derived_ref), base_template(0), recurs_deriv_checked(false),
4016 body(p_body), template_restriction(p_template_restriction),
4017 gen_restriction_check(false)
4018 {
4019 if (!p_type || !p_body) FATAL_ERROR("Ttcn::Def_Template::Def_Template()");
4020 type->set_ownertype(Type::OT_TEMPLATE_DEF, this);
4021 if (fp_list) fp_list->set_my_def(this);
4022 }
4023
4024 Def_Template::~Def_Template()
4025 {
4026 delete type;
4027 delete fp_list;
4028 delete derived_ref;
4029 delete body;
4030 }
4031
4032 Def_Template *Def_Template::clone() const
4033 {
4034 FATAL_ERROR("Def_Template::clone");
4035 }
4036
4037 void Def_Template::set_fullname(const string& p_fullname)
4038 {
4039 Definition::set_fullname(p_fullname);
4040 type->set_fullname(p_fullname + ".<type>");
4041 if (fp_list) fp_list->set_fullname(p_fullname + ".<formal_par_list>");
4042 if (derived_ref)
4043 derived_ref->set_fullname(p_fullname + ".<derived_reference>");
4044 body->set_fullname(p_fullname);
4045 }
4046
4047 void Def_Template::set_my_scope(Scope *p_scope)
4048 {
4049 bridgeScope.set_parent_scope(p_scope);
4050 bridgeScope.set_scopeMacro_name(id->get_dispname());
4051
4052 Definition::set_my_scope(&bridgeScope);
4053 type->set_my_scope(&bridgeScope);
4054 if (derived_ref) derived_ref->set_my_scope(&bridgeScope);
4055 if (fp_list) {
4056 fp_list->set_my_scope(&bridgeScope);
4057 body->set_my_scope(fp_list);
4058 } else body->set_my_scope(&bridgeScope);
4059 }
4060
4061 Setting *Def_Template::get_Setting()
4062 {
4063 return get_Template();
4064 }
4065
4066 Type *Def_Template::get_Type()
4067 {
4068 if (!checked) chk();
4069 return type;
4070 }
4071
4072 Template *Def_Template::get_Template()
4073 {
4074 if (!checked) chk();
4075 return body;
4076 }
4077
4078 FormalParList *Def_Template::get_FormalParList()
4079 {
4080 if (!checked) chk();
4081 return fp_list;
4082 }
4083
4084 void Def_Template::chk()
4085 {
4086 if (checked) return;
4087 Error_Context cntxt(this, "In template definition `%s'",
4088 id->get_dispname().c_str());
4089 const string& t_genname = get_genname();
4090 type->set_genname(_T_, t_genname);
4091 type->chk();
4092 if (w_attrib_path) {
4093 w_attrib_path->chk_global_attrib(true);
4094 switch (type->get_type_refd_last()->get_typetype_ttcn3()) {
4095 case Type::T_SEQ_T:
4096 case Type::T_SET_T:
4097 case Type::T_CHOICE_T:
4098 // These types may have qualified attributes
4099 break;
4100 case Type::T_SEQOF: case Type::T_SETOF:
4101 break;
4102 default:
4103 w_attrib_path->chk_no_qualif();
4104 break;
4105 }
4106 }
4107 if (fp_list) {
4108 chk_default();
4109 fp_list->chk(asstype);
4110 if (local_scope) error("Parameterized local template `%s' not supported",
4111 id->get_dispname().c_str());
4112 }
4113
4114 // Merge the elements of "all from" into the list
4115 body->flatten(false);
4116
4117 body->set_my_governor(type);
4118
4119 if (body->get_templatetype() == Template::CSTR_PATTERN &&
4120 type->get_type_refd_last()->get_typetype() == Type::T_USTR) {
4121 body->set_templatetype(Template::USTR_PATTERN);
4122 body->get_ustr_pattern()->set_pattern_type(PatternString::USTR_PATTERN);
4123 }
4124
4125 type->chk_this_template_ref(body);
4126 checked = true;
4127 Type *t = type->get_type_refd_last();
4128 if (t->get_typetype() == Type::T_PORT) {
4129 error("Template cannot be defined for port type `%s'",
4130 t->get_fullname().c_str());
4131 }
4132 chk_modified();
4133 chk_recursive_derivation();
4134 type->chk_this_template_generic(body, INCOMPLETE_ALLOWED, OMIT_ALLOWED,
4135 ANY_OR_OMIT_ALLOWED, SUB_CHK,
4136 has_implicit_omit_attr() ? IMPLICIT_OMIT : NOT_IMPLICIT_OMIT, 0);
4137
4138 chk_erroneous_attr();
4139 if (erroneous_attrs) body->set_err_descr(erroneous_attrs->get_err_descr());
4140
4141 {
4142 ReferenceChain refch(type, "While checking embedded recursions");
4143 body->chk_recursions(refch);
4144 }
4145 if (template_restriction!=TR_NONE) {
4146 Error_Context ec(this, "While checking template restriction `%s'",
4147 Template::get_restriction_name(template_restriction));
4148 gen_restriction_check =
4149 body->chk_restriction("template definition", template_restriction, body);
4150 if (fp_list && template_restriction!=TR_PRESENT) {
4151 size_t nof_fps = fp_list->get_nof_fps();
4152 for (size_t i=0; i<nof_fps; i++) {
4153 FormalPar* fp = fp_list->get_fp_byIndex(i);
4154 // if formal par is not template then skip restriction checking,
4155 // templates can have only `in' parameters
4156 if (fp->get_asstype()!=A_PAR_TEMPL_IN) continue;
4157 template_restriction_t fp_tr = fp->get_template_restriction();
4158 switch (template_restriction) {
4159 case TR_VALUE:
4160 case TR_OMIT:
4161 switch (fp_tr) {
4162 case TR_VALUE:
4163 case TR_OMIT:
4164 // allowed
4165 break;
4166 case TR_PRESENT:
4167 fp->error("Formal parameter with template restriction `%s' "
4168 "not allowed here", Template::get_restriction_name(fp_tr));
4169 break;
4170 case TR_NONE:
4171 fp->error("Formal parameter without template restriction "
4172 "not allowed here");
4173 break;
4174 default:
4175 FATAL_ERROR("Ttcn::Def_Template::chk()");
4176 }
4177 break;
4178 default:
4179 FATAL_ERROR("Ttcn::Def_Template::chk()");
4180 }
4181 }
4182 }
4183 }
4184 if (!semantic_check_only) {
4185 if (fp_list) fp_list->set_genname(t_genname);
4186 body->set_genname_prefix("template_");
4187 body->set_genname_recursive(t_genname);
4188 body->set_code_section(fp_list ? GovernedSimple::CS_INLINE :
4189 GovernedSimple::CS_POST_INIT);
4190 }
4191
4192 }
4193
4194 void Def_Template::chk_default() const
4195 {
4196 if (!fp_list) FATAL_ERROR("Def_Template::chk_default()");
4197 if (!derived_ref) {
4198 if (fp_list->has_notused_defval())
4199 fp_list->error("Only modified templates are allowed to use the not "
4200 "used symbol (`-') as the default parameter");
4201 return;
4202 }
4203 Common::Assignment *ass = derived_ref->get_refd_assignment(false);
4204 if (!ass || ass->get_asstype() != A_TEMPLATE) return; // Work locally.
4205 Def_Template *base = dynamic_cast<Def_Template *>(ass);
4206 if (!base) FATAL_ERROR("Def_Template::chk_default()");
4207 FormalParList *base_fpl = base->get_FormalParList();
4208 size_t nof_base_fps = base_fpl ? base_fpl->get_nof_fps() : 0;
4209 size_t nof_local_fps = fp_list ? fp_list->get_nof_fps() : 0;
4210 size_t min_fps = nof_base_fps;
4211 if (nof_local_fps < nof_base_fps) min_fps = nof_local_fps;
4212 for (size_t i = 0; i < min_fps; i++) {
4213 FormalPar *base_fp = base_fpl->get_fp_byIndex(i);
4214 FormalPar *local_fp = fp_list->get_fp_byIndex(i);
4215 if (local_fp->has_notused_defval()) {
4216 if (base_fp->has_defval()) {
4217 local_fp->set_defval(base_fp->get_defval());
4218 } else {
4219 local_fp->error("Not used symbol (`-') doesn't have the "
4220 "corresponding default parameter in the "
4221 "base template");
4222 }
4223 }
4224 }
4225 // Additional parameters in the derived template with using the not used
4226 // symbol. TODO: Merge the loops.
4227 for (size_t i = nof_base_fps; i < nof_local_fps; i++) {
4228 FormalPar *local_fp = fp_list->get_fp_byIndex(i);
4229 if (local_fp->has_notused_defval())
4230 local_fp->error("Not used symbol (`-') doesn't have the "
4231 "corresponding default parameter in the "
4232 "base template");
4233 }
4234 }
4235
4236 void Def_Template::chk_modified()
4237 {
4238 if (!derived_ref) return;
4239 // Do not check the (non-existent) actual parameter list of the derived
4240 // reference against the formal parameter list of the base template.
4241 // According to TTCN-3 syntax the derived reference cannot have parameters
4242 // even if the base template is parameterized.
4243 Common::Assignment *ass = derived_ref->get_refd_assignment(false);
4244 // Checking the existence and type compatibility of the base template.
4245 if (!ass) return;
4246 if (ass->get_asstype() != A_TEMPLATE) {
4247 derived_ref->error("Reference to a template was expected in the "
4248 "`modifies' definition instead of %s",
4249 ass->get_description().c_str());
4250 return;
4251 }
4252 base_template = dynamic_cast<Def_Template*>(ass);
4253 if (!base_template) FATAL_ERROR("Def_Template::chk_modified()");
4254 Type *base_type = base_template->get_Type();
4255 TypeCompatInfo info_base(my_scope->get_scope_mod(), type, base_type, true,
4256 false, true);
4257 TypeChain l_chain_base;
4258 TypeChain r_chain_base;
4259 if (!type->is_compatible(base_type, &info_base, &l_chain_base,
4260 &r_chain_base)) {
4261 if (info_base.is_subtype_error()) {
4262 type->error("%s", info_base.get_subtype_error().c_str());
4263 } else
4264 if (!info_base.is_erroneous()) {
4265 type->error("The modified template has different type than base "
4266 "template `%s': `%s' was expected instead of `%s'",
4267 ass->get_fullname().c_str(),
4268 base_type->get_typename().c_str(),
4269 type->get_typename().c_str());
4270 } else {
4271 // Always use the format string.
4272 type->error("%s", info_base.get_error_str_str().c_str());
4273 }
4274 } else {
4275 if (info_base.needs_conversion())
4276 body->set_needs_conversion();
4277 }
4278 // Check for restriction.
4279 if (Template::is_less_restrictive(base_template->get_template_restriction(),
4280 template_restriction)) {
4281 error("The template restriction is not the same or more "
4282 "restrictive as of base template `%s'", ass->get_fullname().c_str());
4283 }
4284 // Checking formal parameter lists.
4285 FormalParList *base_fpl = base_template->get_FormalParList();
4286 size_t nof_base_fps = base_fpl ? base_fpl->get_nof_fps() : 0;
4287 size_t nof_local_fps = fp_list ? fp_list->get_nof_fps() : 0;
4288 size_t min_fps;
4289 if (nof_local_fps < nof_base_fps) {
4290 error("The modified template has fewer formal parameters than base "
4291 "template `%s': at least %lu parameter%s expected instead of %lu",
4292 ass->get_fullname().c_str(), (unsigned long)nof_base_fps,
4293 nof_base_fps > 1 ? "s were" : " was", (unsigned long)nof_local_fps);
4294 min_fps = nof_local_fps;
4295 } else min_fps = nof_base_fps;
4296
4297 for (size_t i = 0; i < min_fps; i++) {
4298 FormalPar *base_fp = base_fpl->get_fp_byIndex(i);
4299 FormalPar *local_fp = fp_list->get_fp_byIndex(i);
4300 Error_Context cntxt(local_fp, "In formal parameter #%lu",
4301 (unsigned long)(i + 1));
4302 // Check for parameter kind equivalence (value or template).
4303 if (base_fp->get_asstype() != local_fp->get_asstype())
4304 local_fp->error("The kind of parameter is not the same as in base "
4305 "template `%s': %s was expected instead of %s",
4306 ass->get_fullname().c_str(), base_fp->get_assname(),
4307 local_fp->get_assname());
4308 // Check for type compatibility.
4309 Type *base_fp_type = base_fp->get_Type();
4310 Type *local_fp_type = local_fp->get_Type();
4311 TypeCompatInfo info_par(my_scope->get_scope_mod(), base_fp_type,
4312 local_fp_type, true, false);
4313 TypeChain l_chain_par;
4314 TypeChain r_chain_par;
4315 if (!base_fp_type->is_compatible(local_fp_type, &info_par, &l_chain_par,
4316 &r_chain_par)) {
4317 if (info_par.is_subtype_error()) {
4318 local_fp_type->error("%s", info_par.get_subtype_error().c_str());
4319 } else
4320 if (!info_par.is_erroneous()) {
4321 local_fp_type->error("The type of parameter is not the same as in "
4322 "base template `%s': `%s' was expected instead "
4323 "of `%s'",
4324 ass->get_fullname().c_str(),
4325 base_fp_type->get_typename().c_str(),
4326 local_fp_type->get_typename().c_str());
4327 } else {
4328 local_fp_type->error("%s", info_par.get_error_str_str().c_str());
4329 }
4330 } else {
4331 if (info_par.needs_conversion())
4332 body->set_needs_conversion();
4333 }
4334 // Check for name equivalence.
4335 const Identifier& base_fp_id = base_fp->get_id();
4336 const Identifier& local_fp_id = local_fp->get_id();
4337 if (!(base_fp_id == local_fp_id))
4338 local_fp->error("The name of parameter is not the same as in base "
4339 "template `%s': `%s' was expected instead of `%s'",
4340 ass->get_fullname().c_str(),
4341 base_fp_id.get_dispname().c_str(),
4342 local_fp_id.get_dispname().c_str());
4343 // Check for restrictions: the derived must be same or more restrictive.
4344 if (base_fp->get_asstype()==local_fp->get_asstype() &&
4345 Template::is_less_restrictive(base_fp->get_template_restriction(),
4346 local_fp->get_template_restriction())) {
4347 local_fp->error("The restriction of parameter is not the same or more "
4348 "restrictive as in base template `%s'", ass->get_fullname().c_str());
4349 }
4350 }
4351 // Set the pointer to the body of base template.
4352 body->set_base_template(base_template->get_Template());
4353 }
4354
4355 void Def_Template::chk_recursive_derivation()
4356 {
4357 if (recurs_deriv_checked) return;
4358 if (base_template) {
4359 ReferenceChain refch(this, "While checking the chain of base templates");
4360 refch.add(get_fullname());
4361 for (Def_Template *iter = base_template; iter; iter = iter->base_template)
4362 {
4363 if (iter->recurs_deriv_checked) break;
4364 else if (refch.add(iter->get_fullname()))
4365 iter->recurs_deriv_checked = true;
4366 else break;
4367 }
4368 }
4369 recurs_deriv_checked = true;
4370 }
4371
4372 void Def_Template::generate_code(output_struct *target, bool)
4373 {
4374 type->generate_code(target);
4375 if (fp_list) {
4376 // Parameterized template. Generate code for a function which returns
4377 // a $(genname)_template and has the appropriate parameters.
4378 const string& t_genname = get_genname();
4379 const char *template_name = t_genname.c_str();
4380 const char *template_dispname = id->get_dispname().c_str();
4381 const string& type_genname = type->get_genname_template(my_scope);
4382 const char *type_genname_str = type_genname.c_str();
4383
4384 // assemble the function body first (this also determines which parameters
4385 // are never used)
4386 size_t nof_base_pars = 0;
4387 char* function_body = create_location_object(memptystr(), "TEMPLATE",
4388 template_dispname);
4389 if (debugger_active) {
4390 function_body = generate_code_debugger_function_init(function_body, this);
4391 }
4392 if (base_template) {
4393 // modified template
4394 function_body = mputprintf(function_body, "%s ret_val(%s",
4395 type_genname_str,
4396 base_template->get_genname_from_scope(my_scope).c_str());
4397 if (base_template->fp_list) {
4398 // the base template is also parameterized
4399 function_body = mputc(function_body, '(');
4400 nof_base_pars = base_template->fp_list->get_nof_fps();
4401 for (size_t i = 0; i < nof_base_pars; i++) {
4402 if (i > 0) function_body = mputstr(function_body, ", ");
4403 function_body = mputstr(function_body,
4404 fp_list->get_fp_byIndex(i)->get_id().get_name().c_str());
4405 }
4406 function_body = mputc(function_body, ')');
4407 }
4408 function_body = mputstr(function_body, ");\n");
4409 } else {
4410 // simple template
4411 function_body = mputprintf(function_body, "%s ret_val;\n",
4412 type_genname_str);
4413 }
4414 if (erroneous_attrs && erroneous_attrs->get_err_descr()) {
4415 function_body = erroneous_attrs->get_err_descr()->
4416 generate_code_str(function_body, string("ret_val"));
4417 }
4418 function_body = body->generate_code_init(function_body, "ret_val");
4419 if (template_restriction!=TR_NONE && gen_restriction_check)
4420 function_body = Template::generate_restriction_check_code(function_body,
4421 "ret_val", template_restriction);
4422 if (debugger_active) {
4423 function_body = mputstr(function_body,
4424 "ttcn3_debugger.set_return_value((TTCN_Logger::begin_event_log2str(), "
4425 "ret_val.log(), TTCN_Logger::end_event_log2str()));\n");
4426 }
4427 function_body = mputstr(function_body, "return ret_val;\n");
4428 // if the template modifies a parameterized template, then the inherited
4429 // formal parameters must always be displayed, otherwise generate a smart
4430 // formal parameter list (where the names of unused parameters are omitted)
4431 char *formal_par_list = fp_list->generate_code(memptystr(), nof_base_pars);
4432 fp_list->generate_code_defval(target);
4433
4434 target->header.function_prototypes =
4435 mputprintf(target->header.function_prototypes,
4436 "extern %s %s(%s);\n",
4437 type_genname_str, template_name, formal_par_list);
4438 target->source.function_bodies = mputprintf(target->source.function_bodies,
4439 "%s %s(%s)\n"
4440 "{\n"
4441 "%s"
4442 "}\n\n", type_genname_str, template_name, formal_par_list, function_body);
4443 Free(formal_par_list);
4444 Free(function_body);
4445 } else {
4446 // non-parameterized template
4447 const_def cdef;
4448 Code::init_cdef(&cdef);
4449 type->generate_code_object(&cdef, body);
4450 cdef.init = update_location_object(cdef.init);
4451 if (base_template) {
4452 // modified template
4453 if (base_template->my_scope->get_scope_mod_gen() ==
4454 my_scope->get_scope_mod_gen()) {
4455 // if the base template is in the same module its body has to be
4456 // initialized first
4457 cdef.init = base_template->body->generate_code_init(cdef.init,
4458 base_template->body->get_lhs_name().c_str());
4459 }
4460 if (use_runtime_2 && body->get_needs_conversion()) {
4461 Type *body_type = body->get_my_governor()->get_type_refd_last();
4462 Type *base_type = base_template->body->get_my_governor()
4463 ->get_type_refd_last();
4464 if (!body_type || !base_type)
4465 FATAL_ERROR("Def_Template::generate_code()");
4466 const string& tmp_id = body->get_temporary_id();
4467 const char *tmp_id_str = tmp_id.c_str();
4468 // base template initialization
4469 cdef.init = mputprintf(cdef.init,
4470 "%s %s;\n"
4471 "if (!%s(%s, %s)) TTCN_error(\"Values or templates of types `%s' "
4472 "and `%s' are not compatible at run-time\");\n"
4473 "%s = %s;\n",
4474 body_type->get_genname_template(my_scope).c_str(), tmp_id_str,
4475 TypeConv::get_conv_func(base_type, body_type, my_scope
4476 ->get_scope_mod()).c_str(), tmp_id_str, base_template
4477 ->get_genname_from_scope(my_scope).c_str(), base_type
4478 ->get_typename().c_str(), body_type->get_typename().c_str(),
4479 body->get_lhs_name().c_str(), tmp_id_str);
4480 } else {
4481 cdef.init = mputprintf(cdef.init, "%s = %s;\n",
4482 body->get_lhs_name().c_str(),
4483 base_template->get_genname_from_scope(my_scope).c_str());
4484 }
4485 }
4486 if (use_runtime_2 && TypeConv::needs_conv_refd(body))
4487 cdef.init = TypeConv::gen_conv_code_refd(cdef.init,
4488 body->get_lhs_name().c_str(), body);
4489 else
4490 cdef.init = body->generate_code_init(cdef.init,
4491 body->get_lhs_name().c_str());
4492 if (template_restriction != TR_NONE && gen_restriction_check)
4493 cdef.init = Template::generate_restriction_check_code(cdef.init,
4494 body->get_lhs_name().c_str(), template_restriction);
4495 target->header.global_vars = mputstr(target->header.global_vars,
4496 cdef.decl);
4497 target->source.global_vars = mputstr(target->source.global_vars,
4498 cdef.def);
4499 target->functions.post_init = mputstr(target->functions.post_init,
4500 cdef.init);
4501 Code::free_cdef(&cdef);
4502 }
4503 }
4504
4505 void Def_Template::generate_code(Common::CodeGenHelper& cgh) {
4506 generate_code(cgh.get_outputstruct(this));
4507 }
4508
4509 char *Def_Template::generate_code_str(char *str)
4510 {
4511 const string& t_genname = get_genname();
4512 const char *genname_str = t_genname.c_str();
4513 const string& type_genname = type->get_genname_template(my_scope);
4514 const char *type_genname_str = type_genname.c_str();
4515 if (fp_list) {
4516 const char *dispname_str = id->get_dispname().c_str();
4517 NOTSUPP("Code generation for parameterized local template `%s'",
4518 dispname_str);
4519 str = mputprintf(str, "/* NOT SUPPORTED: template %s */\n",
4520 dispname_str);
4521 } else {
4522 if (base_template) {
4523 // non-parameterized modified template
4524 if (use_runtime_2 && body->get_needs_conversion()) {
4525 Type *body_type = body->get_my_governor()->get_type_refd_last();
4526 Type *base_type = base_template->body->get_my_governor()
4527 ->get_type_refd_last();
4528 if (!body_type || !base_type)
4529 FATAL_ERROR("Def_Template::generate_code_str()");
4530 const string& tmp_id = body->get_temporary_id();
4531 const char *tmp_id_str = tmp_id.c_str();
4532 str = mputprintf(str,
4533 "%s %s;\n"
4534 "if (!%s(%s, %s)) TTCN_error(\"Values or templates of types `%s' "
4535 "and `%s' are not compatible at run-time\");\n"
4536 "%s %s(%s);\n",
4537 body_type->get_genname_template(my_scope).c_str(), tmp_id_str,
4538 TypeConv::get_conv_func(base_type, body_type, my_scope
4539 ->get_scope_mod()).c_str(), tmp_id_str, base_template
4540 ->get_genname_from_scope(my_scope).c_str(), base_type
4541 ->get_typename().c_str(), body_type->get_typename().c_str(),
4542 type_genname_str, genname_str, tmp_id_str);
4543 } else {
4544 // the object is initialized from the base template by the
4545 // constructor
4546 str = mputprintf(str, "%s %s(%s);\n", type_genname_str, genname_str,
4547 base_template->get_genname_from_scope(my_scope).c_str());
4548 }
4549 // the modified body is assigned in the subsequent statements
4550 str = body->generate_code_init(str, genname_str);
4551 } else {
4552 // non-parameterized non-modified template
4553 if (body->has_single_expr()) {
4554 // the object is initialized by the constructor
4555 str = mputprintf(str, "%s %s(%s);\n", type_genname_str,
4556 genname_str, body->get_single_expr(false).c_str());
4557 // make sure the template's code is not generated twice (TR: HU56425)
4558 body->set_code_generated();
4559 } else {
4560 // the default constructor is used
4561 str = mputprintf(str, "%s %s;\n", type_genname_str, genname_str);
4562 // the body is assigned in the subsequent statements
4563 str = body->generate_code_init(str, genname_str);
4564 }
4565 }
4566 if (template_restriction != TR_NONE && gen_restriction_check)
4567 str = Template::generate_restriction_check_code(str, genname_str,
4568 template_restriction);
4569 }
4570 if (debugger_active) {
4571 str = generate_code_debugger_add_var(str, this);
4572 }
4573 return str;
4574 }
4575
4576 void Def_Template::ilt_generate_code(ILT *ilt)
4577 {
4578 const string& t_genname = get_genname();
4579 const char *genname_str = t_genname.c_str();
4580 char*& def=ilt->get_out_def();
4581 char*& init=ilt->get_out_branches();
4582 if (fp_list) {
4583 const char *dispname_str = id->get_dispname().c_str();
4584 NOTSUPP("Code generation for parameterized local template `%s'",
4585 dispname_str);
4586 def = mputprintf(def, "/* NOT SUPPORTED: template %s */\n", dispname_str);
4587 init = mputprintf(init, "/* NOT SUPPORTED: template %s */\n",
4588 dispname_str);
4589 } else {
4590 // non-parameterized template
4591 // use the default constructor for initialization
4592 def = mputprintf(def, "%s %s;\n",
4593 type->get_genname_template(my_scope).c_str(), genname_str);
4594 if (base_template) {
4595 // copy the base template with an assignment
4596 init = mputprintf(init, "%s = %s;\n", genname_str,
4597 base_template->get_genname_from_scope(my_scope).c_str());
4598 }
4599 // finally assign the body
4600 init = body->generate_code_init(init, genname_str);
4601 if (template_restriction!=TR_NONE && gen_restriction_check)
4602 init = Template::generate_restriction_check_code(init, genname_str,
4603 template_restriction);
4604 }
4605 }
4606
4607 void Def_Template::dump_internal(unsigned level) const
4608 {
4609 DEBUG(level, "Template: %s", id->get_dispname().c_str());
4610 if (fp_list) fp_list->dump(level + 1);
4611 if (derived_ref)
4612 DEBUG(level + 1, "modifies: %s", derived_ref->get_dispname().c_str());
4613 if (template_restriction!=TR_NONE)
4614 DEBUG(level + 1, "restriction: %s",
4615 Template::get_restriction_name(template_restriction));
4616 type->dump(level + 1);
4617 body->dump(level + 1);
4618 }
4619
4620 // =================================
4621 // ===== Def_Var
4622 // =================================
4623
4624 Def_Var::Def_Var(Identifier *p_id, Type *p_type, Value *p_initial_value)
4625 : Definition(A_VAR, p_id), type(p_type), initial_value(p_initial_value)
4626 {
4627 if (!p_type) FATAL_ERROR("Ttcn::Def_Var::Def_Var()");
4628 type->set_ownertype(Type::OT_VAR_DEF, this);
4629 }
4630
4631 Def_Var::~Def_Var()
4632 {
4633 delete type;
4634 delete initial_value;
4635 }
4636
4637 Def_Var *Def_Var::clone() const
4638 {
4639 FATAL_ERROR("Def_Var::clone");
4640 }
4641
4642 void Def_Var::set_fullname(const string& p_fullname)
4643 {
4644 Definition::set_fullname(p_fullname);
4645 type->set_fullname(p_fullname + ".<type>");
4646 if (initial_value)
4647 initial_value->set_fullname(p_fullname + ".<initial_value>");
4648 }
4649
4650 void Def_Var::set_my_scope(Scope *p_scope)
4651 {
4652 Definition::set_my_scope(p_scope);
4653 type->set_my_scope(p_scope);
4654 if (initial_value) initial_value->set_my_scope(p_scope);
4655 }
4656
4657 Type *Def_Var::get_Type()
4658 {
4659 chk();
4660 return type;
4661 }
4662
4663 void Def_Var::chk()
4664 {
4665 if(checked) return;
4666 Error_Context cntxt(this, "In variable definition `%s'",
4667 id->get_dispname().c_str());
4668 type->set_genname(_T_, get_genname());
4669 type->chk();
4670 checked = true;
4671 Type *t = type->get_type_refd_last();
4672 switch (t->get_typetype()) {
4673 case Type::T_PORT:
4674 error("Variable cannot be defined for port type `%s'",
4675 t->get_fullname().c_str());
4676 break;
4677 case Type::T_SIGNATURE:
4678 error("Variable cannot be defined for signature `%s'",
4679 t->get_fullname().c_str());
4680 break;
4681 default:
4682 if (initial_value) {
4683 initial_value->set_my_governor(type);
4684 type->chk_this_value_ref(initial_value);
4685 type->chk_this_value(initial_value, this, is_local() ?
4686 Type::EXPECTED_DYNAMIC_VALUE : Type::EXPECTED_STATIC_VALUE,
4687 INCOMPLETE_ALLOWED, OMIT_NOT_ALLOWED, SUB_CHK);
4688 if (!semantic_check_only) {
4689 initial_value->set_genname_recursive(get_genname());
4690 initial_value->set_code_section(GovernedSimple::CS_INLINE);
4691 }
4692 }
4693 break;
4694 }
4695
4696 if (w_attrib_path) {
4697 w_attrib_path->chk_global_attrib();
4698 w_attrib_path->chk_no_qualif();
4699 }
4700 }
4701
4702 bool Def_Var::chk_identical(Definition *p_def)
4703 {
4704 chk();
4705 p_def->chk();
4706 if (p_def->get_asstype() != A_VAR) {
4707 const char *dispname_str = id->get_dispname().c_str();
4708 error("Local definition `%s' is a variable, but the definition "
4709 "inherited from component type `%s' is a %s", dispname_str,
4710 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
4711 p_def->note("The inherited definition of `%s' is here", dispname_str);
4712 return false;
4713 }
4714 Def_Var *p_def_var = dynamic_cast<Def_Var*>(p_def);
4715 if (!p_def_var) FATAL_ERROR("Def_Var::chk_identical()");
4716 if (!type->is_identical(p_def_var->type)) {
4717 const char *dispname_str = id->get_dispname().c_str();
4718 type->error("Local variable `%s' has type `%s', but the variable "
4719 "inherited from component type `%s' has type `%s'", dispname_str,
4720 type->get_typename().c_str(),
4721 p_def_var->get_my_scope()->get_fullname().c_str(),
4722 p_def_var->type->get_typename().c_str());
4723 p_def_var->note("The inherited variable `%s' is here", dispname_str);
4724 return false;
4725 }
4726 if (initial_value) {
4727 if (p_def_var->initial_value) {
4728 if (!initial_value->is_unfoldable() &&
4729 !p_def_var->initial_value->is_unfoldable() &&
4730 !(*initial_value == *p_def_var->initial_value)) {
4731 const char *dispname_str = id->get_dispname().c_str();
4732 initial_value->warning("Local variable `%s' and the variable "
4733 "inherited from component type `%s' have different initial values",
4734 dispname_str, p_def_var->get_my_scope()->get_fullname().c_str());
4735 p_def_var->note("The inherited variable `%s' is here", dispname_str);
4736 }
4737 } else {
4738 const char *dispname_str = id->get_dispname().c_str();
4739 initial_value->warning("Local variable `%s' has initial value, but "
4740 "the variable inherited from component type `%s' does not",
4741 dispname_str, p_def_var->get_my_scope()->get_fullname().c_str());
4742 p_def_var->note("The inherited variable `%s' is here", dispname_str);
4743 }
4744 } else if (p_def_var->initial_value) {
4745 const char *dispname_str = id->get_dispname().c_str();
4746 warning("Local variable `%s' does not have initial value, but the "
4747 "variable inherited from component type `%s' has", dispname_str,
4748 p_def_var->get_my_scope()->get_fullname().c_str());
4749 p_def_var->note("The inherited variable `%s' is here", dispname_str);
4750 }
4751 return true;
4752 }
4753
4754 void Def_Var::generate_code(output_struct *target, bool clean_up)
4755 {
4756 type->generate_code(target);
4757 const_def cdef;
4758 Code::init_cdef(&cdef);
4759 type->generate_code_object(&cdef, my_scope, get_genname(), 0, false);
4760 Code::merge_cdef(target, &cdef);
4761 Code::free_cdef(&cdef);
4762 if (initial_value) {
4763 target->functions.init_comp =
4764 initial_value->generate_code_init(target->functions.init_comp,
4765 initial_value->get_lhs_name().c_str());
4766 } else if (clean_up) { // No initial value.
4767 target->functions.init_comp = mputprintf(target->functions.init_comp,
4768 "%s.clean_up();\n", get_genname().c_str());
4769 }
4770 }
4771
4772 void Def_Var::generate_code(CodeGenHelper& cgh)
4773 {
4774 generate_code(cgh.get_outputstruct(this));
4775 }
4776
4777 char *Def_Var::generate_code_str(char *str)
4778 {
4779 const string& t_genname = get_genname();
4780 const char *genname_str = t_genname.c_str();
4781 if (initial_value && initial_value->has_single_expr()) {
4782 // the initial value can be represented by a single C++ expression
4783 // the object is initialized by the constructor
4784 str = mputprintf(str, "%s %s(%s);\n",
4785 type->get_genname_value(my_scope).c_str(), genname_str,
4786 initial_value->get_single_expr().c_str());
4787 } else {
4788 // use the default constructor
4789 str = mputprintf(str, "%s %s;\n",
4790 type->get_genname_value(my_scope).c_str(), genname_str);
4791 if (initial_value) {
4792 // the initial value is assigned using subsequent statements
4793 str = initial_value->generate_code_init(str, genname_str);
4794 }
4795 }
4796 if (debugger_active) {
4797 str = generate_code_debugger_add_var(str, this);
4798 }
4799 return str;
4800 }
4801
4802 void Def_Var::ilt_generate_code(ILT *ilt)
4803 {
4804 const string& t_genname = get_genname();
4805 const char *genname_str = t_genname.c_str();
4806 char*& def=ilt->get_out_def();
4807 char*& init=ilt->get_out_branches();
4808 def = mputprintf(def, "%s %s;\n", type->get_genname_value(my_scope).c_str(),
4809 genname_str);
4810 if (initial_value)
4811 init = initial_value->generate_code_init(init, genname_str);
4812 }
4813
4814 char *Def_Var::generate_code_init_comp(char *str, Definition *base_defn)
4815 {
4816 if (initial_value) {
4817 str = initial_value->generate_code_init(str,
4818 base_defn->get_genname_from_scope(my_scope).c_str());
4819 }
4820 return str;
4821 }
4822
4823 void Def_Var::dump_internal(unsigned level) const
4824 {
4825 DEBUG(level, "Variable %s", id->get_dispname().c_str());
4826 type->dump(level + 1);
4827 if (initial_value) initial_value->dump(level + 1);
4828 }
4829
4830 // =================================
4831 // ===== Def_Var_Template
4832 // =================================
4833
4834 Def_Var_Template::Def_Var_Template(Identifier *p_id, Type *p_type,
4835 Template *p_initial_value, template_restriction_t p_template_restriction)
4836 : Definition(A_VAR_TEMPLATE, p_id), type(p_type),
4837 initial_value(p_initial_value), template_restriction(p_template_restriction)
4838 {
4839 if (!p_type) FATAL_ERROR("Ttcn::Def_Var_Template::Def_Var_Template()");
4840 type->set_ownertype(Type::OT_VARTMPL_DEF, this);
4841 }
4842
4843 Def_Var_Template::~Def_Var_Template()
4844 {
4845 delete type;
4846 delete initial_value;
4847 }
4848
4849 Def_Var_Template *Def_Var_Template::clone() const
4850 {
4851 FATAL_ERROR("Def_Var_Template::clone");
4852 }
4853
4854 void Def_Var_Template::set_fullname(const string& p_fullname)
4855 {
4856 Definition::set_fullname(p_fullname);
4857 type->set_fullname(p_fullname + ".<type>");
4858 if (initial_value)
4859 initial_value->set_fullname(p_fullname + ".<initial_value>");
4860 }
4861
4862 void Def_Var_Template::set_my_scope(Scope *p_scope)
4863 {
4864 Definition::set_my_scope(p_scope);
4865 type->set_my_scope(p_scope);
4866 if (initial_value) initial_value->set_my_scope(p_scope);
4867 }
4868
4869 Type *Def_Var_Template::get_Type()
4870 {
4871 chk();
4872 return type;
4873 }
4874
4875 void Def_Var_Template::chk()
4876 {
4877 if(checked) return;
4878 Error_Context cntxt(this, "In template variable definition `%s'",
4879 id->get_dispname().c_str());
4880 type->set_genname(_T_, get_genname());
4881 type->chk();
4882 checked = true;
4883 Type *t = type->get_type_refd_last();
4884 if (t->get_typetype() == Type::T_PORT) {
4885 error("Template variable cannot be defined for port type `%s'",
4886 t->get_fullname().c_str());
4887 }
4888
4889 if (initial_value) {
4890 initial_value->set_my_governor(type);
4891 initial_value->flatten(false);
4892
4893 if (initial_value->get_templatetype() == Template::CSTR_PATTERN &&
4894 type->get_type_refd_last()->get_typetype() == Type::T_USTR) {
4895 initial_value->set_templatetype(Template::USTR_PATTERN);
4896 initial_value->get_ustr_pattern()->set_pattern_type(
4897 PatternString::USTR_PATTERN);
4898 }
4899
4900 type->chk_this_template_ref(initial_value);
4901 // temporary hack: to allow incomplete body as initial value
4902 // checking as a modified template, but without a base template
4903 type->chk_this_template_generic(initial_value, INCOMPLETE_ALLOWED,
4904 OMIT_ALLOWED, ANY_OR_OMIT_ALLOWED, SUB_CHK, IMPLICIT_OMIT, 0);
4905 gen_restriction_check =
4906 initial_value->chk_restriction("template variable definition",
4907 template_restriction, initial_value);
4908 if (!semantic_check_only) {
4909 initial_value->set_genname_recursive(get_genname());
4910 initial_value->set_code_section(GovernedSimple::CS_INLINE);
4911 }
4912 }
4913 if (w_attrib_path) {
4914 w_attrib_path->chk_global_attrib();
4915 w_attrib_path->chk_no_qualif();
4916 }
4917 }
4918
4919 bool Def_Var_Template::chk_identical(Definition *p_def)
4920 {
4921 chk();
4922 p_def->chk();
4923 if (p_def->get_asstype() != A_VAR_TEMPLATE) {
4924 const char *dispname_str = id->get_dispname().c_str();
4925 error("Local definition `%s' is a template variable, but the definition "
4926 "inherited from component type `%s' is a %s", dispname_str,
4927 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
4928 p_def->note("The inherited definition of `%s' is here", dispname_str);
4929 return false;
4930 }
4931 Def_Var_Template *p_def_var_template =
4932 dynamic_cast<Def_Var_Template*>(p_def);
4933 if (!p_def_var_template) FATAL_ERROR("Def_Var_Template::chk_identical()");
4934 if (!type->is_identical(p_def_var_template->type)) {
4935 const char *dispname_str = id->get_dispname().c_str();
4936 type->error("Local template variable `%s' has type `%s', but the "
4937 "template variable inherited from component type `%s' has type `%s'",
4938 dispname_str, type->get_typename().c_str(),
4939 p_def_var_template->get_my_scope()->get_fullname().c_str(),
4940 p_def_var_template->type->get_typename().c_str());
4941 p_def_var_template->note("The inherited template variable `%s' is here",
4942 dispname_str);
4943 return false;
4944 }
4945 if (initial_value) {
4946 if (!p_def_var_template->initial_value) {
4947 const char *dispname_str = id->get_dispname().c_str();
4948 initial_value->warning("Local template variable `%s' has initial "
4949 "value, but the template variable inherited from component type "
4950 "`%s' does not", dispname_str,
4951 p_def_var_template->get_my_scope()->get_fullname().c_str());
4952 p_def_var_template->note("The inherited template variable `%s' is here",
4953 dispname_str);
4954 }
4955 } else if (p_def_var_template->initial_value) {
4956 const char *dispname_str = id->get_dispname().c_str();
4957 warning("Local template variable `%s' does not have initial value, but "
4958 "the template variable inherited from component type `%s' has",
4959 dispname_str,
4960 p_def_var_template->get_my_scope()->get_fullname().c_str());
4961 p_def_var_template->note("The inherited template variable `%s' is here",
4962 dispname_str);
4963 }
4964 return true;
4965 }
4966
4967 void Def_Var_Template::generate_code(output_struct *target, bool clean_up)
4968 {
4969 type->generate_code(target);
4970 const_def cdef;
4971 Code::init_cdef(&cdef);
4972 type->generate_code_object(&cdef, my_scope, get_genname(), 0, true);
4973 Code::merge_cdef(target, &cdef);
4974 Code::free_cdef(&cdef);
4975 if (initial_value) {
4976 if (Common::Type::T_SEQOF == initial_value->get_my_governor()->get_typetype() ||
4977 Common::Type::T_ARRAY == initial_value->get_my_governor()->get_typetype()) {
4978 target->functions.init_comp = mputprintf(target->functions.init_comp,
4979 "%s.remove_all_permutations();\n", initial_value->get_lhs_name().c_str());
4980 }
4981 target->functions.init_comp =
4982 initial_value->generate_code_init(target->functions.init_comp,
4983 initial_value->get_lhs_name().c_str());
4984 if (template_restriction!=TR_NONE && gen_restriction_check)
4985 target->functions.init_comp = Template::generate_restriction_check_code(
4986 target->functions.init_comp, initial_value->get_lhs_name().c_str(),
4987 template_restriction);
4988 } else if (clean_up) { // No initial value.
4989 // Always reset component variables/variable templates on component
4990 // reinitialization. Fix for HM79493.
4991 target->functions.init_comp = mputprintf(target->functions.init_comp,
4992 "%s.clean_up();\n", get_genname().c_str());
4993 }
4994 }
4995
4996 void Def_Var_Template::generate_code(CodeGenHelper& cgh)
4997 {
4998 generate_code(cgh.get_outputstruct(this));
4999 }
5000
5001 char *Def_Var_Template::generate_code_str(char *str)
5002 {
5003 const string& t_genname = get_genname();
5004 const char *genname_str = t_genname.c_str();
5005 if (initial_value && initial_value->has_single_expr()) {
5006 // The initial value can be represented by a single C++ expression
5007 // the object is initialized by the constructor.
5008 str = mputprintf(str, "%s %s(%s);\n",
5009 type->get_genname_template(my_scope).c_str(), genname_str,
5010 initial_value->get_single_expr(false).c_str());
5011 } else {
5012 // Use the default constructor.
5013 str = mputprintf(str, "%s %s;\n",
5014 type->get_genname_template(my_scope).c_str(), genname_str);
5015 if (initial_value) {
5016 // The initial value is assigned using subsequent statements.
5017 if (use_runtime_2 && TypeConv::needs_conv_refd(initial_value))
5018 str = TypeConv::gen_conv_code_refd(str, genname_str, initial_value);
5019 else str = initial_value->generate_code_init(str, genname_str);
5020 }
5021 }
5022 if (initial_value && template_restriction != TR_NONE
5023 && gen_restriction_check)
5024 str = Template::generate_restriction_check_code(str, genname_str,
5025 template_restriction);
5026 if (debugger_active) {
5027 str = generate_code_debugger_add_var(str, this);
5028 }
5029 return str;
5030 }
5031
5032 void Def_Var_Template::ilt_generate_code(ILT *ilt)
5033 {
5034 const string& t_genname = get_genname();
5035 const char *genname_str = t_genname.c_str();
5036 char*& def=ilt->get_out_def();
5037 char*& init=ilt->get_out_branches();
5038 def = mputprintf(def, "%s %s;\n",
5039 type->get_genname_template(my_scope).c_str(), genname_str);
5040 if (initial_value) {
5041 init = initial_value->generate_code_init(init, genname_str);
5042 if (template_restriction!=TR_NONE && gen_restriction_check)
5043 init = Template::generate_restriction_check_code(init, genname_str,
5044 template_restriction);
5045 }
5046 }
5047
5048 char *Def_Var_Template::generate_code_init_comp(char *str,
5049 Definition *base_defn)
5050 {
5051 if (initial_value) {
5052 str = initial_value->generate_code_init(str,
5053 base_defn->get_genname_from_scope(my_scope).c_str());
5054 if (template_restriction != TR_NONE && gen_restriction_check)
5055 str = Template::generate_restriction_check_code(str,
5056 base_defn->get_genname_from_scope(my_scope).c_str(),
5057 template_restriction);
5058 }
5059 return str;
5060 }
5061
5062 void Def_Var_Template::dump_internal(unsigned level) const
5063 {
5064 DEBUG(level, "Template variable %s", id->get_dispname().c_str());
5065 if (template_restriction!=TR_NONE)
5066 DEBUG(level + 1, "restriction: %s",
5067 Template::get_restriction_name(template_restriction));
5068 type->dump(level + 1);
5069 if (initial_value) initial_value->dump(level + 1);
5070 }
5071
5072 // =================================
5073 // ===== Def_Timer
5074 // =================================
5075
5076 Def_Timer::~Def_Timer()
5077 {
5078 delete dimensions;
5079 delete default_duration;
5080 }
5081
5082 Def_Timer *Def_Timer::clone() const
5083 {
5084 FATAL_ERROR("Def_Timer::clone");
5085 }
5086
5087 void Def_Timer::set_fullname(const string& p_fullname)
5088 {
5089 Definition::set_fullname(p_fullname);
5090 if (dimensions) dimensions->set_fullname(p_fullname + ".<dimensions>");
5091 if (default_duration)
5092 default_duration->set_fullname(p_fullname + ".<default_duration>");
5093 }
5094
5095 void Def_Timer::set_my_scope(Scope *p_scope)
5096 {
5097 Definition::set_my_scope(p_scope);
5098 if (dimensions) dimensions->set_my_scope(p_scope);
5099 if (default_duration) default_duration->set_my_scope(p_scope);
5100 }
5101
5102 ArrayDimensions *Def_Timer::get_Dimensions()
5103 {
5104 if (!checked) chk();
5105 return dimensions;
5106 }
5107
5108 void Def_Timer::chk()
5109 {
5110 if(checked) return;
5111 Error_Context cntxt(this, "In timer definition `%s'",
5112 id->get_dispname().c_str());
5113 if (dimensions) dimensions->chk();
5114 if (default_duration) {
5115 Error_Context cntxt2(default_duration, "In default duration");
5116 if (dimensions) chk_array_duration(default_duration);
5117 else chk_single_duration(default_duration);
5118 if (!semantic_check_only) {
5119 default_duration->set_code_section(GovernedSimple::CS_POST_INIT);
5120 }
5121 }
5122 checked = true;
5123 if (w_attrib_path) {
5124 w_attrib_path->chk_global_attrib();
5125 w_attrib_path->chk_no_qualif();
5126 }
5127 }
5128
5129 bool Def_Timer::chk_identical(Definition *p_def)
5130 {
5131 chk();
5132 p_def->chk();
5133 if (p_def->get_asstype() != A_TIMER) {
5134 const char *dispname_str = id->get_dispname().c_str();
5135 error("Local definition `%s' is a timer, but the definition inherited "
5136 "from component type `%s' is a %s", dispname_str,
5137 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
5138 p_def->note("The inherited definition of `%s' is here", dispname_str);
5139 return false;
5140 }
5141 Def_Timer *p_def_timer = dynamic_cast<Def_Timer*>(p_def);
5142 if (!p_def_timer) FATAL_ERROR("Def_Timer::chk_identical()");
5143 if (dimensions) {
5144 if (p_def_timer->dimensions) {
5145 if (!dimensions->is_identical(p_def_timer->dimensions)) {
5146 const char *dispname_str = id->get_dispname().c_str();
5147 error("Local timer `%s' and the timer inherited from component type "
5148 "`%s' have different array dimensions", dispname_str,
5149 p_def_timer->get_my_scope()->get_fullname().c_str());
5150 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5151 return false;
5152 }
5153 } else {
5154 const char *dispname_str = id->get_dispname().c_str();
5155 error("Local definition `%s' is a timer array, but the definition "
5156 "inherited from component type `%s' is a single timer", dispname_str,
5157 p_def_timer->get_my_scope()->get_fullname().c_str());
5158 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5159 return false;
5160 }
5161 } else if (p_def_timer->dimensions) {
5162 const char *dispname_str = id->get_dispname().c_str();
5163 error("Local definition `%s' is a single timer, but the definition "
5164 "inherited from component type `%s' is a timer array", dispname_str,
5165 p_def_timer->get_my_scope()->get_fullname().c_str());
5166 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5167 return false;
5168 }
5169 if (default_duration) {
5170 if (p_def_timer->default_duration) {
5171 if (!default_duration->is_unfoldable() &&
5172 !p_def_timer->default_duration->is_unfoldable() &&
5173 !(*default_duration == *p_def_timer->default_duration)) {
5174 const char *dispname_str = id->get_dispname().c_str();
5175 default_duration->warning("Local timer `%s' and the timer inherited "
5176 "from component type `%s' have different default durations",
5177 dispname_str, p_def_timer->get_my_scope()->get_fullname().c_str());
5178 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5179 }
5180 } else {
5181 const char *dispname_str = id->get_dispname().c_str();
5182 default_duration->error("Local timer `%s' has default duration, but "
5183 "the timer inherited from component type `%s' does not", dispname_str,
5184 p_def_timer->get_my_scope()->get_fullname().c_str());
5185 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5186 return false;
5187 }
5188 } else if (p_def_timer->default_duration) {
5189 const char *dispname_str = id->get_dispname().c_str();
5190 error("Local timer `%s' does not have default duration, but the timer "
5191 "inherited from component type `%s' has", dispname_str,
5192 p_def_timer->get_my_scope()->get_fullname().c_str());
5193 p_def_timer->note("The inherited timer `%s' is here", dispname_str);
5194 return false;
5195 }
5196 return true;
5197 }
5198
5199 bool Def_Timer::has_default_duration(FieldOrArrayRefs *p_subrefs)
5200 {
5201 // return true in case of any uncertainity
5202 if (!default_duration) return false;
5203 else if (!dimensions || !p_subrefs) return true;
5204 Value *v = default_duration;
5205 size_t nof_dims = dimensions->get_nof_dims();
5206 size_t nof_refs = p_subrefs->get_nof_refs();
5207 size_t upper_limit = nof_dims < nof_refs ? nof_dims : nof_refs;
5208 for (size_t i = 0; i < upper_limit; i++) {
5209 v = v->get_value_refd_last();
5210 if (v->get_valuetype() != Value::V_SEQOF) break;
5211 FieldOrArrayRef *ref = p_subrefs->get_ref(i);
5212 if (ref->get_type() != FieldOrArrayRef::ARRAY_REF) return true;
5213 Value *v_index = ref->get_val()->get_value_refd_last();
5214 if (v_index->get_valuetype() != Value::V_INT) return true;
5215 Int index = v_index->get_val_Int()->get_val()
5216 - dimensions->get_dim_byIndex(i)->get_offset();
5217 if (index >= 0 && index < static_cast<Int>(v->get_nof_comps()))
5218 v = v->get_comp_byIndex(index);
5219 else return true;
5220 }
5221 return v->get_valuetype() != Value::V_NOTUSED;
5222 }
5223
5224 void Def_Timer::chk_single_duration(Value *dur)
5225 {
5226 dur->chk_expr_float(is_local() ?
5227 Type::EXPECTED_DYNAMIC_VALUE : Type::EXPECTED_STATIC_VALUE);
5228 Value *v = dur->get_value_refd_last();
5229 if (v->get_valuetype() == Value::V_REAL) {
5230 ttcn3float v_real = v->get_val_Real();
5231 if ( (v_real<0.0) || isSpecialFloatValue(v_real) ) {
5232 dur->error("A non-negative float value was expected "
5233 "as timer duration instead of `%s'", Real2string(v_real).c_str());
5234 }
5235 }
5236 }
5237
5238 void Def_Timer::chk_array_duration(Value *dur, size_t start_dim)
5239 {
5240 ArrayDimension *dim = dimensions->get_dim_byIndex(start_dim);
5241 bool array_size_known = !dim->get_has_error();
5242 size_t array_size = 0;
5243 if (array_size_known) array_size = dim->get_size();
5244 Value *v = dur->get_value_refd_last();
5245 switch (v->get_valuetype()) {
5246 case Value::V_ERROR:
5247 return;
5248 case Value::V_SEQOF: {
5249 size_t nof_vs = v->get_nof_comps();
5250 // Value-list notation.
5251 if (!v->is_indexed()) {
5252 if (array_size_known) {
5253 if (array_size > nof_vs) {
5254 dur->error("Too few elements in the default duration of timer "
5255 "array: %lu was expected instead of %lu",
5256 (unsigned long)array_size, (unsigned long)nof_vs);
5257 } else if (array_size < nof_vs) {
5258 dur->error("Too many elements in the default duration of timer "
5259 "array: %lu was expected instead of %lu",
5260 (unsigned long)array_size, (unsigned long)nof_vs);
5261 }
5262 }
5263 bool last_dimension = start_dim + 1 >= dimensions->get_nof_dims();
5264 for (size_t i = 0; i < nof_vs; i++) {
5265 Value *array_v = v->get_comp_byIndex(i);
5266 if (array_v->get_valuetype() == Value::V_NOTUSED) continue;
5267 if (last_dimension) chk_single_duration(array_v);
5268 else chk_array_duration(array_v, start_dim + 1);
5269 }
5270 } else {
5271 // Indexed-notation.
5272 bool last_dimension = start_dim + 1 >= dimensions->get_nof_dims();
5273 map<Int, Int> index_map;
5274 for (size_t i = 0; i < nof_vs; i++) {
5275 Value *array_v = v->get_comp_byIndex(i);
5276 if (array_v->get_valuetype() == Value::V_NOTUSED) continue;
5277 if (last_dimension) chk_single_duration(array_v);
5278 else chk_array_duration(array_v, start_dim + 1);
5279 Error_Context cntxt(this, "In timer array element %lu",
5280 (unsigned long)(i + 1));
5281 Value *index = v->get_index_byIndex(i);
5282 dim->chk_index(index, Type::EXPECTED_DYNAMIC_VALUE);
5283 if (index->get_value_refd_last()->get_valuetype() == Value::V_INT) {
5284 const int_val_t *index_int = index->get_value_refd_last()
5285 ->get_val_Int();
5286 if (*index_int > INT_MAX) {
5287 index->error("An integer value less than `%d' was expected for "
5288 "indexing timer array instead of `%s'", INT_MAX,
5289 (index_int->t_str()).c_str());
5290 index->set_valuetype(Value::V_ERROR);
5291 } else {
5292 Int index_val = index_int->get_val();
5293 if (index_map.has_key(index_val)) {
5294 index->error("Duplicate index value `%s' for timer array "
5295 "elements `%s' and `%s'",
5296 Int2string(index_val).c_str(),
5297 Int2string((Int)i + 1).c_str(),
5298 Int2string(*index_map[index_val]).c_str());
5299 index->set_valuetype(Value::V_ERROR);
5300 } else {
5301 index_map.add(index_val, new Int((Int)i + 1));
5302 }
5303 }
5304 }
5305 }
5306 // It's not possible to have "index_map.size() > array_size", since we
5307 // add only correct constant-index values into the map. It's possible
5308 // to create partially initialized timer arrays.
5309 for (size_t i = 0; i < index_map.size(); i++)
5310 delete index_map.get_nth_elem(i);
5311 index_map.clear();
5312 }
5313 break; }
5314 default:
5315 if (array_size_known) {
5316 dur->error("An array value (with %lu elements) was expected as "
5317 "default duration of timer array",
5318 (unsigned long)array_size);
5319 } else {
5320 dur->error("An array value was expected as default duration of timer "
5321 "array");
5322 }
5323 dur->set_valuetype(Value::V_ERROR);
5324 return;
5325 }
5326 }
5327
5328 void Def_Timer::generate_code(output_struct *target, bool)
5329 {
5330 const string& t_genname = get_genname();
5331 const char *genname_str = t_genname.c_str();
5332 const string& dispname = id->get_dispname();
5333 if (dimensions) {
5334 // timer array
5335 const string& array_type = dimensions->get_timer_type();
5336 const char *array_type_str = array_type.c_str();
5337 target->header.global_vars = mputprintf(target->header.global_vars,
5338 "extern %s %s;\n", array_type_str, genname_str);
5339 target->source.global_vars = mputprintf(target->source.global_vars,
5340 "%s %s;\n", array_type_str, genname_str);
5341 target->functions.pre_init = mputstr(target->functions.pre_init, "{\n"
5342 "static const char * const timer_name = \"");
5343 target->functions.pre_init = mputstr(target->functions.pre_init,
5344 dispname.c_str());
5345 target->functions.pre_init = mputprintf(target->functions.pre_init,
5346 "\";\n"
5347 "%s.set_name(timer_name);\n"
5348 "}\n", genname_str);
5349 if (default_duration) target->functions.post_init =
5350 generate_code_array_duration(target->functions.post_init, genname_str,
5351 default_duration);
5352 } else {
5353 // single timer
5354 target->header.global_vars = mputprintf(target->header.global_vars,
5355 "extern TIMER %s;\n", genname_str);
5356 if (default_duration) {
5357 // has default duration
5358 Value *v = default_duration->get_value_refd_last();
5359 if (v->get_valuetype() == Value::V_REAL) {
5360 // duration is known at compilation time -> set in the constructor
5361 target->source.global_vars = mputprintf(target->source.global_vars,
5362 "TIMER %s(\"%s\", %s);\n", genname_str, dispname.c_str(),
5363 v->get_single_expr().c_str());
5364 } else {
5365 // duration is known only at runtime -> set in post_init
5366 target->source.global_vars = mputprintf(target->source.global_vars,
5367 "TIMER %s(\"%s\");\n", genname_str, dispname.c_str());
5368 expression_struct expr;
5369 Code::init_expr(&expr);
5370 expr.expr = mputprintf(expr.expr, "%s.set_default_duration(",
5371 genname_str);
5372 default_duration->generate_code_expr(&expr);
5373 expr.expr = mputc(expr.expr, ')');
5374 target->functions.post_init =
5375 Code::merge_free_expr(target->functions.post_init, &expr);
5376 }
5377 } else {
5378 // does not have default duration
5379 target->source.global_vars = mputprintf(target->source.global_vars,
5380 "TIMER %s(\"%s\");\n", genname_str, dispname.c_str());
5381 }
5382 }
5383 }
5384
5385 void Def_Timer::generate_code(CodeGenHelper& cgh) {
5386 generate_code(cgh.get_current_outputstruct());
5387 }
5388
5389 char *Def_Timer::generate_code_array_duration(char *str,
5390 const char *object_name, Value *dur, size_t start_dim)
5391 {
5392 ArrayDimension *dim = dimensions->get_dim_byIndex(start_dim);
5393 size_t dim_size = dim->get_size();
5394 Value *v = dur->get_value_refd_last();
5395 if (v->get_valuetype() != Value::V_SEQOF
5396 || (v->get_nof_comps() != dim_size && !v->is_indexed()))
5397 FATAL_ERROR("Def_Timer::generate_code_array_duration()");
5398 // Value-list notation.
5399 if (!v->is_indexed()) {
5400 if (start_dim + 1 < dimensions->get_nof_dims()) {
5401 // There are more dimensions, the elements of "v" are arrays a
5402 // temporary reference shall be introduced if the next dimension has
5403 // more than 1 elements.
5404 bool temp_ref_needed =
5405 dimensions->get_dim_byIndex(start_dim + 1)->get_size() > 1;
5406 for (size_t i = 0; i < dim_size; i++) {
5407 Value *v_elem = v->get_comp_byIndex(i);
5408 if (v_elem->get_valuetype() == Value::V_NOTUSED) continue;
5409 if (temp_ref_needed) {
5410 const string& tmp_id = my_scope->get_scope_mod_gen()
5411 ->get_temporary_id();
5412 const char *tmp_str = tmp_id.c_str();
5413 str = mputprintf(str, "{\n"
5414 "%s& %s = %s.array_element(%lu);\n",
5415 dimensions->get_timer_type(start_dim + 1).c_str(),
5416 tmp_str, object_name, (unsigned long)i);
5417 str = generate_code_array_duration(str, tmp_str, v_elem,
5418 start_dim + 1);
5419 str = mputstr(str, "}\n");
5420 } else {
5421 char *tmp_str = mprintf("%s.array_element(%lu)", object_name,
5422 (unsigned long)i);
5423 str = generate_code_array_duration(str, tmp_str, v_elem,
5424 start_dim + 1);
5425 Free(tmp_str);
5426 }
5427 }
5428 } else {
5429 // We are in the last dimension, the elements of "v" are floats.
5430 for (size_t i = 0; i < dim_size; i++) {
5431 Value *v_elem = v->get_comp_byIndex(i);
5432 if (v_elem->get_valuetype() == Value::V_NOTUSED) continue;
5433 expression_struct expr;
5434 Code::init_expr(&expr);
5435 expr.expr = mputprintf(expr.expr,
5436 "%s.array_element(%lu).set_default_duration(",
5437 object_name, (unsigned long)i);
5438 v_elem->generate_code_expr(&expr);
5439 expr.expr = mputc(expr.expr, ')');
5440 str = Code::merge_free_expr(str, &expr);
5441 }
5442 }
5443 // Indexed-list notation.
5444 } else {
5445 if (start_dim + 1 < dimensions->get_nof_dims()) {
5446 bool temp_ref_needed =
5447 dimensions->get_dim_byIndex(start_dim + 1)->get_size() > 1;
5448 for (size_t i = 0; i < v->get_nof_comps(); i++) {
5449 Value *v_elem = v->get_comp_byIndex(i);
5450 if (v_elem->get_valuetype() == Value::V_NOTUSED) continue;
5451 if (temp_ref_needed) {
5452 const string& tmp_id = my_scope->get_scope_mod_gen()
5453 ->get_temporary_id();
5454 const string& idx_id = my_scope->get_scope_mod_gen()
5455 ->get_temporary_id();
5456 const char *tmp_str = tmp_id.c_str();
5457 str = mputstr(str, "{\n");
5458 str = mputprintf(str, "int %s;\n", idx_id.c_str());
5459 str = v->get_index_byIndex(i)->generate_code_init(str,
5460 idx_id.c_str());
5461 str = mputprintf(str, "%s& %s = %s.array_element(%s);\n",
5462 dimensions->get_timer_type(start_dim + 1).c_str(),
5463 tmp_str, object_name, idx_id.c_str());
5464 str = generate_code_array_duration(str, tmp_str, v_elem,
5465 start_dim + 1);
5466 str = mputstr(str, "}\n");
5467 } else {
5468 const string& idx_id = my_scope->get_scope_mod_gen()
5469 ->get_temporary_id();
5470 str = mputstr(str, "{\n");
5471 str = mputprintf(str, "int %s;\n", idx_id.c_str());
5472 str = v->get_index_byIndex(i)->generate_code_init(str,
5473 idx_id.c_str());
5474 char *tmp_str = mprintf("%s.array_element(%s)", object_name,
5475 idx_id.c_str());
5476 str = generate_code_array_duration(str, tmp_str, v_elem,
5477 start_dim + 1);
5478 str = mputstr(str, "}\n");
5479 Free(tmp_str);
5480 }
5481 }
5482 } else {
5483 for (size_t i = 0; i < v->get_nof_comps(); i++) {
5484 Value *v_elem = v->get_comp_byIndex(i);
5485 if (v_elem->get_valuetype() == Value::V_NOTUSED) continue;
5486 expression_struct expr;
5487 Code::init_expr(&expr);
5488 str = mputstr(str, "{\n");
5489 const string& idx_id = my_scope->get_scope_mod_gen()
5490 ->get_temporary_id();
5491 str = mputprintf(str, "int %s;\n", idx_id.c_str());
5492 str = v->get_index_byIndex(i)->generate_code_init(str,
5493 idx_id.c_str());
5494 str = mputprintf(str,
5495 "%s.array_element(%s).set_default_duration(",
5496 object_name, idx_id.c_str());
5497 v_elem->generate_code_expr(&expr);
5498 expr.expr = mputc(expr.expr, ')');
5499 str = Code::merge_free_expr(str, &expr);
5500 str = mputstr(str, "}\n");
5501 }
5502 }
5503 }
5504 return str;
5505 }
5506
5507 char *Def_Timer::generate_code_str(char *str)
5508 {
5509 const string& t_genname = get_genname();
5510 const char *genname_str = t_genname.c_str();
5511 const string& dispname = id->get_dispname();
5512 if (dimensions) {
5513 // timer array
5514 const string& array_type = dimensions->get_timer_type();
5515 const char *array_type_str = array_type.c_str();
5516 str = mputprintf(str, "%s %s;\n", array_type_str, genname_str);
5517 str = mputstr(str, "{\n"
5518 "static const char * const timer_name = \"");
5519 str = mputstr(str, dispname.c_str());
5520 str = mputprintf(str, "\";\n"
5521 "%s.set_name(timer_name);\n"
5522 "}\n", genname_str);
5523 if (default_duration) str = generate_code_array_duration(str,
5524 genname_str, default_duration);
5525 } else {
5526 // single timer
5527 if (default_duration && default_duration->has_single_expr()) {
5528 // the default duration can be passed to the constructor
5529 str = mputprintf(str, "TIMER %s(\"%s\", %s);\n", genname_str,
5530 dispname.c_str(), default_duration->get_single_expr().c_str());
5531 } else {
5532 // only the name is passed to the constructor
5533 str = mputprintf(str, "TIMER %s(\"%s\");\n", genname_str,
5534 dispname.c_str());
5535 if (default_duration) {
5536 // the default duration is set explicitly
5537 expression_struct expr;
5538 Code::init_expr(&expr);
5539 expr.expr = mputprintf(expr.expr, "%s.set_default_duration(",
5540 genname_str);
5541 default_duration->generate_code_expr(&expr);
5542 expr.expr = mputc(expr.expr, ')');
5543 str = Code::merge_free_expr(str, &expr);
5544 }
5545 }
5546 }
5547 if (debugger_active) {
5548 str = generate_code_debugger_add_var(str, this);
5549 }
5550 return str;
5551 }
5552
5553 void Def_Timer::ilt_generate_code(ILT *ilt)
5554 {
5555 const string& t_genname = get_genname();
5556 const char *genname_str = t_genname.c_str();
5557 const string& dispname = id->get_dispname();
5558
5559 char*& def = ilt->get_out_def();
5560 char*& init = ilt->get_out_branches();
5561
5562 if (dimensions) {
5563 // timer array
5564 const string& array_type = dimensions->get_timer_type();
5565 const char *array_type_str = array_type.c_str();
5566 def = mputprintf(def, "%s %s;\n", array_type_str, genname_str);
5567 def = mputstr(def, "{\n"
5568 "static const char * const timer_names[] = { ");
5569 def = dimensions->generate_element_names(def, dispname);
5570 def = mputprintf(def, " };\n"
5571 "%s.set_name(%lu, timer_names);\n"
5572 "}\n", genname_str, (unsigned long) dimensions->get_array_size());
5573 if (default_duration) init = generate_code_array_duration(init,
5574 genname_str, default_duration);
5575 } else {
5576 // single timer
5577 if (default_duration) {
5578 // has default duration
5579 Value *v = default_duration->get_value_refd_last();
5580 if (v->get_valuetype() == Value::V_REAL) {
5581 // duration is known at compilation time -> set in the constructor
5582 def = mputprintf(def, "TIMER %s(\"%s\", %s);\n", genname_str,
5583 dispname.c_str(), v->get_single_expr().c_str());
5584 } else {
5585 // duration is known only at runtime -> set when control reaches the
5586 // timer definition
5587 def = mputprintf(def, "TIMER %s(\"%s\");\n", genname_str,
5588 dispname.c_str());
5589 expression_struct expr;
5590 Code::init_expr(&expr);
5591 expr.expr = mputprintf(expr.expr, "%s.set_default_duration(",
5592 genname_str);
5593 default_duration->generate_code_expr(&expr);
5594 expr.expr = mputc(expr.expr, ')');
5595 init = Code::merge_free_expr(init, &expr);
5596 }
5597 } else {
5598 // does not have default duration
5599 def = mputprintf(def, "TIMER %s(\"%s\");\n", genname_str,
5600 dispname.c_str());
5601 }
5602 }
5603 }
5604
5605 char *Def_Timer::generate_code_init_comp(char *str, Definition *base_defn)
5606 {
5607 if (default_duration) {
5608 Def_Timer *base_timer_defn = dynamic_cast<Def_Timer*>(base_defn);
5609 if (!base_timer_defn || !base_timer_defn->default_duration)
5610 FATAL_ERROR("Def_Timer::generate_code_init_comp()");
5611 // initializer is not needed if the default durations are the same
5612 // constants in both timers
5613 if (default_duration->is_unfoldable() ||
5614 base_timer_defn->default_duration->is_unfoldable() ||
5615 !(*default_duration == *base_timer_defn->default_duration)) {
5616 if (dimensions) {
5617 str = generate_code_array_duration(str,
5618 base_timer_defn->get_genname_from_scope(my_scope).c_str(),
5619 default_duration);
5620 } else {
5621 expression_struct expr;
5622 Code::init_expr(&expr);
5623 expr.expr = mputprintf(expr.expr, "%s.set_default_duration(",
5624 base_timer_defn->get_genname_from_scope(my_scope).c_str());
5625 default_duration->generate_code_expr(&expr);
5626 expr.expr = mputc(expr.expr, ')');
5627 str = Code::merge_free_expr(str, &expr);
5628 }
5629 }
5630 }
5631 return str;
5632 }
5633
5634 void Def_Timer::dump_internal(unsigned level) const
5635 {
5636 DEBUG(level, "Timer: %s", id->get_dispname().c_str());
5637 if (dimensions) dimensions->dump(level + 1);
5638 if (default_duration) {
5639 DEBUG(level + 1, "Default duration:");
5640 default_duration->dump(level + 1);
5641 }
5642 }
5643
5644 // =================================
5645 // ===== Def_Port
5646 // =================================
5647
5648 Def_Port::Def_Port(Identifier *p_id, Reference *p_tref,
5649 ArrayDimensions *p_dims)
5650 : Definition(A_PORT, p_id), type_ref(p_tref), port_type(0),
5651 dimensions(p_dims)
5652 {
5653 if (!p_tref) FATAL_ERROR("Def_Port::Def_Port()");
5654 }
5655
5656 Def_Port::~Def_Port()
5657 {
5658 delete type_ref;
5659 delete dimensions;
5660 }
5661
5662 Def_Port *Def_Port::clone() const
5663 {
5664 FATAL_ERROR("Def_Port::clone");
5665 }
5666
5667 void Def_Port::set_fullname(const string& p_fullname)
5668 {
5669 Definition::set_fullname(p_fullname);
5670 type_ref->set_fullname(p_fullname + ".<type_ref>");
5671 if (dimensions) dimensions->set_fullname(p_fullname);
5672 }
5673
5674 void Def_Port::set_my_scope(Scope *p_scope)
5675 {
5676 Definition::set_my_scope(p_scope);
5677 type_ref->set_my_scope(p_scope);
5678 if (dimensions) dimensions->set_my_scope(p_scope);
5679 }
5680
5681 Type *Def_Port::get_Type()
5682 {
5683 chk();
5684 return port_type;
5685 }
5686
5687 ArrayDimensions *Def_Port::get_Dimensions()
5688 {
5689 if (!checked) chk();
5690 return dimensions;
5691 }
5692
5693 void Def_Port::chk()
5694 {
5695 if (checked) return;
5696 checked = true;
5697 Error_Context cntxt(this, "In port definition `%s'",
5698 id->get_dispname().c_str());
5699 Common::Assignment *ass = type_ref->get_refd_assignment();
5700 if (ass) {
5701 if (ass->get_asstype() == A_TYPE) {
5702 Type *t = ass->get_Type()->get_type_refd_last();
5703 if (t->get_typetype() == Type::T_PORT) port_type = t;
5704 else type_ref->error("Type reference `%s' does not refer to a "
5705 "port type", type_ref->get_dispname().c_str());
5706 } else type_ref->error("Reference `%s' does not refer to a "
5707 "type", type_ref->get_dispname().c_str());
5708 }
5709 if (dimensions) dimensions->chk();
5710 if (w_attrib_path) {
5711 w_attrib_path->chk_global_attrib();
5712 w_attrib_path->chk_no_qualif();
5713 }
5714 }
5715
5716 bool Def_Port::chk_identical(Definition *p_def)
5717 {
5718 chk();
5719 p_def->chk();
5720 if (p_def->get_asstype() != A_PORT) {
5721 const char *dispname_str = id->get_dispname().c_str();
5722 error("Local definition `%s' is a port, but the definition inherited "
5723 "from component type `%s' is a %s", dispname_str,
5724 p_def->get_my_scope()->get_fullname().c_str(), p_def->get_assname());
5725 p_def->note("The inherited definition of `%s' is here", dispname_str);
5726 return false;
5727 }
5728 Def_Port *p_def_port = dynamic_cast<Def_Port*>(p_def);
5729 if (!p_def_port) FATAL_ERROR("Def_Port::chk_identical()");
5730 if (port_type && p_def_port->port_type &&
5731 port_type != p_def_port->port_type) {
5732 const char *dispname_str = id->get_dispname().c_str();
5733 type_ref->error("Local port `%s' has type `%s', but the port inherited "
5734 "from component type `%s' has type `%s'", dispname_str,
5735 port_type->get_typename().c_str(),
5736 p_def_port->get_my_scope()->get_fullname().c_str(),
5737 p_def_port->port_type->get_typename().c_str());
5738 p_def_port->note("The inherited port `%s' is here", dispname_str);
5739 return false;
5740 }
5741 if (dimensions) {
5742 if (p_def_port->dimensions) {
5743 if (!dimensions->is_identical(p_def_port->dimensions)) {
5744 const char *dispname_str = id->get_dispname().c_str();
5745 error("Local port `%s' and the port inherited from component type "
5746 "`%s' have different array dimensions", dispname_str,
5747 p_def_port->get_my_scope()->get_fullname().c_str());
5748 p_def_port->note("The inherited port `%s' is here", dispname_str);
5749 return false;
5750 }
5751 } else {
5752 const char *dispname_str = id->get_dispname().c_str();
5753 error("Local definition `%s' is a port array, but the definition "
5754 "inherited from component type `%s' is a single port", dispname_str,
5755 p_def_port->get_my_scope()->get_fullname().c_str());
5756 p_def_port->note("The inherited port `%s' is here", dispname_str);
5757 return false;
5758 }
5759 } else if (p_def_port->dimensions) {
5760 const char *dispname_str = id->get_dispname().c_str();
5761 error("Local definition `%s' is a single port, but the definition "
5762 "inherited from component type `%s' is a port array", dispname_str,
5763 p_def_port->get_my_scope()->get_fullname().c_str());
5764 p_def_port->note("The inherited port `%s' is here", dispname_str);
5765 return false;
5766 }
5767 return true;
5768 }
5769
5770 void Def_Port::generate_code(output_struct *target, bool)
5771 {
5772 const string& t_genname = get_genname();
5773 const char *genname_str = t_genname.c_str();
5774 const string& type_genname = port_type->get_genname_value(my_scope);
5775 const string& dispname = id->get_dispname();
5776 if (dimensions) {
5777 // port array
5778 const string& array_type = dimensions->get_port_type(type_genname);
5779 const char *array_type_str = array_type.c_str();
5780 target->header.global_vars = mputprintf(target->header.global_vars,
5781 "extern %s %s;\n", array_type_str, genname_str);
5782 target->source.global_vars = mputprintf(target->source.global_vars,
5783 "%s %s;\n", array_type_str, genname_str);
5784 target->functions.pre_init = mputstr(target->functions.pre_init, "{\n"
5785 "static const char * const port_name = \"");
5786 target->functions.pre_init = mputstr(target->functions.pre_init,
5787 dispname.c_str());
5788 target->functions.pre_init = mputprintf(target->functions.pre_init,
5789 "\";\n"
5790 "%s.set_name(port_name);\n"
5791 "}\n", genname_str);
5792 } else {
5793 // single port
5794 const char *type_genname_str = type_genname.c_str();
5795 target->header.global_vars = mputprintf(target->header.global_vars,
5796 "extern %s %s;\n", type_genname_str, genname_str);
5797 target->source.global_vars = mputprintf(target->source.global_vars,
5798 "%s %s(\"%s\");\n", type_genname_str, genname_str, dispname.c_str());
5799 }
5800 target->functions.init_comp = mputprintf(target->functions.init_comp,
5801 "%s.activate_port();\n", genname_str);
5802 }
5803
5804 void Def_Port::generate_code(CodeGenHelper& cgh) {
5805 generate_code(cgh.get_current_outputstruct());
5806 }
5807
5808 char *Def_Port::generate_code_init_comp(char *str, Definition *base_defn)
5809 {
5810 return mputprintf(str, "%s.activate_port();\n",
5811 base_defn->get_genname_from_scope(my_scope).c_str());
5812 }
5813
5814 void Def_Port::dump_internal(unsigned level) const
5815 {
5816 DEBUG(level, "Port: %s", id->get_dispname().c_str());
5817 DEBUG(level + 1, "Port type:");
5818 type_ref->dump(level + 2);
5819 if (dimensions) dimensions->dump(level + 1);
5820 }
5821
5822 // =================================
5823 // ===== Def_Function_Base
5824 // =================================
5825
5826 Def_Function_Base::asstype_t Def_Function_Base::determine_asstype(
5827 bool is_external, bool has_return_type, bool returns_template)
5828 {
5829 if (is_external) {
5830 if (has_return_type) {
5831 if (returns_template) return A_EXT_FUNCTION_RTEMP;
5832 else return A_EXT_FUNCTION_RVAL;
5833 } else {
5834 if (returns_template)
5835 FATAL_ERROR("Def_Function_Base::determine_asstype()");
5836 return A_EXT_FUNCTION;
5837 }
5838 } else { // not an external function
5839 if (has_return_type) {
5840 if (returns_template) return A_FUNCTION_RTEMP;
5841 else return A_FUNCTION_RVAL;
5842 } else {
5843 if (returns_template)
5844 FATAL_ERROR("Def_Function_Base::determine_asstype()");
5845 return A_FUNCTION;
5846 }
5847 }
5848 }
5849
5850 Def_Function_Base::Def_Function_Base(const Def_Function_Base& p)
5851 : Definition(p), prototype(PROTOTYPE_NONE), input_type(0), output_type(0)
5852 {
5853 fp_list = p.fp_list->clone();
5854 fp_list->set_my_def(this);
5855 return_type = p.return_type ? p.return_type->clone() : 0;
5856 template_restriction = p.template_restriction;
5857 }
5858
5859 Def_Function_Base::Def_Function_Base(bool is_external, Identifier *p_id,
5860 FormalParList *p_fpl, Type *p_return_type, bool returns_template,
5861 template_restriction_t p_template_restriction)
5862 : Definition(determine_asstype(is_external, p_return_type != 0,
5863 returns_template), p_id), fp_list(p_fpl), return_type(p_return_type),
5864 prototype(PROTOTYPE_NONE), input_type(0), output_type(0),
5865 template_restriction(p_template_restriction)
5866 {
5867 if (!p_fpl) FATAL_ERROR("Def_Function_Base::Def_Function_Base()");
5868 fp_list->set_my_def(this);
5869 if (return_type) return_type->set_ownertype(Type::OT_FUNCTION_DEF, this);
5870 }
5871
5872 Def_Function_Base::~Def_Function_Base()
5873 {
5874 delete fp_list;
5875 delete return_type;
5876 }
5877
5878 void Def_Function_Base::set_fullname(const string& p_fullname)
5879 {
5880 Definition::set_fullname(p_fullname);
5881 fp_list->set_fullname(p_fullname + ".<formal_par_list>");
5882 if (return_type) return_type->set_fullname(p_fullname + ".<return_type>");
5883 }
5884
5885 void Def_Function_Base::set_my_scope(Scope *p_scope)
5886 {
5887 Definition::set_my_scope(p_scope);
5888 fp_list->set_my_scope(p_scope);
5889 if (return_type) return_type->set_my_scope(p_scope);
5890 }
5891
5892 Type *Def_Function_Base::get_Type()
5893 {
5894 if (!checked) chk();
5895 return return_type;
5896 }
5897
5898 FormalParList *Def_Function_Base::get_FormalParList()
5899 {
5900 if (!checked) chk();
5901 return fp_list;
5902 }
5903
5904 const char *Def_Function_Base::get_prototype_name() const
5905 {
5906 switch (prototype) {
5907 case PROTOTYPE_NONE:
5908 return "<no prototype>";
5909 case PROTOTYPE_CONVERT:
5910 return "convert";
5911 case PROTOTYPE_FAST:
5912 return "fast";
5913 case PROTOTYPE_BACKTRACK:
5914 return "backtrack";
5915 case PROTOTYPE_SLIDING:
5916 return "sliding";
5917 default:
5918 return "<unknown prototype>";
5919 }
5920 }
5921
5922 void Def_Function_Base::chk_prototype()
5923 {
5924 switch (prototype) {
5925 case PROTOTYPE_NONE:
5926 // return immediately
5927 return;
5928 case PROTOTYPE_CONVERT:
5929 case PROTOTYPE_FAST:
5930 case PROTOTYPE_BACKTRACK:
5931 case PROTOTYPE_SLIDING:
5932 // perform the checks below
5933 break;
5934 default:
5935 FATAL_ERROR("Def_Function_Base::chk_prototype()");
5936 }
5937 // checking the formal parameter list
5938 if (prototype == PROTOTYPE_CONVERT) {
5939 if (fp_list->get_nof_fps() == 1) {
5940 FormalPar *par = fp_list->get_fp_byIndex(0);
5941 if (par->get_asstype() == A_PAR_VAL_IN) {
5942 input_type = par->get_Type();
5943 } else {
5944 par->error("The parameter must be an `in' value parameter for "
5945 "attribute `prototype(%s)' instead of %s", get_prototype_name(),
5946 par->get_assname());
5947 }
5948 } else {
5949 fp_list->error("The function must have one parameter instead of %lu "
5950 "for attribute `prototype(%s)'", (unsigned long) fp_list->get_nof_fps(),
5951 get_prototype_name());
5952 }
5953 } else { // not PROTOTYPE_CONVERT
5954 if (fp_list->get_nof_fps() == 2) {
5955 FormalPar *first_par = fp_list->get_fp_byIndex(0);
5956 if (prototype == PROTOTYPE_SLIDING) {
5957 if (first_par->get_asstype() == A_PAR_VAL_INOUT) {
5958 Type *first_par_type = first_par->get_Type();
5959 switch (first_par_type->get_type_refd_last()
5960 ->get_typetype_ttcn3()) {
5961 case Type::T_ERROR:
5962 case Type::T_OSTR:
5963 case Type::T_CSTR:
5964 case Type::T_BSTR:
5965 input_type = first_par_type;
5966 break;
5967 default:
5968 first_par_type->error("The type of the first parameter must be "
5969 "`octetstring' or `charstring' or `bitstring' for attribute "
5970 "`prototype(%s)' instead of `%s'", get_prototype_name(),
5971 first_par_type->get_typename().c_str());
5972 }
5973 } else {
5974 first_par->error("The first parameter must be an `inout' value "
5975 "parameter for attribute `prototype(%s)' instead of %s",
5976 get_prototype_name(), first_par->get_assname());
5977 }
5978 } else {
5979 if (first_par->get_asstype() == A_PAR_VAL_IN) {
5980 input_type = first_par->get_Type();
5981 } else {
5982 first_par->error("The first parameter must be an `in' value "
5983 "parameter for attribute `prototype(%s)' instead of %s",
5984 get_prototype_name(), first_par->get_assname());
5985 }
5986 }
5987 FormalPar *second_par = fp_list->get_fp_byIndex(1);
5988 if (second_par->get_asstype() == A_PAR_VAL_OUT) {
5989 output_type = second_par->get_Type();
5990 } else {
5991 second_par->error("The second parameter must be an `out' value "
5992 "parameter for attribute `prototype(%s)' instead of %s",
5993 get_prototype_name(), second_par->get_assname());
5994 }
5995 } else {
5996 fp_list->error("The function must have two parameters for attribute "
5997 "`prototype(%s)' instead of %lu", get_prototype_name(),
5998 (unsigned long) fp_list->get_nof_fps());
5999 }
6000 }
6001 // checking the return type
6002 if (prototype == PROTOTYPE_FAST) {
6003 if (return_type) {
6004 return_type->error("The function cannot have return type for "
6005 "attribute `prototype(%s)'", get_prototype_name());
6006 }
6007 } else {
6008 if (return_type) {
6009 if (asstype == A_FUNCTION_RTEMP || asstype == A_EXT_FUNCTION_RTEMP)
6010 return_type->error("The function must return a value instead of a "
6011 "template for attribute `prototype(%s)'", get_prototype_name());
6012 if (prototype == PROTOTYPE_CONVERT) {
6013 output_type = return_type;
6014 } else {
6015 switch (return_type->get_type_refd_last()->get_typetype_ttcn3()) {
6016 case Type::T_ERROR:
6017 case Type::T_INT:
6018 break;
6019 default:
6020 return_type->error("The return type of the function must be "
6021 "`integer' instead of `%s' for attribute `prototype(%s)'",
6022 return_type->get_typename().c_str(), get_prototype_name());
6023 }
6024 }
6025 } else {
6026 error("The function must have return type for attribute "
6027 "`prototype(%s)'", get_prototype_name());
6028 }
6029 }
6030 // checking the 'runs on' clause
6031 if (get_RunsOnType()) {
6032 error("The function cannot have `runs on' clause for attribute "
6033 "`prototype(%s)'", get_prototype_name());
6034 }
6035 }
6036
6037 Type *Def_Function_Base::get_input_type()
6038 {
6039 if (!checked) chk();
6040 return input_type;
6041 }
6042
6043 Type *Def_Function_Base::get_output_type()
6044 {
6045 if (!checked) chk();
6046 return output_type;
6047 }
6048
6049
6050 // =================================
6051 // ===== Def_Function
6052 // =================================
6053
6054 Def_Function::Def_Function(Identifier *p_id, FormalParList *p_fpl,
6055 Reference *p_runs_on_ref, Type *p_return_type,
6056 bool returns_template,
6057 template_restriction_t p_template_restriction,
6058 StatementBlock *p_block)
6059 : Def_Function_Base(false, p_id, p_fpl, p_return_type, returns_template,
6060 p_template_restriction),
6061 runs_on_ref(p_runs_on_ref), runs_on_type(0), block(p_block),
6062 is_startable(false), transparent(false)
6063 {
6064 if (!p_block) FATAL_ERROR("Def_Function::Def_Function()");
6065 block->set_my_def(this);
6066 }
6067
6068 Def_Function::~Def_Function()
6069 {
6070 delete runs_on_ref;
6071 delete block;
6072 }
6073
6074 Def_Function *Def_Function::clone() const
6075 {
6076 FATAL_ERROR("Def_Function::clone");
6077 }
6078
6079 void Def_Function::set_fullname(const string& p_fullname)
6080 {
6081 Def_Function_Base::set_fullname(p_fullname);
6082 if (runs_on_ref) runs_on_ref->set_fullname(p_fullname + ".<runs_on_type>");
6083 block->set_fullname(p_fullname + ".<statement_block>");
6084 }
6085
6086 void Def_Function::set_my_scope(Scope *p_scope)
6087 {
6088 bridgeScope.set_parent_scope(p_scope);
6089 bridgeScope.set_scopeMacro_name(id->get_dispname());
6090
6091 Def_Function_Base::set_my_scope(&bridgeScope);
6092 if (runs_on_ref) runs_on_ref->set_my_scope(&bridgeScope);
6093 block->set_my_scope(fp_list);
6094 }
6095
6096 Type *Def_Function::get_RunsOnType()
6097 {
6098 if (!checked) chk();
6099 return runs_on_type;
6100 }
6101
6102 RunsOnScope *Def_Function::get_runs_on_scope(Type *comptype)
6103 {
6104 Module *my_module = dynamic_cast<Module*>(my_scope->get_scope_mod());
6105 if (!my_module) FATAL_ERROR("Def_Function::get_runs_on_scope()");
6106 return my_module->get_runs_on_scope(comptype);
6107 }
6108
6109 void Def_Function::chk()
6110 {
6111 if (checked) return;
6112 checked = true;
6113 Error_Context cntxt(this, "In function definition `%s'",
6114 id->get_dispname().c_str());
6115 // checking the `runs on' clause
6116 if (runs_on_ref) {
6117 Error_Context cntxt2(runs_on_ref, "In `runs on' clause");
6118 runs_on_type = runs_on_ref->chk_comptype_ref();
6119 // override the scope of the formal parameter list
6120 if (runs_on_type) {
6121 Scope *runs_on_scope = get_runs_on_scope(runs_on_type);
6122 runs_on_scope->set_parent_scope(my_scope);
6123 fp_list->set_my_scope(runs_on_scope);
6124 }
6125 }
6126 // checking the formal parameter list
6127 fp_list->chk(asstype);
6128 // checking of return type
6129 if (return_type) {
6130 Error_Context cntxt2(return_type, "In return type");
6131 return_type->chk();
6132 return_type->chk_as_return_type(asstype == A_FUNCTION_RVAL,"function");
6133 }
6134 // decision of startability
6135 is_startable = runs_on_ref != 0;
6136 if (is_startable && !fp_list->get_startability()) is_startable = false;
6137 if (is_startable && return_type && return_type->is_component_internal())
6138 is_startable = false;
6139 // checking of statement block
6140 block->chk();
6141 if (return_type) {
6142 // checking the presence of return statements
6143 switch (block->has_return()) {
6144 case StatementBlock::RS_NO:
6145 error("The function has return type, but it does not have any return "
6146 "statement");
6147 break;
6148 case StatementBlock::RS_MAYBE:
6149 error("The function has return type, but control might leave it "
6150 "without reaching a return statement");
6151 default:
6152 break;
6153 }
6154 }
6155 if (!semantic_check_only) {
6156 fp_list->set_genname(get_genname());
6157 block->set_code_section(GovernedSimple::CS_INLINE);
6158 }
6159 if (w_attrib_path) {
6160 w_attrib_path->chk_global_attrib();
6161 w_attrib_path->chk_no_qualif();
6162 Ttcn::ExtensionAttributes * extattrs = parse_extattributes(w_attrib_path);
6163 if (extattrs != 0) { // NULL means parsing error
6164 size_t num_atrs = extattrs->size();
6165 for (size_t i=0; i < num_atrs; ++i) {
6166 ExtensionAttribute &ea = extattrs->get(i);
6167 switch (ea.get_type()) {
6168 case ExtensionAttribute::PROTOTYPE: {
6169 if (get_prototype() != Def_Function_Base::PROTOTYPE_NONE) {
6170 ea.error("Duplicate attribute `prototype'");
6171 }
6172 Def_Function_Base::prototype_t proto = ea.get_proto();
6173 set_prototype(proto);
6174 break; }
6175
6176 case ExtensionAttribute::ANYTYPELIST: // ignore it
6177 case ExtensionAttribute::NONE: // erroneous, do not issue an error
6178 break;
6179
6180 case ExtensionAttribute::TRANSPARENT:
6181 transparent = true;
6182 break;
6183
6184 case ExtensionAttribute::ENCODE:
6185 case ExtensionAttribute::DECODE:
6186 case ExtensionAttribute::ERRORBEHAVIOR:
6187 case ExtensionAttribute::PRINTING:
6188 ea.error("Extension attribute 'encode', 'decode', 'errorbehavior'"
6189 " or 'printing' can only be applied to external functions");
6190 // fall through
6191
6192 default: // complain
6193 ea.error("Function definition can only have the 'prototype'"
6194 " extension attribute");
6195 break;
6196 }
6197 }
6198 delete extattrs;
6199 }
6200 }
6201 chk_prototype();
6202 }
6203
6204 bool Def_Function::chk_startable()
6205 {
6206 if (!checked) chk();
6207 if (is_startable) return true;
6208 if (!runs_on_ref) error("Function `%s' cannot be started on a parallel "
6209 "test component because it does not have `runs on' clause",
6210 get_fullname().c_str());
6211 fp_list->chk_startability("Function", get_fullname().c_str());
6212 if (return_type && return_type->is_component_internal()) {
6213 map<Type*,void> type_chain;
6214 char* err_str = mprintf("the return type or embedded in the return type "
6215 "of function `%s' if it is started on a parallel test component",
6216 get_fullname().c_str());
6217 return_type->chk_component_internal(type_chain, err_str);
6218 Free(err_str);
6219 }
6220 return false;
6221 }
6222
6223 void Def_Function::generate_code(output_struct *target, bool)
6224 {
6225 transparency_holder glass(*this);
6226 const string& t_genname = get_genname();
6227 const char *genname_str = t_genname.c_str();
6228 const char *dispname_str = id->get_dispname().c_str();
6229 string return_type_name;
6230 switch (asstype) {
6231 case A_FUNCTION:
6232 return_type_name = "void";
6233 break;
6234 case A_FUNCTION_RVAL:
6235 return_type_name = return_type->get_genname_value(my_scope);
6236 break;
6237 case A_FUNCTION_RTEMP:
6238 return_type_name = return_type->get_genname_template(my_scope);
6239 break;
6240 default:
6241 FATAL_ERROR("Def_Function::generate_code()");
6242 }
6243 const char *return_type_str = return_type_name.c_str();
6244
6245 // assemble the function body first (this also determines which parameters
6246 // are never used)
6247 char* body = create_location_object(memptystr(), "FUNCTION", dispname_str);
6248 if (!enable_set_bound_out_param)
6249 body = fp_list->generate_code_set_unbound(body); // conform the standard out parameter is unbound
6250 body = fp_list->generate_shadow_objects(body);
6251 if (debugger_active) {
6252 body = generate_code_debugger_function_init(body, this);
6253 }
6254 body = block->generate_code(body);
6255 // smart formal parameter list (names of unused parameters are omitted)
6256 char *formal_par_list = fp_list->generate_code(memptystr());
6257 fp_list->generate_code_defval(target);
6258 // function prototype
6259 target->header.function_prototypes =
6260 mputprintf(target->header.function_prototypes, "extern %s %s(%s);\n",
6261 return_type_str, genname_str, formal_par_list);
6262
6263 // function body
6264 target->source.function_bodies = mputprintf(target->source.function_bodies,
6265 "%s %s(%s)\n"
6266 "{\n"
6267 "%s"
6268 "}\n\n", return_type_str, genname_str, formal_par_list, body);
6269 Free(formal_par_list);
6270 Free(body);
6271
6272 if (is_startable) {
6273 size_t nof_fps = fp_list->get_nof_fps();
6274 // use the full list of formal parameters here (since they are all logged)
6275 char *full_formal_par_list = fp_list->generate_code(memptystr(), nof_fps);
6276 // starter function (stub)
6277 // function prototype
6278 target->header.function_prototypes =
6279 mputprintf(target->header.function_prototypes,
6280 "extern void start_%s(const COMPONENT& component_reference%s%s);\n",
6281 genname_str, nof_fps>0?", ":"", full_formal_par_list);
6282 // function body
6283 body = mprintf("void start_%s(const COMPONENT& component_reference%s"
6284 "%s)\n"
6285 "{\n"
6286 "TTCN_Logger::begin_event(TTCN_Logger::PARALLEL_PTC);\n"
6287 "TTCN_Logger::log_event_str(\"Starting function %s(\");\n",
6288 genname_str, nof_fps>0?", ":"", full_formal_par_list, dispname_str);
6289 for (size_t i = 0; i < nof_fps; i++) {
6290 if (i > 0) body = mputstr(body,
6291 "TTCN_Logger::log_event_str(\", \");\n");
6292 body = mputprintf(body, "%s.log();\n",
6293 fp_list->get_fp_byIndex(i)->get_reference_name(my_scope).c_str());
6294 }
6295 body = mputprintf(body,
6296 "TTCN_Logger::log_event_str(\") on component \");\n"
6297 "component_reference.log();\n"
6298 "TTCN_Logger::log_char('.');\n"
6299 "TTCN_Logger::end_event();\n"
6300 "Text_Buf text_buf;\n"
6301 "TTCN_Runtime::prepare_start_component(component_reference, "
6302 "\"%s\", \"%s\", text_buf);\n",
6303 my_scope->get_scope_mod()->get_modid().get_dispname().c_str(),
6304 dispname_str);
6305 for (size_t i = 0; i < nof_fps; i++) {
6306 body = mputprintf(body, "%s.encode_text(text_buf);\n",
6307 fp_list->get_fp_byIndex(i)->get_reference_name(my_scope).c_str());
6308 }
6309 body = mputstr(body, "TTCN_Runtime::send_start_component(text_buf);\n"
6310 "}\n\n");
6311 target->source.function_bodies = mputstr(target->source.function_bodies,
6312 body);
6313 Free(body);
6314
6315 // an entry in start_ptc_function
6316 body = mprintf("if (!strcmp(function_name, \"%s\")) {\n",
6317 dispname_str);
6318 if (nof_fps > 0) {
6319 body = fp_list->generate_code_object(body, "", ' ');
6320 for (size_t i = 0; i < nof_fps; i++) {
6321 body = mputprintf(body, "%s.decode_text(function_arguments);\n",
6322 fp_list->get_fp_byIndex(i)->get_reference_name(my_scope).c_str());
6323 }
6324 body = mputprintf(body,
6325 "TTCN_Logger::begin_event(TTCN_Logger::PARALLEL_PTC);\n"
6326 "TTCN_Logger::log_event_str(\"Starting function %s(\");\n",
6327 dispname_str);
6328 for (size_t i = 0; i < nof_fps; i++) {
6329 if (i > 0) body = mputstr(body,
6330 "TTCN_Logger::log_event_str(\", \");\n");
6331 body = mputprintf(body, "%s.log();\n",
6332 fp_list->get_fp_byIndex(i)->get_reference_name(my_scope).c_str());
6333 }
6334 body = mputstr(body, "TTCN_Logger::log_event_str(\").\");\n"
6335 "TTCN_Logger::end_event();\n");
6336 } else {
6337 body = mputprintf(body,
6338 "TTCN_Logger::log_str(TTCN_Logger::PARALLEL_PTC, \"Starting function "
6339 "%s().\");\n", dispname_str);
6340 }
6341 body = mputstr(body,
6342 "TTCN_Runtime::function_started(function_arguments);\n");
6343 char *actual_par_list =
6344 fp_list->generate_code_actual_parlist(memptystr(), "");
6345 bool return_value_kept = false;
6346 if (asstype == A_FUNCTION_RVAL) {
6347 // the return value is kept only if the function returns a value
6348 // (rather than a template) and the return type has the "done"
6349 // extension attribute
6350 for (Type *t = return_type; ; t = t->get_type_refd()) {
6351 if (t->has_done_attribute()) {
6352 return_value_kept = true;
6353 break;
6354 } else if (!t->is_ref()) break;
6355 }
6356 }
6357 if (return_value_kept) {
6358 const string& return_type_dispname = return_type->get_typename();
6359 const char *return_type_dispname_str = return_type_dispname.c_str();
6360 body = mputprintf(body, "%s ret_val(%s(%s));\n"
6361 "TTCN_Logger::begin_event(TTCN_PARALLEL);\n"
6362 "TTCN_Logger::log_event_str(\"Function %s returned %s : \");\n"
6363 "ret_val.log();\n"
6364 "Text_Buf text_buf;\n"
6365 "TTCN_Runtime::prepare_function_finished(\"%s\", text_buf);\n"
6366 "ret_val.encode_text(text_buf);\n"
6367 "TTCN_Runtime::send_function_finished(text_buf);\n",
6368 return_type_str, genname_str, actual_par_list, dispname_str,
6369 return_type_dispname_str, return_type_dispname_str);
6370 } else {
6371 body = mputprintf(body, "%s(%s);\n"
6372 "TTCN_Runtime::function_finished(\"%s\");\n",
6373 genname_str, actual_par_list, dispname_str);
6374 }
6375 Free(actual_par_list);
6376 body = mputstr(body, "return TRUE;\n"
6377 "} else ");
6378 target->functions.start = mputstr(target->functions.start, body);
6379 Free(body);
6380 Free(full_formal_par_list);
6381 }
6382
6383 target->functions.pre_init = mputprintf(target->functions.pre_init,
6384 "%s.add_function(\"%s\", (genericfunc_t)&%s, ", get_module_object_name(),
6385 dispname_str, genname_str);
6386 if(is_startable)
6387 target->functions.pre_init = mputprintf(target->functions.pre_init,
6388 "(genericfunc_t)&start_%s);\n", genname_str);
6389 else
6390 target->functions.pre_init = mputstr(target->functions.pre_init,
6391 "NULL);\n");
6392 }
6393
6394 void Def_Function::generate_code(CodeGenHelper& cgh) {
6395 generate_code(cgh.get_current_outputstruct());
6396 }
6397
6398 void Def_Function::dump_internal(unsigned level) const
6399 {
6400 DEBUG(level, "Function: %s", id->get_dispname().c_str());
6401 DEBUG(level + 1, "Parameters:");
6402 fp_list->dump(level + 1);
6403 if (runs_on_ref) {
6404 DEBUG(level + 1, "Runs on clause:");
6405 runs_on_ref->dump(level + 2);
6406 }
6407 if (return_type) {
6408 DEBUG(level + 1, "Return type:");
6409 return_type->dump(level + 2);
6410 if (asstype == A_FUNCTION_RTEMP) DEBUG(level + 1, "Returns template");
6411 }
6412 if (prototype != PROTOTYPE_NONE)
6413 DEBUG(level + 1, "Prototype: %s", get_prototype_name());
6414 //DEBUG(level + 1, "Statement block:");
6415 block->dump(level + 1);
6416 }
6417
6418 void Def_Function::set_parent_path(WithAttribPath* p_path) {
6419 Def_Function_Base::set_parent_path(p_path);
6420 block->set_parent_path(w_attrib_path);
6421 }
6422
6423 // =================================
6424 // ===== Def_ExtFunction
6425 // =================================
6426
6427 Def_ExtFunction::~Def_ExtFunction()
6428 {
6429 delete encoding_options;
6430 delete eb_list;
6431 if (NULL != json_printing) {
6432 delete json_printing;
6433 }
6434 }
6435
6436 Def_ExtFunction *Def_ExtFunction::clone() const
6437 {
6438 FATAL_ERROR("Def_ExtFunction::clone");
6439 }
6440
6441 void Def_ExtFunction::set_fullname(const string& p_fullname)
6442 {
6443 Def_Function_Base::set_fullname(p_fullname);
6444 if (eb_list) eb_list->set_fullname(p_fullname + ".<errorbehavior_list>");
6445 }
6446
6447 void Def_ExtFunction::set_encode_parameters(Type::MessageEncodingType_t
6448 p_encoding_type, string *p_encoding_options)
6449 {
6450 function_type = EXTFUNC_ENCODE;
6451 encoding_type = p_encoding_type;
6452 delete encoding_options;
6453 encoding_options = p_encoding_options;
6454 }
6455
6456 void Def_ExtFunction::set_decode_parameters(Type::MessageEncodingType_t
6457 p_encoding_type, string *p_encoding_options)
6458 {
6459 function_type = EXTFUNC_DECODE;
6460 encoding_type = p_encoding_type;
6461 delete encoding_options;
6462 encoding_options = p_encoding_options;
6463 }
6464
6465 void Def_ExtFunction::add_eb_list(Ttcn::ErrorBehaviorList *p_eb_list)
6466 {
6467 if (!p_eb_list) FATAL_ERROR("Def_ExtFunction::add_eb_list()");
6468 if (eb_list) {
6469 eb_list->steal_ebs(p_eb_list);
6470 delete p_eb_list;
6471 } else {
6472 eb_list = p_eb_list;
6473 eb_list->set_fullname(get_fullname() + ".<errorbehavior_list>");
6474 }
6475 }
6476
6477 void Def_ExtFunction::chk_function_type()
6478 {
6479 switch (function_type) {
6480 case EXTFUNC_MANUAL:
6481 if (eb_list) {
6482 eb_list->error("Attribute `errorbehavior' can only be used together "
6483 "with `encode' or `decode'");
6484 eb_list->chk();
6485 }
6486 break;
6487 case EXTFUNC_ENCODE:
6488 switch (prototype) {
6489 case PROTOTYPE_NONE:
6490 error("Attribute `encode' cannot be used without `prototype'");
6491 break;
6492 case PROTOTYPE_BACKTRACK:
6493 case PROTOTYPE_SLIDING:
6494 error("Attribute `encode' cannot be used with `prototype(%s)'",
6495 get_prototype_name());
6496 default: /* CONVERT and FAST allowed */
6497 break;
6498 }
6499
6500 if (input_type) {
6501 if (!input_type->has_encoding(encoding_type, encoding_options)) {
6502 if (Common::Type::CT_CUSTOM == encoding_type) {
6503 input_type->error("Input type `%s' does not support custom encoding '%s'",
6504 input_type->get_typename().c_str(), encoding_options->c_str());
6505 }
6506 else {
6507 input_type->error("Input type `%s' does not support %s encoding",
6508 input_type->get_typename().c_str(),
6509 Type::get_encoding_name(encoding_type));
6510 }
6511 }
6512 else {
6513 if (Common::Type::CT_XER == encoding_type
6514 && input_type->get_type_refd_last()->is_untagged()) {
6515 // "untagged" on the (toplevel) input type will have no effect.
6516 warning("UNTAGGED encoding attribute is ignored on top-level type");
6517 }
6518 if (Common::Type::CT_CUSTOM == encoding_type) {
6519 if (PROTOTYPE_CONVERT != prototype) {
6520 error("Only `prototype(convert)' is allowed for custom encoding functions");
6521 }
6522 else {
6523 // let the input type know that this is its encoding function
6524 input_type->get_type_refd()->set_coding_function(true,
6525 get_genname_from_scope(input_type->get_type_refd()->get_my_scope()));
6526 // treat this as a manual external function during code generation
6527 function_type = EXTFUNC_MANUAL;
6528 }
6529 }
6530 }
6531 }
6532 if (output_type) {
6533 if(encoding_type == Common::Type::CT_TEXT) { // TEXT encoding supports both octetstring and charstring stream types
6534 Type *stream_type = Type::get_stream_type(encoding_type,0);
6535 Type *stream_type2 = Type::get_stream_type(encoding_type,1);
6536 if ( (!stream_type->is_identical(output_type)) && (!stream_type2->is_identical(output_type)) ) {
6537 output_type->error("The output type of %s encoding should be `%s' or `%s' "
6538 "instead of `%s'", Type::get_encoding_name(encoding_type),
6539 stream_type->get_typename().c_str(),
6540 stream_type2->get_typename().c_str(),
6541 output_type->get_typename().c_str());
6542 }
6543 } else {
6544 Type *stream_type = Type::get_stream_type(encoding_type);
6545 if (!stream_type->is_identical(output_type)) {
6546 output_type->error("The output type of %s encoding should be `%s' "
6547 "instead of `%s'", Type::get_encoding_name(encoding_type),
6548 stream_type->get_typename().c_str(),
6549 output_type->get_typename().c_str());
6550 }
6551 }
6552 }
6553 if (eb_list) eb_list->chk();
6554 chk_allowed_encode();
6555 break;
6556 case EXTFUNC_DECODE:
6557 if (prototype == PROTOTYPE_NONE) {
6558 error("Attribute `decode' cannot be used without `prototype'");
6559 }
6560 if (input_type) {
6561 if(encoding_type == Common::Type::CT_TEXT) { // TEXT encoding supports both octetstring and charstring stream types
6562 Type *stream_type = Type::get_stream_type(encoding_type,0);
6563 Type *stream_type2 = Type::get_stream_type(encoding_type,1);
6564 if ( (!stream_type->is_identical(input_type)) && (!stream_type2->is_identical(input_type)) ) {
6565 input_type->error("The input type of %s decoding should be `%s' or `%s' "
6566 "instead of `%s'", Type::get_encoding_name(encoding_type),
6567 stream_type->get_typename().c_str(),
6568 stream_type2->get_typename().c_str(),
6569 input_type->get_typename().c_str());
6570 }
6571 } else {
6572 Type *stream_type = Type::get_stream_type(encoding_type);
6573 if (!stream_type->is_identical(input_type)) {
6574 input_type->error("The input type of %s decoding should be `%s' "
6575 "instead of `%s'", Type::get_encoding_name(encoding_type),
6576 stream_type->get_typename().c_str(),
6577 input_type->get_typename().c_str());
6578 }
6579 }
6580
6581 }
6582 if (output_type && !output_type->has_encoding(encoding_type, encoding_options)) {
6583 if (Common::Type::CT_CUSTOM == encoding_type) {
6584 output_type->error("Output type `%s' does not support custom encoding '%s'",
6585 output_type->get_typename().c_str(), encoding_options->c_str());
6586 }
6587 else {
6588 output_type->error("Output type `%s' does not support %s encoding",
6589 output_type->get_typename().c_str(),
6590 Type::get_encoding_name(encoding_type));
6591 }
6592 }
6593 else {
6594 if (Common::Type::CT_CUSTOM == encoding_type) {
6595 if (PROTOTYPE_SLIDING != prototype) {
6596 error("Only `prototype(sliding)' is allowed for custom decoding functions");
6597 }
6598 else if (output_type) {
6599 // let the output type know that this is its decoding function
6600 output_type->get_type_refd()->set_coding_function(false,
6601 get_genname_from_scope(output_type->get_type_refd()->get_my_scope()));
6602 // treat this as a manual external function during code generation
6603 function_type = EXTFUNC_MANUAL;
6604 }
6605 }
6606 }
6607 if (eb_list) eb_list->chk();
6608 chk_allowed_encode();
6609 break;
6610 default:
6611 FATAL_ERROR("Def_ExtFunction::chk()");
6612 }
6613 }
6614
6615 void Def_ExtFunction::chk_allowed_encode()
6616 {
6617 switch (encoding_type) {
6618 case Type::CT_BER:
6619 if (enable_ber()) return; // ok
6620 break;
6621 case Type::CT_RAW:
6622 if (enable_raw()) return; // ok
6623 break;
6624 case Type::CT_TEXT:
6625 if (enable_text()) return; // ok
6626 break;
6627 case Type::CT_XER:
6628 if (enable_xer()) return; // ok
6629 break;
6630 case Type::CT_PER:
6631 if (enable_per()) return; // ok?
6632 break;
6633 case Type::CT_JSON:
6634 if (enable_json()) return;
6635 break;
6636 case Type::CT_CUSTOM:
6637 return; // cannot be disabled
6638 default:
6639 FATAL_ERROR("Def_ExtFunction::chk_allowed_encode");
6640 break;
6641 }
6642
6643 error("%s encoding is disallowed by license or commandline options",
6644 Type::get_encoding_name(encoding_type));
6645 }
6646
6647 void Def_ExtFunction::chk()
6648 {
6649 if (checked) return;
6650 checked = true;
6651 Error_Context cntxt(this, "In external function definition `%s'",
6652 id->get_dispname().c_str());
6653 fp_list->chk(asstype);
6654 if (return_type) {
6655 Error_Context cntxt2(return_type, "In return type");
6656 return_type->chk();
6657 return_type->chk_as_return_type(asstype == A_EXT_FUNCTION_RVAL,
6658 "external function");
6659 }
6660 if (!semantic_check_only) fp_list->set_genname(get_genname());
6661 if (w_attrib_path) {
6662 w_attrib_path->chk_global_attrib();
6663 w_attrib_path->chk_no_qualif();
6664 const Ttcn::ExtensionAttributes * extattrs = parse_extattributes(w_attrib_path);
6665 if (extattrs != 0) {
6666 size_t num_atrs = extattrs->size();
6667 for (size_t i=0; i < num_atrs; ++i) {
6668 ExtensionAttribute &ea = extattrs->get(i);
6669 switch (ea.get_type()) {
6670 case ExtensionAttribute::PROTOTYPE: {
6671 if (get_prototype() != Def_Function_Base::PROTOTYPE_NONE) {
6672 ea.error("Duplicate attribute `prototype'");
6673 }
6674 Def_Function_Base::prototype_t proto = ea.get_proto();
6675 set_prototype(proto);
6676 break; }
6677
6678 case ExtensionAttribute::ENCODE: {
6679 switch (get_function_type()) {
6680 case Def_ExtFunction::EXTFUNC_MANUAL:
6681 break;
6682 case Def_ExtFunction::EXTFUNC_ENCODE: {
6683 ea.error("Duplicate attribute `encode'");
6684 break; }
6685 case Def_ExtFunction::EXTFUNC_DECODE: {
6686 ea.error("Attributes `decode' and `encode' "
6687 "cannot be used at the same time");
6688 break; }
6689 default:
6690 FATAL_ERROR("coding_attrib_parse(): invalid external function type");
6691 }
6692 Type::MessageEncodingType_t et;
6693 string *opt;
6694 ea.get_encdec_parameters(et, opt);
6695 set_encode_parameters(et, opt);
6696 break; }
6697
6698 case ExtensionAttribute::ERRORBEHAVIOR: {
6699 add_eb_list(ea.get_eb_list());
6700 break; }
6701
6702 case ExtensionAttribute::DECODE: {
6703 switch (get_function_type()) {
6704 case Def_ExtFunction::EXTFUNC_MANUAL:
6705 break;
6706 case Def_ExtFunction::EXTFUNC_ENCODE: {
6707 ea.error("Attributes `encode' and `decode' "
6708 "cannot be used at the same time");
6709 break; }
6710 case Def_ExtFunction::EXTFUNC_DECODE: {
6711 ea.error("Duplicate attribute `decode'");
6712 break; }
6713 default:
6714 FATAL_ERROR("coding_attrib_parse(): invalid external function type");
6715 }
6716 Type::MessageEncodingType_t et;
6717 string *opt;
6718 ea.get_encdec_parameters(et, opt);
6719 set_decode_parameters(et, opt);
6720 break; }
6721
6722 case ExtensionAttribute::PRINTING: {
6723 json_printing = ea.get_printing();
6724 break; }
6725
6726 case ExtensionAttribute::ANYTYPELIST:
6727 // ignore, because we can't distinguish between a local
6728 // "extension anytype" (which is bogus) and an inherited one
6729 // (which was meant for a type definition)
6730 break;
6731
6732 case ExtensionAttribute::NONE:
6733 // Ignore, do not issue "wrong type" error
6734 break;
6735
6736 default:
6737 ea.error(
6738 "Only the following extension attributes may be applied to "
6739 "external functions: 'prototype', 'encode', 'decode', 'errorbehavior'");
6740 break;
6741 } // switch type
6742 } // next attribute
6743 delete extattrs;
6744 } // if extatrs
6745 }
6746 chk_prototype();
6747 chk_function_type();
6748
6749 if (NULL != json_printing && (EXTFUNC_ENCODE != function_type ||
6750 Type::CT_JSON != encoding_type)) {
6751 error("Attribute 'printing' is only allowed for JSON encoding functions.");
6752 }
6753 }
6754
6755 char *Def_ExtFunction::generate_code_encode(char *str)
6756 {
6757 const char *function_name = id->get_dispname().c_str();
6758 const char *first_par_name =
6759 fp_list->get_fp_byIndex(0)->get_id().get_name().c_str();
6760 // producing debug printout of the input PDU
6761 str = mputprintf(str,
6762 #ifndef NDEBUG
6763 "// written by %s in " __FILE__ " at %d\n"
6764 #endif
6765 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6766 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6767 "TTCN_Logger::log_event_str(\"%s(): Encoding %s: \");\n"
6768 "%s.log();\n"
6769 "TTCN_Logger::end_event();\n"
6770 "}\n"
6771 #ifndef NDEBUG
6772 , __FUNCTION__, __LINE__
6773 #endif
6774 , function_name, input_type->get_typename().c_str(), first_par_name);
6775 // setting error behavior
6776 if (eb_list) str = eb_list->generate_code(str);
6777 else str = mputstr(str, "TTCN_EncDec::set_error_behavior("
6778 "TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);\n");
6779 // encoding PDU into the buffer
6780 str = mputstr(str, "TTCN_Buffer ttcn_buffer;\n");
6781 str = mputprintf(str, "%s.encode(%s_descr_, ttcn_buffer, TTCN_EncDec::CT_%s",
6782 first_par_name,
6783 input_type->get_genname_typedescriptor(my_scope).c_str(),
6784 Type::get_encoding_name(encoding_type));
6785 if (encoding_type == Type::CT_JSON) {
6786 if (json_printing != NULL) {
6787 str = json_printing->generate_code(str);
6788 } else {
6789 str = mputstr(str, ", 0");
6790 }
6791 }
6792 if (encoding_options) str = mputprintf(str, ", %s",
6793 encoding_options->c_str());
6794 str = mputstr(str, ");\n");
6795 const char *result_name;
6796 switch (prototype) {
6797 case PROTOTYPE_CONVERT:
6798 result_name = "ret_val";
6799 // creating a local variable for the result stream
6800 str = mputprintf(str, "%s ret_val;\n",
6801 output_type->get_genname_value(my_scope).c_str());
6802 break;
6803 case PROTOTYPE_FAST:
6804 result_name = fp_list->get_fp_byIndex(1)->get_id().get_name().c_str();
6805 break;
6806 default:
6807 FATAL_ERROR("Def_ExtFunction::generate_code_encode()");
6808 result_name = 0;
6809 }
6810 // taking the result from the buffer and producing debug printout
6811 str = mputprintf(str, "ttcn_buffer.get_string(%s);\n"
6812 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6813 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6814 "TTCN_Logger::log_event_str(\"%s(): Stream after encoding: \");\n"
6815 "%s.log();\n"
6816 "TTCN_Logger::end_event();\n"
6817 "}\n", result_name, function_name, result_name);
6818 // returning the result stream if necessary
6819 if (prototype == PROTOTYPE_CONVERT) {
6820 if (debugger_active) {
6821 str = mputstr(str,
6822 "ttcn3_debugger.set_return_value((TTCN_Logger::begin_event_log2str(), "
6823 "ret_val.log(), TTCN_Logger::end_event_log2str()));\n");
6824 }
6825 str = mputstr(str, "return ret_val;\n");
6826 }
6827 return str;
6828 }
6829
6830 char *Def_ExtFunction::generate_code_decode(char *str)
6831 {
6832 const char *function_name = id->get_dispname().c_str();
6833 const char *first_par_name =
6834 fp_list->get_fp_byIndex(0)->get_id().get_name().c_str();
6835 // producing debug printout of the input stream
6836 str = mputprintf(str,
6837 #ifndef NDEBUG
6838 "// written by %s in " __FILE__ " at %d\n"
6839 #endif
6840 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6841 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6842 "TTCN_Logger::log_event_str(\"%s(): Stream before decoding: \");\n"
6843 "%s.log();\n"
6844 "TTCN_Logger::end_event();\n"
6845 "}\n"
6846 #ifndef NDEBUG
6847 , __FUNCTION__, __LINE__
6848 #endif
6849 , function_name, first_par_name);
6850 // setting error behavior
6851 if (eb_list) str = eb_list->generate_code(str);
6852 else if (prototype == PROTOTYPE_BACKTRACK || prototype == PROTOTYPE_SLIDING) {
6853 str = mputstr(str, "TTCN_EncDec::set_error_behavior("
6854 "TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_WARNING);\n");
6855 } else str = mputstr(str, "TTCN_EncDec::set_error_behavior("
6856 "TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);\n");
6857 // creating a buffer from the input stream
6858 str = mputprintf(str, "TTCN_EncDec::clear_error();\n"
6859 "TTCN_Buffer ttcn_buffer(%s);\n", first_par_name);
6860 const char *result_name;
6861 if (prototype == PROTOTYPE_CONVERT) {
6862 // creating a local variable for the result
6863 str = mputprintf(str, "%s ret_val;\n",
6864 output_type->get_genname_value(my_scope).c_str());
6865 result_name = "ret_val";
6866 } else {
6867 result_name = fp_list->get_fp_byIndex(1)->get_id().get_name().c_str();
6868 }
6869 if(encoding_type==Type::CT_TEXT){
6870 str = mputprintf(str,
6871 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6872 " TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_LOG_MATCHING, TTCN_EncDec::EB_WARNING);\n"
6873 "}\n");
6874 }
6875 str = mputprintf(str, "%s.decode(%s_descr_, ttcn_buffer, "
6876 "TTCN_EncDec::CT_%s", result_name,
6877 output_type->get_genname_typedescriptor(my_scope).c_str(),
6878 Type::get_encoding_name(encoding_type));
6879 if (encoding_options) str = mputprintf(str, ", %s",
6880 encoding_options->c_str());
6881 str = mputstr(str, ");\n");
6882 // producing debug printout of the result PDU
6883 str = mputprintf(str,
6884 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6885 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6886 "TTCN_Logger::log_event_str(\"%s(): Decoded %s: \");\n"
6887 "%s.log();\n"
6888 "TTCN_Logger::end_event();\n"
6889 "}\n", function_name, output_type->get_typename().c_str(), result_name);
6890 if (prototype != PROTOTYPE_SLIDING) {
6891 // checking for remaining data in the buffer if decoding was successful
6892 str = mputprintf(str, "if (TTCN_EncDec::get_last_error_type() == "
6893 "TTCN_EncDec::ET_NONE) {\n"
6894 "if (ttcn_buffer.get_pos() < ttcn_buffer.get_len()-1 && "
6895 "TTCN_Logger::log_this_event(TTCN_WARNING)) {\n"
6896 "ttcn_buffer.cut();\n"
6897 "%s remaining_stream;\n"
6898 "ttcn_buffer.get_string(remaining_stream);\n"
6899 "TTCN_Logger::begin_event(TTCN_WARNING);\n"
6900 "TTCN_Logger::log_event_str(\"%s(): Warning: Data remained at the end "
6901 "of the stream after successful decoding: \");\n"
6902 "remaining_stream.log();\n"
6903 "TTCN_Logger::end_event();\n"
6904 "}\n", input_type->get_genname_value(my_scope).c_str(), function_name);
6905 // closing the block and returning the appropriate result or status code
6906 if (prototype == PROTOTYPE_BACKTRACK) {
6907 if (debugger_active) {
6908 str = mputstr(str, "ttcn3_debugger.set_return_value(\"0\");\n");
6909 }
6910 str = mputstr(str,
6911 "return 0;\n"
6912 "} else {\n");
6913 if (debugger_active) {
6914 str = mputstr(str, "ttcn3_debugger.set_return_value(\"1\");\n");
6915 }
6916 str = mputstr(str,
6917 "return 1;\n"
6918 "}\n");
6919 } else {
6920 str = mputstr(str, "}\n");
6921 if (prototype == PROTOTYPE_CONVERT) {
6922 if (debugger_active) {
6923 str = mputstr(str,
6924 "ttcn3_debugger.set_return_value((TTCN_Logger::begin_event_log2str(), "
6925 "ret_val.log(), TTCN_Logger::end_event_log2str()));\n");
6926 }
6927 str = mputstr(str, "return ret_val;\n");
6928 }
6929 }
6930 } else {
6931 // result handling and debug printout for sliding decoders
6932 str = mputprintf(str, "switch (TTCN_EncDec::get_last_error_type()) {\n"
6933 "case TTCN_EncDec::ET_NONE:\n"
6934 // TTCN_Buffer::get_string will call OCTETSTRING::clean_up()
6935 "ttcn_buffer.cut();\n"
6936 "ttcn_buffer.get_string(%s);\n"
6937 "if (TTCN_Logger::log_this_event(TTCN_Logger::DEBUG_ENCDEC)) {\n"
6938 "TTCN_Logger::begin_event(TTCN_Logger::DEBUG_ENCDEC);\n"
6939 "TTCN_Logger::log_event_str(\"%s(): Stream after decoding: \");\n"
6940 "%s.log();\n"
6941 "TTCN_Logger::end_event();\n"
6942 "}\n"
6943 "%sreturn 0;\n"
6944 "case TTCN_EncDec::ET_INCOMPL_MSG:\n"
6945 "case TTCN_EncDec::ET_LEN_ERR:\n"
6946 "%sreturn 2;\n"
6947 "default:\n"
6948 "%sreturn 1;\n"
6949 "}\n", first_par_name, function_name, first_par_name,
6950 debugger_active ? "ttcn3_debugger.set_return_value(\"0\");\n" : "",
6951 debugger_active ? "ttcn3_debugger.set_return_value(\"2\");\n" : "",
6952 debugger_active ? "ttcn3_debugger.set_return_value(\"1\");\n" : "");
6953 }
6954 return str;
6955 }
6956
6957 void Def_ExtFunction::generate_code(output_struct *target, bool)
6958 {
6959 const string& t_genname = get_genname();
6960 const char *genname_str = t_genname.c_str();
6961 string return_type_name;
6962 switch (asstype) {
6963 case A_EXT_FUNCTION:
6964 return_type_name = "void";
6965 break;
6966 case A_EXT_FUNCTION_RVAL:
6967 return_type_name = return_type->get_genname_value(my_scope);
6968 break;
6969 case A_EXT_FUNCTION_RTEMP:
6970 return_type_name = return_type->get_genname_template(my_scope);
6971 break;
6972 default:
6973 FATAL_ERROR("Def_ExtFunction::generate_code()");
6974 }
6975 const char *return_type_str = return_type_name.c_str();
6976 char *formal_par_list = fp_list->generate_code(memptystr(), fp_list->get_nof_fps());
6977 fp_list->generate_code_defval(target);
6978 // function prototype
6979 target->header.function_prototypes =
6980 mputprintf(target->header.function_prototypes, "extern %s %s(%s);\n",
6981 return_type_str, genname_str, formal_par_list);
6982
6983 if (function_type != EXTFUNC_MANUAL) {
6984 // function body written by the compiler
6985 char *body = 0;
6986 #ifndef NDEBUG
6987 body = mprintf("// written by %s in " __FILE__ " at %d\n"
6988 , __FUNCTION__, __LINE__);
6989 #endif
6990 body = mputprintf(body,
6991 "%s %s(%s)\n"
6992 "{\n"
6993 , return_type_str, genname_str, formal_par_list);
6994 if (debugger_active) {
6995 body = generate_code_debugger_function_init(body, this);
6996 }
6997 switch (function_type) {
6998 case EXTFUNC_ENCODE:
6999 body = generate_code_encode(body);
7000 break;
7001 case EXTFUNC_DECODE:
7002 body = generate_code_decode(body);
7003 break;
7004 default:
7005 FATAL_ERROR("Def_ExtFunction::generate_code()");
7006 }
7007 body = mputstr(body, "}\n\n");
7008 target->source.function_bodies = mputstr(target->source.function_bodies,
7009 body);
7010 Free(body);
7011 }
7012
7013 Free(formal_par_list);
7014
7015 target->functions.pre_init = mputprintf(target->functions.pre_init,
7016 "%s.add_function(\"%s\", (genericfunc_t)&%s, NULL);\n",
7017 get_module_object_name(), id->get_dispname().c_str(), genname_str);
7018 }
7019
7020 void Def_ExtFunction::generate_code(CodeGenHelper& cgh) {
7021 generate_code(cgh.get_current_outputstruct());
7022 }
7023
7024 void Def_ExtFunction::dump_internal(unsigned level) const
7025 {
7026 DEBUG(level, "External function: %s", id->get_dispname().c_str());
7027 DEBUG(level + 1, "Parameters:");
7028 fp_list->dump(level + 2);
7029 if (return_type) {
7030 DEBUG(level + 1, "Return type:");
7031 return_type->dump(level + 2);
7032 if(asstype == A_EXT_FUNCTION_RTEMP) DEBUG(level + 1, "Returns template");
7033 }
7034 if (prototype != PROTOTYPE_NONE)
7035 DEBUG(level + 1, "Prototype: %s", get_prototype_name());
7036 if (function_type != EXTFUNC_MANUAL) {
7037 DEBUG(level + 1, "Automatically generated: %s",
7038 function_type == EXTFUNC_ENCODE ? "encoder" : "decoder");
7039 DEBUG(level + 2, "Encoding type: %s",
7040 Type::get_encoding_name(encoding_type));
7041 if (encoding_options)
7042 DEBUG(level + 2, "Encoding options: %s", encoding_options->c_str());
7043 }
7044 if (eb_list) eb_list->dump(level + 1);
7045 }
7046
7047 void Def_ExtFunction::generate_json_schema_ref(map<Type*, JSON_Tokenizer>& json_refs)
7048 {
7049 // only do anything if this is a JSON encoding or decoding function
7050 if (encoding_type == Type::CT_JSON &&
7051 (function_type == EXTFUNC_ENCODE || function_type == EXTFUNC_DECODE)) {
7052 // retrieve the encoded type
7053 Type* type = NULL;
7054 if (function_type == EXTFUNC_ENCODE) {
7055 // for encoding functions it's always the first parameter
7056 type = fp_list->get_fp_byIndex(0)->get_Type();
7057 } else {
7058 // for decoding functions it depends on the prototype
7059 switch (prototype) {
7060 case PROTOTYPE_CONVERT:
7061 type = return_type;
7062 break;
7063 case PROTOTYPE_FAST:
7064 case PROTOTYPE_BACKTRACK:
7065 case PROTOTYPE_SLIDING:
7066 type = fp_list->get_fp_byIndex(1)->get_Type();
7067 break;
7068 default:
7069 FATAL_ERROR("Def_ExtFunction::generate_json_schema_ref");
7070 }
7071 }
7072
7073 // step over the type reference created for this function
7074 type = type->get_type_refd();
7075
7076 JSON_Tokenizer* json = NULL;
7077 if (json_refs.has_key(type)) {
7078 // the schema segment containing the type's reference already exists
7079 json = json_refs[type];
7080 } else {
7081 // the schema segment doesn't exist yet, create it and insert the reference
7082 json = new JSON_Tokenizer;
7083 json_refs.add(type, json);
7084 type->generate_json_schema_ref(*json);
7085 }
7086
7087 // insert a property to specify which function this is (encoding or decoding)
7088 json->put_next_token(JSON_TOKEN_NAME, (function_type == EXTFUNC_ENCODE) ?
7089 "encoding" : "decoding");
7090
7091 // place the function's info in an object
7092 json->put_next_token(JSON_TOKEN_OBJECT_START);
7093
7094 // insert information related to the function's prototype in an array
7095 json->put_next_token(JSON_TOKEN_NAME, "prototype");
7096 json->put_next_token(JSON_TOKEN_ARRAY_START);
7097
7098 // 1st element: external function prototype name (as string)
7099 switch(prototype) {
7100 case PROTOTYPE_CONVERT:
7101 json->put_next_token(JSON_TOKEN_STRING, "\"convert\"");
7102 break;
7103 case PROTOTYPE_FAST:
7104 json->put_next_token(JSON_TOKEN_STRING, "\"fast\"");
7105 break;
7106 case PROTOTYPE_BACKTRACK:
7107 json->put_next_token(JSON_TOKEN_STRING, "\"backtrack\"");
7108 break;
7109 case PROTOTYPE_SLIDING:
7110 json->put_next_token(JSON_TOKEN_STRING, "\"sliding\"");
7111 break;
7112 default:
7113 FATAL_ERROR("Def_ExtFunction::generate_json_schema_ref");
7114 }
7115
7116 // 2nd element: external function name
7117 char* func_name_str = mprintf("\"%s\"", id->get_dispname().c_str());
7118 json->put_next_token(JSON_TOKEN_STRING, func_name_str);
7119 Free(func_name_str);
7120
7121 // the rest of the elements contain the names of the function's parameters (1 or 2)
7122 for (size_t i = 0; i < fp_list->get_nof_fps(); ++i) {
7123 char* param_str = mprintf("\"%s\"",
7124 fp_list->get_fp_byIndex(i)->get_id().get_dispname().c_str());
7125 json->put_next_token(JSON_TOKEN_STRING, param_str);
7126 Free(param_str);
7127 }
7128
7129 // end of the prototype's array
7130 json->put_next_token(JSON_TOKEN_ARRAY_END);
7131
7132 // insert error behavior data
7133 if (eb_list != NULL) {
7134 json->put_next_token(JSON_TOKEN_NAME, "errorBehavior");
7135 json->put_next_token(JSON_TOKEN_OBJECT_START);
7136
7137 // add each error behavior modification as a property
7138 for (size_t i = 0; i < eb_list->get_nof_ebs(); ++i) {
7139 ErrorBehaviorSetting* eb = eb_list->get_ebs_byIndex(i);
7140 json->put_next_token(JSON_TOKEN_NAME, eb->get_error_type().c_str());
7141 char* handling_str = mprintf("\"%s\"", eb->get_error_handling().c_str());
7142 json->put_next_token(JSON_TOKEN_STRING, handling_str);
7143 Free(handling_str);
7144 }
7145
7146 json->put_next_token(JSON_TOKEN_OBJECT_END);
7147 }
7148
7149 // insert printing type
7150 if (json_printing != NULL) {
7151 json->put_next_token(JSON_TOKEN_NAME, "printing");
7152 json->put_next_token(JSON_TOKEN_STRING,
7153 (json_printing->get_printing() == PrintingType::PT_PRETTY) ?
7154 "\"pretty\"" : "\"compact\"");
7155 }
7156
7157 // end of this function's object
7158 json->put_next_token(JSON_TOKEN_OBJECT_END);
7159 }
7160 }
7161
7162 // =================================
7163 // ===== Def_Altstep
7164 // =================================
7165
7166 Def_Altstep::Def_Altstep(Identifier *p_id, FormalParList *p_fpl,
7167 Reference *p_runs_on_ref, StatementBlock *p_sb,
7168 AltGuards *p_ags)
7169 : Definition(A_ALTSTEP, p_id), fp_list(p_fpl), runs_on_ref(p_runs_on_ref),
7170 runs_on_type(0), sb(p_sb), ags(p_ags)
7171 {
7172 if (!p_fpl || !p_sb || !p_ags)
7173 FATAL_ERROR("Def_Altstep::Def_Altstep()");
7174 fp_list->set_my_def(this);
7175 sb->set_my_def(this);
7176 ags->set_my_def(this);
7177 ags->set_my_sb(sb, 0);
7178 }
7179
7180 Def_Altstep::~Def_Altstep()
7181 {
7182 delete fp_list;
7183 delete runs_on_ref;
7184 delete sb;
7185 delete ags;
7186 }
7187
7188 Def_Altstep *Def_Altstep::clone() const
7189 {
7190 FATAL_ERROR("Def_Altstep::clone");
7191 }
7192
7193 void Def_Altstep::set_fullname(const string& p_fullname)
7194 {
7195 Definition::set_fullname(p_fullname);
7196 fp_list->set_fullname(p_fullname + ".<formal_par_list>");
7197 if (runs_on_ref) runs_on_ref->set_fullname(p_fullname + ".<runs_on_type>");
7198 sb->set_fullname(p_fullname+".<block>");
7199 ags->set_fullname(p_fullname + ".<guards>");
7200 }
7201
7202 void Def_Altstep::set_my_scope(Scope *p_scope)
7203 {
7204 bridgeScope.set_parent_scope(p_scope);
7205 bridgeScope.set_scopeMacro_name(id->get_dispname());
7206
7207 Definition::set_my_scope(&bridgeScope);
7208 // the scope of the parameter list is set during checking
7209 if (runs_on_ref) runs_on_ref->set_my_scope(&bridgeScope);
7210 sb->set_my_scope(fp_list);
7211 ags->set_my_scope(sb);
7212 }
7213
7214 Type *Def_Altstep::get_RunsOnType()
7215 {
7216 if (!checked) chk();
7217 return runs_on_type;
7218 }
7219
7220 FormalParList *Def_Altstep::get_FormalParList()
7221 {
7222 if (!checked) chk();
7223 return fp_list;
7224 }
7225
7226 RunsOnScope *Def_Altstep::get_runs_on_scope(Type *comptype)
7227 {
7228 Module *my_module = dynamic_cast<Module*>(my_scope->get_scope_mod());
7229 if (!my_module) FATAL_ERROR("Def_Altstep::get_runs_on_scope()");
7230 return my_module->get_runs_on_scope(comptype);
7231 }
7232
7233 void Def_Altstep::chk()
7234 {
7235 if (checked) return;
7236 checked = true;
7237 Error_Context cntxt(this, "In altstep definition `%s'",
7238 id->get_dispname().c_str());
7239 Scope *parlist_scope = my_scope;
7240 if (runs_on_ref) {
7241 Error_Context cntxt2(runs_on_ref, "In `runs on' clause");
7242 runs_on_type = runs_on_ref->chk_comptype_ref();
7243 if (runs_on_type) {
7244 Scope *runs_on_scope = get_runs_on_scope(runs_on_type);
7245 runs_on_scope->set_parent_scope(my_scope);
7246 parlist_scope = runs_on_scope;
7247 }
7248 }
7249 fp_list->set_my_scope(parlist_scope);
7250 fp_list->chk(asstype);
7251 sb->chk();
7252 ags->set_is_altstep();
7253 ags->set_my_ags(ags);
7254 ags->set_my_laic_stmt(ags, 0);
7255 ags->chk();
7256 if (!semantic_check_only) {
7257 fp_list->set_genname(get_genname());
7258 sb->set_code_section(GovernedSimple::CS_INLINE);
7259 ags->set_code_section(GovernedSimple::CS_INLINE);
7260 }
7261 if (w_attrib_path) {
7262 w_attrib_path->chk_global_attrib();
7263 w_attrib_path->chk_no_qualif();
7264 }
7265 }
7266
7267 void Def_Altstep::generate_code(output_struct *target, bool)
7268 {
7269 const string& t_genname = get_genname();
7270 const char *genname_str = t_genname.c_str();
7271 const char *dispname_str = id->get_dispname().c_str();
7272
7273 // function for altstep instance:
7274 // assemble the function body first (this also determines which parameters
7275 // are never used)
7276 char* body = create_location_object(memptystr(), "ALTSTEP", dispname_str);
7277 body = fp_list->generate_shadow_objects(body);
7278 if (debugger_active) {
7279 body = generate_code_debugger_function_init(body, this);
7280 }
7281 body = sb->generate_code(body);
7282 body = ags->generate_code_altstep(body);
7283 // generate a smart formal parameter list (omits unused parameter names)
7284 char *formal_par_list = fp_list->generate_code(memptystr());
7285 fp_list->generate_code_defval(target);
7286
7287 // function for altstep instance: prototype
7288 target->header.function_prototypes =
7289 mputprintf(target->header.function_prototypes,
7290 "extern alt_status %s_instance(%s);\n", genname_str, formal_par_list);
7291
7292 // function for altstep instance: body
7293 target->source.function_bodies = mputprintf(target->source.function_bodies,
7294 "alt_status %s_instance(%s)\n"
7295 "{\n"
7296 "%s"
7297 "}\n\n", genname_str, formal_par_list, body);
7298 Free(formal_par_list);
7299 Free(body);
7300
7301 char *actual_par_list =
7302 fp_list->generate_code_actual_parlist(memptystr(), "");
7303
7304 // use a full formal parameter list for the rest of the functions
7305 char *full_formal_par_list = fp_list->generate_code(memptystr(),
7306 fp_list->get_nof_fps());
7307
7308 // wrapper function for stand-alone instantiation: prototype
7309 target->header.function_prototypes =
7310 mputprintf(target->header.function_prototypes,
7311 "extern void %s(%s);\n", genname_str, full_formal_par_list);
7312
7313 // wrapper function for stand-alone instantiation: body
7314 target->source.function_bodies =
7315 mputprintf(target->source.function_bodies, "void %s(%s)\n"
7316 "{\n"
7317 "altstep_begin:\n"
7318 "boolean block_flag = FALSE;\n"
7319 "alt_status altstep_flag = ALT_UNCHECKED, "
7320 "default_flag = ALT_UNCHECKED;\n"
7321 "for ( ; ; ) {\n"
7322 "TTCN_Snapshot::take_new(block_flag);\n"
7323 "if (altstep_flag != ALT_NO) {\n"
7324 "altstep_flag = %s_instance(%s);\n"
7325 "if (altstep_flag == ALT_YES || altstep_flag == ALT_BREAK) return;\n"
7326 "else if (altstep_flag == ALT_REPEAT) goto altstep_begin;\n"
7327 "}\n"
7328 "if (default_flag != ALT_NO) {\n"
7329 "default_flag = TTCN_Default::try_altsteps();\n"
7330 "if (default_flag == ALT_YES || default_flag == ALT_BREAK) return;\n"
7331 "else if (default_flag == ALT_REPEAT) goto altstep_begin;\n"
7332 "}\n"
7333 "if (altstep_flag == ALT_NO && default_flag == ALT_NO) "
7334 "TTCN_error(\"None of the branches can be chosen in altstep %s.\");\n"
7335 "else block_flag = TRUE;\n"
7336 "}\n"
7337 "}\n\n", genname_str, full_formal_par_list, genname_str, actual_par_list,
7338 dispname_str);
7339
7340 // class for keeping the altstep in the default context
7341 // the class is for internal use, we do not need to publish it in the
7342 // header file
7343 char* str = mprintf("class %s_Default : public Default_Base {\n", genname_str);
7344 str = fp_list->generate_code_object(str, "par_");
7345 str = mputprintf(str, "public:\n"
7346 "%s_Default(%s);\n"
7347 "alt_status call_altstep();\n"
7348 "};\n\n", genname_str, full_formal_par_list);
7349 target->source.class_defs = mputstr(target->source.class_defs, str);
7350 Free(str);
7351 // member functions of the class
7352 str = mprintf("%s_Default::%s_Default(%s)\n"
7353 " : Default_Base(\"%s\")", genname_str, genname_str, full_formal_par_list,
7354 dispname_str);
7355 for (size_t i = 0; i < fp_list->get_nof_fps(); i++) {
7356 const char *fp_name_str =
7357 fp_list->get_fp_byIndex(i)->get_id().get_name().c_str();
7358 str = mputprintf(str, ", par_%s(%s)", fp_name_str, fp_name_str);
7359 }
7360 str = mputstr(str, "\n{\n}\n\n");
7361 char *actual_par_list_prefixed =
7362 fp_list->generate_code_actual_parlist(memptystr(), "par_");
7363 str = mputprintf(str, "alt_status %s_Default::call_altstep()\n"
7364 "{\n"
7365 "return %s_instance(%s);\n"
7366 "}\n\n", genname_str, genname_str, actual_par_list_prefixed);
7367 Free(actual_par_list_prefixed);
7368 target->source.methods = mputstr(target->source.methods, str);
7369 Free(str);
7370
7371 // function for default activation: prototype
7372 target->header.function_prototypes =
7373 mputprintf(target->header.function_prototypes,
7374 "extern Default_Base *activate_%s(%s);\n", genname_str,
7375 full_formal_par_list);
7376
7377 // function for default activation: body
7378 str = mprintf("Default_Base *activate_%s(%s)\n"
7379 "{\n", genname_str, full_formal_par_list);
7380 str = mputprintf(str, "return new %s_Default(%s);\n"
7381 "}\n\n", genname_str, actual_par_list);
7382 target->source.function_bodies = mputstr(target->source.function_bodies,
7383 str);
7384 Free(str);
7385
7386 Free(full_formal_par_list);
7387 Free(actual_par_list);
7388
7389 target->functions.pre_init = mputprintf(target->functions.pre_init,
7390 "%s.add_altstep(\"%s\", (genericfunc_t)&%s_instance, (genericfunc_t )&activate_%s, "
7391 "(genericfunc_t )&%s);\n", get_module_object_name(), dispname_str, genname_str,
7392 genname_str, genname_str);
7393 }
7394
7395 void Def_Altstep::generate_code(CodeGenHelper& cgh) {
7396 generate_code(cgh.get_current_outputstruct());
7397 }
7398
7399 void Def_Altstep::dump_internal(unsigned level) const
7400 {
7401 DEBUG(level, "Altstep: %s", id->get_dispname().c_str());
7402 DEBUG(level + 1, "Parameters:");
7403 fp_list->dump(level + 1);
7404 if (runs_on_ref) {
7405 DEBUG(level + 1, "Runs on clause:");
7406 runs_on_ref->dump(level + 2);
7407 }
7408 /*
7409 DEBUG(level + 1, "Local definitions:");
7410 sb->dump(level + 2);
7411 */
7412 DEBUG(level + 1, "Guards:");
7413 ags->dump(level + 2);
7414 }
7415
7416 void Def_Altstep::set_parent_path(WithAttribPath* p_path) {
7417 Definition::set_parent_path(p_path);
7418 sb->set_parent_path(w_attrib_path);
7419 }
7420
7421 // =================================
7422 // ===== Def_Testcase
7423 // =================================
7424
7425 Def_Testcase::Def_Testcase(Identifier *p_id, FormalParList *p_fpl,
7426 Reference *p_runs_on_ref, Reference *p_system_ref,
7427 StatementBlock *p_block)
7428 : Definition(A_TESTCASE, p_id), fp_list(p_fpl), runs_on_ref(p_runs_on_ref),
7429 runs_on_type(0), system_ref(p_system_ref), system_type(0), block(p_block)
7430 {
7431 if (!p_fpl || !p_runs_on_ref || !p_block)
7432 FATAL_ERROR("Def_Testcase::Def_Testcase()");
7433 fp_list->set_my_def(this);
7434 block->set_my_def(this);
7435 }
7436
7437 Def_Testcase::~Def_Testcase()
7438 {
7439 delete fp_list;
7440 delete runs_on_ref;
7441 delete system_ref;
7442 delete block;
7443 }
7444
7445 Def_Testcase *Def_Testcase::clone() const
7446 {
7447 FATAL_ERROR("Def_Testcase::clone");
7448 }
7449
7450 void Def_Testcase::set_fullname(const string& p_fullname)
7451 {
7452 Definition::set_fullname(p_fullname);
7453 fp_list->set_fullname(p_fullname + ".<formal_par_list>");
7454 runs_on_ref->set_fullname(p_fullname + ".<runs_on_type>");
7455 if (system_ref) system_ref->set_fullname(p_fullname + ".<system_type>");
7456 block->set_fullname(p_fullname + ".<statement_block>");
7457 }
7458
7459 void Def_Testcase::set_my_scope(Scope *p_scope)
7460 {
7461 bridgeScope.set_parent_scope(p_scope);
7462 bridgeScope.set_scopeMacro_name(id->get_dispname());
7463
7464 Definition::set_my_scope(&bridgeScope);
7465 // the scope of the parameter list is set during checking
7466 runs_on_ref->set_my_scope(&bridgeScope);
7467 if (system_ref) system_ref->set_my_scope(&bridgeScope);
7468 block->set_my_scope(fp_list);
7469 }
7470
7471 Type *Def_Testcase::get_RunsOnType()
7472 {
7473 if (!checked) chk();
7474 return runs_on_type;
7475 }
7476
7477 Type *Def_Testcase::get_SystemType()
7478 {
7479 if (!checked) chk();
7480 return system_type;
7481 }
7482
7483 FormalParList *Def_Testcase::get_FormalParList()
7484 {
7485 if (!checked) chk();
7486 return fp_list;
7487 }
7488
7489 RunsOnScope *Def_Testcase::get_runs_on_scope(Type *comptype)
7490 {
7491 Module *my_module = dynamic_cast<Module*>(my_scope->get_scope_mod());
7492 if (!my_module) FATAL_ERROR("Def_Testcase::get_runs_on_scope()");
7493 return my_module->get_runs_on_scope(comptype);
7494 }
7495
7496 void Def_Testcase::chk()
7497 {
7498 if (checked) return;
7499 checked = true;
7500 Error_Context cntxt(this, "In testcase definition `%s'",
7501 id->get_dispname().c_str());
7502 Scope *parlist_scope = my_scope;
7503 {
7504 Error_Context cntxt2(runs_on_ref, "In `runs on' clause");
7505 runs_on_type = runs_on_ref->chk_comptype_ref();
7506 if (runs_on_type) {
7507 Scope *runs_on_scope = get_runs_on_scope(runs_on_type);
7508 runs_on_scope->set_parent_scope(my_scope);
7509 parlist_scope = runs_on_scope;
7510 }
7511 }
7512 if (system_ref) {
7513 Error_Context cntxt2(system_ref, "In `system' clause");
7514 system_type = system_ref->chk_comptype_ref();;
7515 }
7516 fp_list->set_my_scope(parlist_scope);
7517 fp_list->chk(asstype);
7518 block->chk();
7519 if (!semantic_check_only) {
7520 fp_list->set_genname(get_genname());
7521 block->set_code_section(GovernedSimple::CS_INLINE);
7522 }
7523 if (w_attrib_path) {
7524 w_attrib_path->chk_global_attrib();
7525 w_attrib_path->chk_no_qualif();
7526 }
7527 }
7528
7529 void Def_Testcase::generate_code(output_struct *target, bool)
7530 {
7531 const string& t_genname = get_genname();
7532 const char *genname_str = t_genname.c_str();
7533 const char *dispname_str = id->get_dispname().c_str();
7534
7535 // assemble the function body first (this also determines which parameters
7536 // are never used)
7537
7538 // Checking whether the testcase was invoked from another one.
7539 // At this point the location information should refer to the execute()
7540 // statement rather than this testcase.
7541 char* body = mputstr(memptystr(), "TTCN_Runtime::check_begin_testcase(has_timer, "
7542 "timer_value);\n");
7543 body = create_location_object(body, "TESTCASE", dispname_str);
7544 body = fp_list->generate_shadow_objects(body);
7545 body = mputprintf(body, "try {\n"
7546 "TTCN_Runtime::begin_testcase(\"%s\", \"%s\", ",
7547 my_scope->get_scope_mod()->get_modid().get_dispname().c_str(),
7548 dispname_str);
7549 ComponentTypeBody *runs_on_body = runs_on_type->get_CompBody();
7550 body = runs_on_body->generate_code_comptype_name(body);
7551 body = mputstr(body, ", ");
7552 if (system_type)
7553 body = system_type->get_CompBody()->generate_code_comptype_name(body);
7554 else body = runs_on_body->generate_code_comptype_name(body);
7555 body = mputstr(body, ", has_timer, timer_value);\n");
7556 if (debugger_active) {
7557 body = generate_code_debugger_function_init(body, this);
7558 }
7559 body = block->generate_code(body);
7560 body = mputprintf(body,
7561 "} catch (const TC_Error& tc_error) {\n"
7562 "} catch (const TC_End& tc_end) {\n"
7563 "TTCN_Logger::log_str(TTCN_FUNCTION, \"Test case %s was stopped.\");\n"
7564 "}\n", dispname_str);
7565 body = mputstr(body, "return TTCN_Runtime::end_testcase();\n");
7566
7567 // smart formal parameter list (names of unused parameters are omitted)
7568 char *formal_par_list = fp_list->generate_code(memptystr());
7569 fp_list->generate_code_defval(target);
7570 if (fp_list->get_nof_fps() > 0)
7571 formal_par_list = mputstr(formal_par_list, ", ");
7572 formal_par_list = mputstr(formal_par_list,
7573 "boolean has_timer, double timer_value");
7574
7575 // function prototype
7576 target->header.function_prototypes =
7577 mputprintf(target->header.function_prototypes,
7578 "extern verdicttype testcase_%s(%s);\n", genname_str, formal_par_list);
7579
7580 // function body
7581 target->source.function_bodies = mputprintf(target->source.function_bodies,
7582 "verdicttype testcase_%s(%s)\n"
7583 "{\n"
7584 "%s"
7585 "}\n\n", genname_str, formal_par_list, body);
7586 Free(formal_par_list);
7587 Free(body);
7588
7589 if (fp_list->get_nof_fps() == 0) {
7590 // adding to the list of startable testcases
7591 target->functions.pre_init = mputprintf(target->functions.pre_init,
7592 "%s.add_testcase_nonpard(\"%s\", testcase_%s);\n",
7593 get_module_object_name(), dispname_str, genname_str);
7594 } else {
7595 target->functions.pre_init = mputprintf(target->functions.pre_init,
7596 "%s.add_testcase_pard(\"%s\", (genericfunc_t)&testcase_%s);\n",
7597 get_module_object_name(), dispname_str, genname_str);
7598
7599 // If every formal parameter has a default value, the testcase
7600 // might be callable after all.
7601 bool callable = true;
7602 for (size_t i = 0; i < fp_list->get_nof_fps(); ++i) {
7603 FormalPar *fp = fp_list->get_fp_byIndex(i);
7604 if (!fp->has_defval()) {
7605 callable = false;
7606 break;
7607 }
7608 }
7609
7610 if (callable) {
7611 // Write a wrapper, which acts as a no-param testcase
7612 // by calling the parameterized testcase with the default values.
7613 target->header.function_prototypes =
7614 mputprintf(target->header.function_prototypes,
7615 "extern verdicttype testcase_%s_defparams(boolean has_timer, double timer_value);\n",
7616 genname_str);
7617 target->source.function_bodies = mputprintf(target->source.function_bodies,
7618 "verdicttype testcase_%s_defparams(boolean has_timer, double timer_value) {\n"
7619 " return testcase_%s(",
7620 genname_str, genname_str);
7621
7622 for (size_t i = 0; i < fp_list->get_nof_fps(); ++i) {
7623 FormalPar *fp = fp_list->get_fp_byIndex(i);
7624 ActualPar *ap = fp->get_defval();
7625 switch (ap->get_selection()) {
7626 case ActualPar::AP_VALUE:
7627 target->source.function_bodies = mputstr(target->source.function_bodies,
7628 ap->get_Value()->get_genname_own(my_scope).c_str());
7629 break;
7630 case ActualPar::AP_TEMPLATE:
7631 target->source.function_bodies = mputstr(target->source.function_bodies,
7632 ap->get_TemplateInstance()->get_Template()->get_genname_own(my_scope).c_str());
7633 break;
7634 case ActualPar::AP_REF:
7635 target->source.function_bodies = mputstr(target->source.function_bodies,
7636 ap->get_Ref()->get_refd_assignment()->get_genname_from_scope(my_scope).c_str());
7637 break;
7638 case ActualPar::AP_DEFAULT:
7639 // Can't happen. This ActualPar was created by
7640 // Ttcn::FormalPar::chk_actual_par as the default value for
7641 // a FormalPar, and it only ever creates vale, template or ref.
7642 // no break
7643 default:
7644 FATAL_ERROR("Def_Testcase::generate_code()");
7645 }
7646
7647 // always append a comma, because has_timer and timer_value follows
7648 target->source.function_bodies = mputstrn(target->source.function_bodies,
7649 ", ", 2);
7650 }
7651
7652 target->source.function_bodies = mputstr(target->source.function_bodies,
7653 "has_timer, timer_value);\n"
7654 "}\n\n");
7655 // Add the non-parameterized wrapper *after* the parameterized one,
7656 // with the same name. Linear search will always find the first
7657 // (user-visible, parameterized) testcase.
7658 // TTCN_Module::execute_testcase knows that if after a parameterized
7659 // testcase another testcase with the same name follows,
7660 // it's the callable, non-parameterized wrapper.
7661 //
7662 // TTCN_Module::list_testcases skips parameterized testcases;
7663 // it will now list the non-parameterized wrapper.
7664 target->functions.pre_init = mputprintf(target->functions.pre_init,
7665 "%s.add_testcase_nonpard(\"%s\", testcase_%s_defparams);\n",
7666 get_module_object_name(), dispname_str, genname_str);
7667 }
7668 } // has formal parameters
7669 }
7670
7671 void Def_Testcase::generate_code(CodeGenHelper& cgh) {
7672 generate_code(cgh.get_current_outputstruct());
7673 }
7674
7675 void Def_Testcase::dump_internal(unsigned level) const
7676 {
7677 DEBUG(level, "Testcase: %s", id->get_dispname().c_str());
7678 DEBUG(level + 1, "Parameters:");
7679 fp_list->dump(level + 1);
7680 DEBUG(level + 1, "Runs on clause:");
7681 runs_on_ref->dump(level + 2);
7682 if (system_ref) {
7683 DEBUG(level + 1, "System clause:");
7684 system_ref->dump(level + 2);
7685 }
7686 DEBUG(level + 1, "Statement block:");
7687 block->dump(level + 2);
7688 }
7689
7690 void Def_Testcase::set_parent_path(WithAttribPath* p_path) {
7691 Definition::set_parent_path(p_path);
7692 if (block)
7693 block->set_parent_path(w_attrib_path);
7694 }
7695
7696 // =================================
7697 // ===== FormalPar
7698 // =================================
7699
7700 FormalPar::FormalPar(asstype_t p_asstype, Type *p_type, Identifier* p_name,
7701 TemplateInstance *p_defval, bool p_lazy_eval)
7702 : Definition(p_asstype, p_name), type(p_type), my_parlist(0),
7703 used_as_lvalue(false), template_restriction(TR_NONE),
7704 lazy_eval(p_lazy_eval), defval_generated(false), usage_found(false)
7705 {
7706 switch (p_asstype) {
7707 case A_PAR_VAL:
7708 case A_PAR_VAL_IN:
7709 case A_PAR_VAL_OUT:
7710 case A_PAR_VAL_INOUT:
7711 case A_PAR_TEMPL_IN:
7712 case A_PAR_TEMPL_OUT:
7713 case A_PAR_TEMPL_INOUT:
7714 case A_PAR_PORT:
7715 break;
7716 default:
7717 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): invalid parameter type");
7718 }
7719 if (!p_type)
7720 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): NULL pointer");
7721 type->set_ownertype(Type::OT_FORMAL_PAR, this);
7722 defval.ti = p_defval;
7723 }
7724
7725 FormalPar::FormalPar(asstype_t p_asstype,
7726 template_restriction_t p_template_restriction, Type *p_type,
7727 Identifier* p_name, TemplateInstance *p_defval, bool p_lazy_eval)
7728 : Definition(p_asstype, p_name), type(p_type), my_parlist(0),
7729 used_as_lvalue(false), template_restriction(p_template_restriction),
7730 lazy_eval(p_lazy_eval), defval_generated(false), usage_found(false)
7731 {
7732 switch (p_asstype) {
7733 case A_PAR_TEMPL_IN:
7734 case A_PAR_TEMPL_OUT:
7735 case A_PAR_TEMPL_INOUT:
7736 break;
7737 default:
7738 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): parameter not template");
7739 }
7740 if (!p_type)
7741 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): NULL pointer");
7742 type->set_ownertype(Type::OT_FORMAL_PAR, this);
7743 defval.ti = p_defval;
7744 }
7745
7746 FormalPar::FormalPar(asstype_t p_asstype, Identifier* p_name,
7747 TemplateInstance *p_defval)
7748 : Definition(p_asstype, p_name), type(0), my_parlist(0),
7749 used_as_lvalue(false), template_restriction(TR_NONE), lazy_eval(false),
7750 defval_generated(false), usage_found(false)
7751 {
7752 if (p_asstype != A_PAR_TIMER)
7753 FATAL_ERROR("Ttcn::FormalPar::FormalPar(): invalid parameter type");
7754 defval.ti = p_defval;
7755 }
7756
7757 FormalPar::~FormalPar()
7758 {
7759 delete type;
7760 if (checked) delete defval.ap;
7761 else delete defval.ti;
7762 }
7763
7764 FormalPar* FormalPar::clone() const
7765 {
7766 FATAL_ERROR("FormalPar::clone");
7767 }
7768
7769 void FormalPar::set_fullname(const string& p_fullname)
7770 {
7771 Definition::set_fullname(p_fullname);
7772 if (type) type->set_fullname(p_fullname + ".<type>");
7773 if (checked) {
7774 if (defval.ap) defval.ap->set_fullname(p_fullname + ".<default_value>");
7775 } else {
7776 if (defval.ti) defval.ti->set_fullname(p_fullname + ".<default_value>");
7777 }
7778 }
7779
7780 void FormalPar::set_my_scope(Scope *p_scope)
7781 {
7782 Definition::set_my_scope(p_scope);
7783 if (type) type->set_my_scope(p_scope);
7784 if (checked) {
7785 if (defval.ap) defval.ap->set_my_scope(p_scope);
7786 } else {
7787 if (defval.ti) defval.ti->set_my_scope(p_scope);
7788 }
7789 }
7790
7791 bool FormalPar::is_local() const
7792 {
7793 return true;
7794 }
7795
7796 Type *FormalPar::get_Type()
7797 {
7798 if (!checked) chk();
7799 if (!type) FATAL_ERROR("FormalPar::get_Type()");
7800 return type;
7801 }
7802
7803 void FormalPar::chk()
7804 {
7805 if (checked) return;
7806 checked = true;
7807 TemplateInstance *default_value = defval.ti;
7808 defval.ti = 0;
7809 if (type) {
7810 type->chk();
7811 Type *t = type->get_type_refd_last();
7812 // checks for forbidden type <-> parameter combinations
7813 switch (t->get_typetype()) {
7814 case Type::T_PORT:
7815 switch (asstype) {
7816 case A_PAR_VAL:
7817 case A_PAR_VAL_INOUT:
7818 asstype = A_PAR_PORT;
7819 break;
7820 default:
7821 error("Port type `%s' cannot be used as %s",
7822 t->get_fullname().c_str(), get_assname());
7823 }
7824 break;
7825 case Type::T_SIGNATURE:
7826 switch (asstype) {
7827 case A_PAR_TEMPL_IN:
7828 case A_PAR_TEMPL_OUT:
7829 case A_PAR_TEMPL_INOUT:
7830 break;
7831 default:
7832 error("Signature `%s' cannot be used as %s",
7833 t->get_fullname().c_str(), get_assname());
7834 }
7835 break;
7836 default:
7837 switch (asstype) {
7838 case A_PAR_PORT:
7839 case A_PAR_TIMER:
7840 FATAL_ERROR("FormalPar::chk()");
7841 case A_PAR_VAL:
7842 asstype = A_PAR_VAL_IN;
7843 default:
7844 break;
7845 }
7846 }
7847 } else if (asstype != A_PAR_TIMER) FATAL_ERROR("FormalPar::chk()");
7848
7849 if (default_value) {
7850 Error_Context cntxt(default_value, "In default value");
7851 defval.ap = chk_actual_par(default_value, Type::EXPECTED_STATIC_VALUE);
7852 delete default_value;
7853 if (!semantic_check_only)
7854 defval.ap->set_code_section(GovernedSimple::CS_POST_INIT);
7855 }
7856 }
7857
7858 bool FormalPar::has_defval() const
7859 {
7860 if (checked) return defval.ap != 0;
7861 else return defval.ti != 0;
7862 }
7863
7864 bool FormalPar::has_notused_defval() const
7865 {
7866 if (checked) FATAL_ERROR("FormalPar::has_notused_defval");
7867 if (!defval.ti || !defval.ti->get_Template())
7868 return false;
7869 return defval.ti->get_Template()->get_templatetype()
7870 == Template::TEMPLATE_NOTUSED;
7871 }
7872
7873 ActualPar *FormalPar::get_defval() const
7874 {
7875 if (!checked) FATAL_ERROR("FormalPar::get_defval()");
7876 return defval.ap;
7877 }
7878
7879 // Extract the TemplateInstance from an ActualPar.
7880 void FormalPar::set_defval(ActualPar *defpar)
7881 {
7882 // ActualPar::clone() is not implemented, since we need such a function
7883 // only here only for AP_{VALUE,TEMPLATE} parameters. AP_ERROR can also
7884 // happen for Def_Template nodes, but they will be errors later.
7885 // FIXME: This function is Def_Template specific.
7886 if (!defval.ti->get_Template() || defval.ti->get_Template()
7887 ->get_templatetype() != Template::TEMPLATE_NOTUSED)
7888 FATAL_ERROR("FormalPar::set_defval()");
7889 TemplateInstance *reversed_ti = 0;
7890 switch (defpar->get_selection()) {
7891 case ActualPar::AP_VALUE:
7892 reversed_ti = new TemplateInstance(type->clone(), 0, new Template
7893 (defpar->get_Value()->clone())); // Trust the clone().
7894 break;
7895 case ActualPar::AP_TEMPLATE:
7896 reversed_ti = defpar->get_TemplateInstance()->clone();
7897 break;
7898 case ActualPar::AP_ERROR:
7899 break; // Can happen, but let it go.
7900 case ActualPar::AP_REF:
7901 case ActualPar::AP_DEFAULT:
7902 default:
7903 FATAL_ERROR("FormalPar::set_defval()");
7904 }
7905 if (reversed_ti) {
7906 delete defval.ti;
7907 reversed_ti->set_my_scope(get_my_scope());
7908 defval.ti = reversed_ti;
7909 }
7910 }
7911
7912 ActualPar *FormalPar::chk_actual_par(TemplateInstance *actual_par,
7913 Type::expected_value_t exp_val)
7914 {
7915 if (!checked) chk();
7916 switch (asstype) {
7917 case A_PAR_VAL:
7918 case A_PAR_VAL_IN:
7919 return chk_actual_par_value(actual_par, exp_val);
7920 case A_PAR_VAL_OUT:
7921 case A_PAR_VAL_INOUT:
7922 return chk_actual_par_by_ref(actual_par, false, exp_val);
7923 case A_PAR_TEMPL_IN:
7924 return chk_actual_par_template(actual_par, exp_val);
7925 case A_PAR_TEMPL_OUT:
7926 case A_PAR_TEMPL_INOUT:
7927 return chk_actual_par_by_ref(actual_par, true, exp_val);
7928 case A_PAR_TIMER:
7929 return chk_actual_par_timer(actual_par, exp_val);
7930 case A_PAR_PORT:
7931 return chk_actual_par_port(actual_par, exp_val);
7932 default:
7933 FATAL_ERROR("FormalPar::chk_actual_par()");
7934 }
7935 return 0; // to avoid warnings
7936 }
7937
7938 ActualPar *FormalPar::chk_actual_par_value(TemplateInstance *actual_par,
7939 Type::expected_value_t exp_val)
7940 {
7941 actual_par->chk_Type(type);
7942 Ref_base *derived_ref = actual_par->get_DerivedRef();
7943 if (derived_ref) {
7944 derived_ref->error("An in-line modified template cannot be used as %s",
7945 get_assname());
7946 actual_par->chk_DerivedRef(type);
7947 }
7948 Template *ap_template = actual_par->get_Template();
7949 if (ap_template->is_Value()) {
7950 Value *v = ap_template->get_Value();
7951 v->set_my_governor(type);
7952 type->chk_this_value_ref(v);
7953 type->chk_this_value(v, 0, exp_val, INCOMPLETE_NOT_ALLOWED,
7954 OMIT_NOT_ALLOWED, SUB_CHK);
7955 return new ActualPar(v);
7956 } else {
7957 actual_par->error("A specific value without matching symbols "
7958 "was expected for a %s", get_assname());
7959 return new ActualPar();
7960 }
7961 }
7962
7963 static void chk_defpar_value(const Value* v)
7964 {
7965 Common::Reference *vref = v->get_reference();
7966 Common::Assignment *ass2 = vref->get_refd_assignment();
7967 ass2->chk();
7968 Scope *scope = ass2->get_my_scope();
7969 ComponentTypeBody *ctb = dynamic_cast<ComponentTypeBody *>(scope);
7970 if (ctb) { // this is a component variable
7971 v->error("default value cannot refer to"
7972 " a template field of the component in the `runs on' clause");
7973 }
7974 }
7975
7976 static void chk_defpar_template(const Template *body,
7977 Type::expected_value_t exp_val)
7978 {
7979 switch (body->get_templatetype()) {
7980 case Template::TEMPLATE_ERROR:
7981 break; // could be erroneous in the source; skip it
7982 case Template::TEMPLATE_NOTUSED:
7983 case Template::OMIT_VALUE:
7984 case Template::ANY_VALUE:
7985 case Template::ANY_OR_OMIT:
7986 break; // acceptable (?)
7987 case Template::TEMPLATE_INVOKE: // calling a function is not acceptable
7988 body->error("default value can not be a function invocation");
7989 break;
7990 case Template::VALUE_RANGE: {
7991 ValueRange *range = body->get_value_range();
7992 Value *low = range->get_min_v();
7993 Type::typetype_t tt_low = low->get_expr_returntype(exp_val);
7994 Value *high = range->get_max_v();
7995 Type::typetype_t tt_high = high->get_expr_returntype(exp_val);
7996 if (tt_low == tt_high) break;
7997 break; }
7998
7999 case Template::BSTR_PATTERN:
8000 case Template::HSTR_PATTERN:
8001 case Template::OSTR_PATTERN:
8002 case Template::CSTR_PATTERN:
8003 case Template::USTR_PATTERN:
8004 break; // should be acceptable in all cases (if only fixed strings possible)
8005
8006 case Template::SPECIFIC_VALUE: {
8007 Common::Value *v = body->get_specific_value();
8008 if (v->get_valuetype() == Value::V_REFD) chk_defpar_value(v);
8009 break; }
8010
8011 case Template::ALL_FROM:
8012 case Template::VALUE_LIST_ALL_FROM:
8013 FATAL_ERROR("should have been flattened");
8014 break;
8015 case Template::SUPERSET_MATCH:
8016 case Template::SUBSET_MATCH:
8017 case Template::PERMUTATION_MATCH:
8018 case Template::TEMPLATE_LIST:
8019 case Template::COMPLEMENTED_LIST:
8020 case Template::VALUE_LIST: {
8021 // in template charstring par := charstring : ("foo", "bar", "baz")
8022 size_t num = body->get_nof_comps();
8023 for (size_t i = 0; i < num; ++i) {
8024 const Template *tpl = body->get_temp_byIndex(i);
8025 chk_defpar_template(tpl, exp_val);
8026 }
8027 break; }
8028
8029 case Template::NAMED_TEMPLATE_LIST: {
8030 size_t num = body->get_nof_comps();
8031 for (size_t i = 0; i < num; ++i) {
8032 const NamedTemplate *nt = body->get_namedtemp_byIndex(i);
8033 const Template *tpl = nt->get_template();
8034 chk_defpar_template(tpl, exp_val);
8035 }
8036 break; }
8037
8038 case Template::INDEXED_TEMPLATE_LIST: {
8039 size_t num = body->get_nof_comps();
8040 for (size_t i = 0; i < num; ++i) {
8041 const IndexedTemplate *it = body->get_indexedtemp_byIndex(i);
8042 const Template *tpl = it->get_template();
8043 chk_defpar_template(tpl, exp_val);
8044 }
8045 break; }
8046
8047 case Template::TEMPLATE_REFD: {
8048 Ref_base *ref = body->get_reference();
8049
8050 Ttcn::ActualParList *aplist = ref->get_parlist();
8051 if (!aplist) break;
8052 size_t num = aplist->get_nof_pars();
8053 for (size_t i = 0; i < num; ++i) {
8054 const Ttcn::ActualPar *ap = aplist->get_par(i);
8055 deeper:
8056 switch (ap->get_selection()) {
8057 case ActualPar::AP_ERROR: {
8058 break; }
8059 case ActualPar::AP_VALUE: {
8060 Value *v = ap->get_Value(); // "v_variable" as the parameter of the template
8061 v->chk();
8062 switch (v->get_valuetype()) {
8063 case Value::V_REFD: {
8064 chk_defpar_value(v);
8065 break; }
8066 default:
8067 break;
8068 }
8069 break; }
8070 case ActualPar::AP_TEMPLATE: {
8071 // A component cannot contain a template definition, parameterized or not.
8072 // Therefore the template this actual par references, cannot be
8073 // a field of a component => no error possible, nothing to do.
8074 break; }
8075 case ActualPar::AP_REF: {
8076 // A template cannot have an out/inout parameter
8077 FATAL_ERROR("Template with out parameter?");
8078 break; }
8079 case ActualPar::AP_DEFAULT: {
8080 ap = ap->get_ActualPar();
8081 goto deeper;
8082 break; }
8083 // no default
8084 } // switch actual par selection
8085 } // next
8086
8087 break; }
8088 } // switch templatetype
8089
8090 }
8091
8092 // This function is called in two situations:
8093 // 1. FormalParList::chk calls FormalPar::chk to compute the default value
8094 // (make an ActualPar from a TemplateInstance).
8095 // In this case, defval.ti==0, and actual_par contains its old value.
8096 // This case is called only if the formal par has a default value.
8097 // 2. FormalParList::chk_actual_parlist calls FormalPar::chk_actual_par
8098 // to check the parameters supplied by the execute statement to the tc.
8099 // In this case, defval.ap has the value computed in case 1.
8100 ActualPar *FormalPar::chk_actual_par_template(TemplateInstance *actual_par,
8101 Type::expected_value_t exp_val)
8102 {
8103 actual_par->chk(type);
8104 // actual_par->template_body may change: SPECIFIC_VALUE to TEMPLATE_REFD
8105 Definition *fplist_def = my_parlist->get_my_def();
8106 // The parameter list belongs to this definition. If it's a function
8107 // or testcase, it may have a "runs on" clause.
8108 Def_Function *parent_fn = dynamic_cast<Def_Function *>(fplist_def);
8109 Type *runs_on_type = 0;
8110 if (parent_fn) runs_on_type = parent_fn->get_RunsOnType();
8111 else { // not a function; maybe a testcase
8112 Def_Testcase *parent_tc = dynamic_cast<Def_Testcase *>(fplist_def);
8113 if (parent_tc) runs_on_type = parent_tc->get_RunsOnType();
8114 }
8115 if (runs_on_type) {
8116 // If it _has_ a runs on clause, the type must be a component.
8117 if (runs_on_type->get_typetype() != Type::T_COMPONENT) FATAL_ERROR("not component?");
8118 // The default value "shall not refer to elements of the component type
8119 // in the runs on clause"
8120 ComponentTypeBody *runs_on_component = runs_on_type->get_CompBody();
8121 size_t compass = runs_on_component->get_nof_asss();
8122 for (size_t c = 0; c < compass; c++) {
8123 Assignment *ass = runs_on_component->get_ass_byIndex(c);
8124 (void)ass;
8125 }
8126 }
8127
8128 Ttcn::Template * body = actual_par->get_Template();
8129 if (exp_val == Type::EXPECTED_STATIC_VALUE
8130 ||exp_val == Type::EXPECTED_CONSTANT) {
8131 chk_defpar_template(body, exp_val);
8132 }
8133 // Rip out the type, derived ref and template from actual_par
8134 // (which may come from a function invocation or the definition
8135 // of the default value) and give it to the new ActualPar.
8136 ActualPar *ret_val = new ActualPar(
8137 new TemplateInstance(actual_par->get_Type(),
8138 actual_par->get_DerivedRef(), actual_par->get_Template()));
8139 // Zero out these members because the caller will soon call delete
8140 // on actual_par, but they now belong to ret_val.
8141 // FIXME: should this really be in here, or outside in the caller before the delete ?
8142 actual_par->release();
8143
8144 if (template_restriction!=TR_NONE) {
8145 bool needs_runtime_check =
8146 ret_val->get_TemplateInstance()->chk_restriction(
8147 "template formal parameter", template_restriction,
8148 ret_val->get_TemplateInstance());
8149 if (needs_runtime_check)
8150 ret_val->set_gen_restriction_check(template_restriction);
8151 }
8152 return ret_val;
8153 }
8154
8155 ActualPar *FormalPar::chk_actual_par_by_ref(TemplateInstance *actual_par,
8156 bool is_template, Type::expected_value_t exp_val)
8157 {
8158 Type *ap_type = actual_par->get_Type();
8159 if (ap_type) {
8160 ap_type->warning("Explicit type specification is useless for an %s",
8161 get_assname());
8162 actual_par->chk_Type(type);
8163 }
8164 Ref_base *derived_ref = actual_par->get_DerivedRef();
8165 if (derived_ref) {
8166 derived_ref->error("An in-line modified template cannot be used as %s",
8167 get_assname());
8168 actual_par->chk_DerivedRef(type);
8169 }
8170 // needed for the error messages
8171 const char *expected_string = is_template ?
8172 "template variable or template parameter" :
8173 "variable or value parameter";
8174 Template *ap_template = actual_par->get_Template();
8175 if (ap_template->is_Ref()) {
8176 Ref_base *ref = ap_template->get_Ref();
8177 Common::Assignment *ass = ref->get_refd_assignment();
8178 if (!ass) {
8179 delete ref;
8180 return new ActualPar();
8181 }
8182 bool asstype_correct = false;
8183 switch (ass->get_asstype()) {
8184 case A_PAR_VAL_IN:
8185 ass->use_as_lvalue(*ref);
8186 if (get_asstype() == A_PAR_VAL_OUT || get_asstype() == A_PAR_TEMPL_OUT) {
8187 ass->warning("Passing an `in' parameter as another function's `out' parameter");
8188 }
8189 // no break
8190 case A_VAR:
8191 case A_PAR_VAL_OUT:
8192 case A_PAR_VAL_INOUT:
8193 if (!is_template) asstype_correct = true;
8194 break;
8195 case A_PAR_TEMPL_IN:
8196 ass->use_as_lvalue(*ref);
8197 if (get_asstype() == A_PAR_VAL_OUT || get_asstype() == A_PAR_TEMPL_OUT) {
8198 ass->warning("Passing an `in' parameter as another function's `out' parameter");
8199 }
8200 // no break
8201 case A_VAR_TEMPLATE:
8202 case A_PAR_TEMPL_OUT:
8203 case A_PAR_TEMPL_INOUT:
8204 if (is_template) asstype_correct = true;
8205 break;
8206 default:
8207 break;
8208 }
8209 if (asstype_correct) {
8210 FieldOrArrayRefs *t_subrefs = ref->get_subrefs();
8211 Type *ref_type = ass->get_Type()->get_field_type(t_subrefs, exp_val);
8212 if (ref_type) {
8213 if (!type->is_identical(ref_type)) {
8214 ref->error("Type mismatch: Reference to a %s of type "
8215 "`%s' was expected instead of `%s'", expected_string,
8216 type->get_typename().c_str(), ref_type->get_typename().c_str());
8217 } else if (type->get_sub_type() && ref_type->get_sub_type() &&
8218 (type->get_sub_type()->get_subtypetype()==ref_type->get_sub_type()->get_subtypetype()) &&
8219 (!type->get_sub_type()->is_compatible(ref_type->get_sub_type()))) {
8220 ref->error("Subtype mismatch: subtype %s has no common value with subtype %s",
8221 type->get_sub_type()->to_string().c_str(),
8222 ref_type->get_sub_type()->to_string().c_str());
8223 }
8224 if (t_subrefs && t_subrefs->refers_to_string_element()) {
8225 ref->error("Reference to a string element of type `%s' cannot be "
8226 "used in this context", ref_type->get_typename().c_str());
8227 }
8228 }
8229 } else {
8230 ref->error("Reference to a %s was expected for an %s instead of %s",
8231 expected_string, get_assname(), ass->get_description().c_str());
8232 }
8233 ActualPar* ret_val_ap = new ActualPar(ref);
8234 // restriction checking if this is a reference to a template variable
8235 // this is an 'out' or 'inout' template parameter
8236 if (is_template && asstype_correct) {
8237 template_restriction_t refd_tr;
8238 switch (ass->get_asstype()) {
8239 case A_VAR_TEMPLATE: {
8240 Def_Var_Template* dvt = dynamic_cast<Def_Var_Template*>(ass);
8241 if (!dvt) FATAL_ERROR("FormalPar::chk_actual_par_by_ref()");
8242 refd_tr = dvt->get_template_restriction();
8243 } break;
8244 case A_PAR_TEMPL_IN:
8245 case A_PAR_TEMPL_OUT:
8246 case A_PAR_TEMPL_INOUT: {
8247 FormalPar* fp = dynamic_cast<FormalPar*>(ass);
8248 if (!fp) FATAL_ERROR("FormalPar::chk_actual_par_by_ref()");
8249 refd_tr = fp->get_template_restriction();
8250 } break;
8251 default:
8252 FATAL_ERROR("FormalPar::chk_actual_par_by_ref()");
8253 break;
8254 }
8255 refd_tr = Template::get_sub_restriction(refd_tr, ref);
8256 if (template_restriction!=refd_tr) {
8257 bool pre_call_check =
8258 Template::is_less_restrictive(template_restriction, refd_tr);
8259 bool post_call_check =
8260 Template::is_less_restrictive(refd_tr, template_restriction);
8261 if (pre_call_check || post_call_check) {
8262 ref->warning("Inadequate restriction on the referenced %s `%s', "
8263 "this may cause a dynamic test case error at runtime",
8264 ass->get_assname(), ref->get_dispname().c_str());
8265 ass->note("Referenced %s is here", ass->get_assname());
8266 }
8267 if (pre_call_check)
8268 ret_val_ap->set_gen_restriction_check(template_restriction);
8269 if (post_call_check)
8270 ret_val_ap->set_gen_post_restriction_check(refd_tr);
8271 }
8272 // for out and inout template parameters of external functions
8273 // always check because we do not trust user written C++ code
8274 if (refd_tr!=TR_NONE) {
8275 switch (my_parlist->get_my_def()->get_asstype()) {
8276 case A_EXT_FUNCTION:
8277 case A_EXT_FUNCTION_RVAL:
8278 case A_EXT_FUNCTION_RTEMP:
8279 ret_val_ap->set_gen_post_restriction_check(refd_tr);
8280 break;
8281 default:
8282 break;
8283 }
8284 }
8285 }
8286 return ret_val_ap;
8287 } else {
8288 actual_par->error("Reference to a %s was expected for an %s",
8289 expected_string, get_assname());
8290 return new ActualPar();
8291 }
8292 }
8293
8294 ActualPar *FormalPar::chk_actual_par_timer(TemplateInstance *actual_par,
8295 Type::expected_value_t exp_val)
8296 {
8297 Type *ap_type = actual_par->get_Type();
8298 if (ap_type) {
8299 ap_type->error("Explicit type specification cannot be used for a "
8300 "timer parameter");
8301 actual_par->chk_Type(0);
8302 }
8303 Ref_base *derived_ref = actual_par->get_DerivedRef();
8304 if (derived_ref) {
8305 derived_ref->error("An in-line modified template cannot be used as "
8306 "timer parameter");
8307 actual_par->chk_DerivedRef(0);
8308 }
8309 Template *ap_template = actual_par->get_Template();
8310 if (ap_template->is_Ref()) {
8311 Ref_base *ref = ap_template->get_Ref();
8312 Common::Assignment *ass = ref->get_refd_assignment();
8313 if (!ass) {
8314 delete ref;
8315 return new ActualPar();
8316 }
8317 switch (ass->get_asstype()) {
8318 case A_TIMER: {
8319 ArrayDimensions *dims = ass->get_Dimensions();
8320 if (dims) dims->chk_indices(ref, "timer", false, exp_val);
8321 else if (ref->get_subrefs()) ref->error("Reference to single %s "
8322 "cannot have field or array sub-references",
8323 ass->get_description().c_str());
8324 break; }
8325 case A_PAR_TIMER:
8326 if (ref->get_subrefs()) ref->error("Reference to %s cannot have "
8327 "field or array sub-references", ass->get_description().c_str());
8328 break;
8329 default:
8330 ref->error("Reference to a timer or timer parameter was expected for "
8331 "a timer parameter instead of %s", ass->get_description().c_str());
8332 }
8333 return new ActualPar(ref);
8334 } else {
8335 actual_par->error("Reference to a timer or timer parameter was "
8336 "expected for a timer parameter");
8337 return new ActualPar();
8338 }
8339 }
8340
8341 ActualPar *FormalPar::chk_actual_par_port(TemplateInstance *actual_par,
8342 Type::expected_value_t exp_val)
8343 {
8344 Type *ap_type = actual_par->get_Type();
8345 if (ap_type) {
8346 ap_type->warning("Explicit type specification is useless for a port "
8347 "parameter");
8348 actual_par->chk_Type(type);
8349 }
8350 Ref_base *derived_ref = actual_par->get_DerivedRef();
8351 if (derived_ref) {
8352 derived_ref->error("An in-line modified template cannot be used as "
8353 "port parameter");
8354 actual_par->chk_DerivedRef(type);
8355 }
8356 Template *ap_template = actual_par->get_Template();
8357 if (ap_template->is_Ref()) {
8358 Ref_base *ref = ap_template->get_Ref();
8359 Common::Assignment *ass = ref->get_refd_assignment();
8360 if (!ass) {
8361 delete ref;
8362 return new ActualPar();
8363 }
8364 bool asstype_correct = false;
8365 switch (ass->get_asstype()) {
8366 case A_PORT: {
8367 ArrayDimensions *dims = ass->get_Dimensions();
8368 if (dims) dims->chk_indices(ref, "port", false, exp_val);
8369 else if (ref->get_subrefs()) ref->error("Reference to single %s "
8370 "cannot have field or array sub-references",
8371 ass->get_description().c_str());
8372 asstype_correct = true;
8373 break; }
8374 case A_PAR_PORT:
8375 if (ref->get_subrefs()) ref->error("Reference to %s cannot have "
8376 "field or array sub-references", ass->get_description().c_str());
8377 asstype_correct = true;
8378 break;
8379 default:
8380 ref->error("Reference to a port or port parameter was expected for a "
8381 "port parameter instead of %s", ass->get_description().c_str());
8382 }
8383 if (asstype_correct) {
8384 Type *ref_type = ass->get_Type();
8385 if (ref_type && !type->is_identical(ref_type))
8386 ref->error("Type mismatch: Reference to a port or port parameter "
8387 "of type `%s' was expected instead of `%s'",
8388 type->get_typename().c_str(), ref_type->get_typename().c_str());
8389 }
8390 return new ActualPar(ref);
8391 } else {
8392 actual_par->error("Reference to a port or port parameter was expected "
8393 "for a port parameter");
8394 return new ActualPar();
8395 }
8396 }
8397
8398 void FormalPar::use_as_lvalue(const Location& p_loc)
8399 {
8400 switch (asstype) {
8401 case A_PAR_VAL_IN:
8402 case A_PAR_TEMPL_IN:
8403 break;
8404 default:
8405 FATAL_ERROR("FormalPar::use_as_lvalue()");
8406 }
8407 if (!used_as_lvalue) {
8408 Definition *my_def = my_parlist->get_my_def();
8409 if (!my_def) FATAL_ERROR("FormalPar::use_as_lvalue()");
8410 if (my_def->get_asstype() == A_TEMPLATE)
8411 p_loc.error("Parameter `%s' of the template cannot be passed further "
8412 "as `out' or `inout' parameter", id->get_dispname().c_str());
8413 else {
8414 // update the genname so that all references in the generated code
8415 // will point to the shadow object
8416 if (!lazy_eval) {
8417 set_genname(id->get_name() + "_shadow");
8418 }
8419 used_as_lvalue = true;
8420 }
8421 }
8422 }
8423
8424 char* FormalPar::generate_code_defval(char* str)
8425 {
8426 if (!defval.ap || defval_generated) return str;
8427 defval_generated = true;
8428 switch (defval.ap->get_selection()) {
8429 case ActualPar::AP_VALUE: {
8430 Value *val = defval.ap->get_Value();
8431 if (use_runtime_2 && TypeConv::needs_conv_refd(val)) {
8432 str = TypeConv::gen_conv_code_refd(str, val->get_lhs_name().c_str(), val);
8433 } else {
8434 str = val->generate_code_init(str, val->get_lhs_name().c_str());
8435 }
8436 break; }
8437 case ActualPar::AP_TEMPLATE: {
8438 TemplateInstance *ti = defval.ap->get_TemplateInstance();
8439 Template *temp = ti->get_Template();
8440 Ref_base *dref = ti->get_DerivedRef();
8441 if (dref) {
8442 expression_struct expr;
8443 Code::init_expr(&expr);
8444 expr.expr = mputprintf(expr.expr, "%s = ",
8445 temp->get_lhs_name().c_str());
8446 dref->generate_code(&expr);
8447 str = Code::merge_free_expr(str, &expr, false);
8448 }
8449 if (use_runtime_2 && TypeConv::needs_conv_refd(temp)) {
8450 str = TypeConv::gen_conv_code_refd(str, temp->get_lhs_name().c_str(), temp);
8451 } else {
8452 str = temp->generate_code_init(str, temp->get_lhs_name().c_str());
8453 }
8454 if (defval.ap->get_gen_restriction_check() != TR_NONE) {
8455 str = Template::generate_restriction_check_code(str,
8456 temp->get_lhs_name().c_str(), defval.ap->get_gen_restriction_check());
8457 }
8458 break; }
8459 case ActualPar::AP_REF:
8460 break;
8461 default:
8462 FATAL_ERROR("FormalPar::generate_code()");
8463 }
8464 return str;
8465 }
8466
8467 void FormalPar::generate_code_defval(output_struct *target, bool)
8468 {
8469 if (!defval.ap) return;
8470 switch (defval.ap->get_selection()) {
8471 case ActualPar::AP_VALUE: {
8472 Value *val = defval.ap->get_Value();
8473 const_def cdef;
8474 Code::init_cdef(&cdef);
8475 type->generate_code_object(&cdef, val);
8476 Code::merge_cdef(target, &cdef);
8477 Code::free_cdef(&cdef);
8478 break; }
8479 case ActualPar::AP_TEMPLATE: {
8480 TemplateInstance *ti = defval.ap->get_TemplateInstance();
8481 Template *temp = ti->get_Template();
8482 const_def cdef;
8483 Code::init_cdef(&cdef);
8484 type->generate_code_object(&cdef, temp);
8485 Code::merge_cdef(target, &cdef);
8486 Code::free_cdef(&cdef);
8487 break; }
8488 case ActualPar::AP_REF:
8489 break;
8490 default:
8491 FATAL_ERROR("FormalPar::generate_code()");
8492 }
8493 target->functions.post_init = generate_code_defval(target->functions.post_init);
8494 }
8495
8496 char *FormalPar::generate_code_fpar(char *str, bool display_unused /* = false */)
8497 {
8498 // the name of the parameter should not be displayed if the parameter is not
8499 // used (to avoid a compiler warning)
8500 bool display_name = (usage_found || display_unused || debugger_active ||
8501 (!enable_set_bound_out_param && (asstype == A_PAR_VAL_OUT || asstype == A_PAR_TEMPL_OUT)));
8502 const char *name_str = display_name ? id->get_name().c_str() : "";
8503 switch (asstype) {
8504 case A_PAR_VAL_IN:
8505 if (lazy_eval) {
8506 str = mputprintf(str, "Lazy_Param<%s>& %s", type->get_genname_value(my_scope).c_str(), name_str);
8507 } else {
8508 str = mputprintf(str, "const %s& %s", type->get_genname_value(my_scope).c_str(), name_str);
8509 }
8510 break;
8511 case A_PAR_VAL_OUT:
8512 case A_PAR_VAL_INOUT:
8513 case A_PAR_PORT:
8514 str = mputprintf(str, "%s& %s", type->get_genname_value(my_scope).c_str(),
8515 name_str);
8516 break;
8517 case A_PAR_TEMPL_IN:
8518 if (lazy_eval) {
8519 str = mputprintf(str, "Lazy_Param<%s>& %s", type->get_genname_template(my_scope).c_str(), name_str);
8520 } else {
8521 str = mputprintf(str, "const %s& %s", type->get_genname_template(my_scope).c_str(), name_str);
8522 }
8523 break;
8524 case A_PAR_TEMPL_OUT:
8525 case A_PAR_TEMPL_INOUT:
8526 str = mputprintf(str, "%s& %s",
8527 type->get_genname_template(my_scope).c_str(), name_str);
8528 break;
8529 case A_PAR_TIMER:
8530 str = mputprintf(str, "TIMER& %s", name_str);
8531 break;
8532 default:
8533 FATAL_ERROR("FormalPar::generate_code()");
8534 }
8535 return str;
8536 }
8537
8538 string FormalPar::get_reference_name(Scope* scope) const
8539 {
8540 string ret_val;
8541 if (lazy_eval) {
8542 ret_val += "((";
8543 switch (asstype) {
8544 case A_PAR_TEMPL_IN:
8545 ret_val += type->get_genname_template(scope);
8546 break;
8547 default:
8548 ret_val += type->get_genname_value(scope);
8549 break;
8550 }
8551 ret_val += "&)";
8552 }
8553 ret_val += get_id().get_name();
8554 if (lazy_eval) {
8555 ret_val += ")";
8556 }
8557 return ret_val;
8558 }
8559
8560 char *FormalPar::generate_code_object(char *str, const char *p_prefix, char refch)
8561 {
8562 const char *name_str = id->get_name().c_str();
8563 switch (asstype) {
8564 case A_PAR_VAL_IN:
8565 if (lazy_eval) {
8566 str = mputprintf(str, "Lazy_Param<%s> %s%s;\n", type->get_genname_value(my_scope).c_str(), p_prefix, name_str);
8567 } else {
8568 str = mputprintf(str, "%s %s%s;\n", type->get_genname_value(my_scope).c_str(), p_prefix, name_str);
8569 }
8570 break;
8571 case A_PAR_VAL_OUT:
8572 case A_PAR_VAL_INOUT:
8573 case A_PAR_PORT:
8574 str = mputprintf(str, "%s%c %s%s;\n",
8575 type->get_genname_value(my_scope).c_str(), refch, p_prefix, name_str);
8576 break;
8577 case A_PAR_TEMPL_IN:
8578 if (lazy_eval) {
8579 str = mputprintf(str, "Lazy_Param<%s> %s%s;\n", type->get_genname_template(my_scope).c_str(), p_prefix, name_str);
8580 } else {
8581 str = mputprintf(str, "%s %s%s;\n", type->get_genname_template(my_scope).c_str(), p_prefix, name_str);
8582 }
8583 break;
8584 case A_PAR_TEMPL_OUT:
8585 case A_PAR_TEMPL_INOUT:
8586 str = mputprintf(str, "%s%c %s%s;\n",
8587 type->get_genname_template(my_scope).c_str(), refch, p_prefix, name_str);
8588 break;
8589 case A_PAR_TIMER:
8590 str = mputprintf(str, "TIMER& %s%s;\n", p_prefix, name_str);
8591 break;
8592 default:
8593 FATAL_ERROR("FormalPar::generate_code_object()");
8594 }
8595 return str;
8596 }
8597
8598 char *FormalPar::generate_shadow_object(char *str) const
8599 {
8600 if (used_as_lvalue && !lazy_eval) {
8601 const string& t_genname = get_genname();
8602 const char *genname_str = t_genname.c_str();
8603 const char *name_str = id->get_name().c_str();
8604 switch (asstype) {
8605 case A_PAR_VAL_IN:
8606 str = mputprintf(str, "%s %s(%s);\n",
8607 type->get_genname_value(my_scope).c_str(), genname_str, name_str);
8608 break;
8609 case A_PAR_TEMPL_IN:
8610 str = mputprintf(str, "%s %s(%s);\n",
8611 type->get_genname_template(my_scope).c_str(), genname_str, name_str);
8612 break;
8613 default:
8614 FATAL_ERROR("FormalPar::generate_shadow_object()");
8615 }
8616 }
8617 return str;
8618 }
8619
8620 char *FormalPar::generate_code_set_unbound(char *str) const
8621 {
8622 switch (asstype) {
8623 case A_PAR_TEMPL_OUT:
8624 case A_PAR_VAL_OUT:
8625 str = mputprintf(str, "%s.clean_up();\n", id->get_name().c_str());
8626 break;
8627 default:
8628 break;
8629 }
8630 return str;
8631 }
8632
8633 void FormalPar::dump_internal(unsigned level) const
8634 {
8635 DEBUG(level, "%s: %s", get_assname(), id->get_dispname().c_str());
8636 if (type) type->dump(level + 1);
8637 if (checked) {
8638 if (defval.ap) {
8639 DEBUG(level + 1, "default value:");
8640 defval.ap->dump(level + 2);
8641 }
8642 } else {
8643 if (defval.ti) {
8644 DEBUG(level + 1, "default value:");
8645 defval.ti->dump(level + 2);
8646 }
8647 }
8648 }
8649
8650 // =================================
8651 // ===== FormalParList
8652 // =================================
8653
8654 FormalParList::~FormalParList()
8655 {
8656 size_t nof_pars = pars_v.size();
8657 for (size_t i = 0; i < nof_pars; i++) delete pars_v[i];
8658 pars_v.clear();
8659 pars_m.clear();
8660 }
8661
8662 FormalParList *FormalParList::clone() const
8663 {
8664 FATAL_ERROR("FormalParList::clone");
8665 }
8666
8667 void FormalParList::set_fullname(const string& p_fullname)
8668 {
8669 Node::set_fullname(p_fullname);
8670 for (size_t i = 0; i < pars_v.size(); i++) {
8671 FormalPar *par = pars_v[i];
8672 par->set_fullname(p_fullname + "." + par->get_id().get_dispname());
8673 }
8674 }
8675
8676 void FormalParList::set_my_scope(Scope *p_scope)
8677 {
8678 set_parent_scope(p_scope);
8679 Node::set_my_scope(p_scope);
8680 // the scope of parameters is set to the parent scope instead of this
8681 // because they cannot refer to each other
8682 for (size_t i = 0; i < pars_v.size(); i++) pars_v[i]->set_my_scope(p_scope);
8683 }
8684
8685 void FormalParList::add_fp(FormalPar *p_fp)
8686 {
8687 if (!p_fp) FATAL_ERROR("NULL parameter: Ttcn::FormalParList::add_fp()");
8688 pars_v.add(p_fp);
8689 p_fp->set_my_parlist(this);
8690 checked = false;
8691 }
8692
8693 bool FormalParList::has_notused_defval() const
8694 {
8695 for (size_t i = 0; i < pars_v.size(); i++) {
8696 if (pars_v[i]->has_notused_defval())
8697 return true;
8698 }
8699 return false;
8700 }
8701
8702 bool FormalParList::has_only_default_values() const
8703 {
8704 for (size_t i = 0; i < pars_v.size(); i++) {
8705 if (!pars_v[i]->has_defval()) {
8706 return false;
8707 }
8708 }
8709
8710 return true;
8711 }
8712
8713 bool FormalParList::has_fp_withName(const Identifier& p_name)
8714 {
8715 if (!checked) chk(Definition::A_UNDEF);
8716 return pars_m.has_key(p_name.get_name());
8717 }
8718
8719 FormalPar *FormalParList::get_fp_byName(const Identifier& p_name)
8720 {
8721 if (!checked) chk(Definition::A_UNDEF);
8722 return pars_m[p_name.get_name()];
8723 }
8724
8725 bool FormalParList::get_startability()
8726 {
8727 if(!checked) FATAL_ERROR("FormalParList::get_startability()");
8728 return is_startable;
8729 }
8730
8731 Common::Assignment *FormalParList::get_ass_bySRef(Common::Ref_simple *p_ref)
8732 {
8733 if (!p_ref || !checked) FATAL_ERROR("FormalParList::get_ass_bySRef()");
8734 if (p_ref->get_modid()) return parent_scope->get_ass_bySRef(p_ref);
8735 else {
8736 const string& name = p_ref->get_id()->get_name();
8737 if (pars_m.has_key(name)) return pars_m[name];
8738 else return parent_scope->get_ass_bySRef(p_ref);
8739 }
8740 }
8741
8742 bool FormalParList::has_ass_withId(const Identifier& p_id)
8743 {
8744 if (!checked) FATAL_ERROR("Ttcn::FormalParList::has_ass_withId()");
8745 return pars_m.has_key(p_id.get_name())
8746 || parent_scope->has_ass_withId(p_id);
8747 }
8748
8749 void FormalParList::set_genname(const string& p_prefix)
8750 {
8751 for (size_t i = 0; i < pars_v.size(); i++) {
8752 FormalPar *par = pars_v[i];
8753 const string& par_name = par->get_id().get_name();
8754 if (par->get_asstype() != Definition::A_PAR_TIMER)
8755 par->get_Type()->set_genname(p_prefix, par_name);
8756 if (par->has_defval()) {
8757 string embedded_genname(p_prefix);
8758 embedded_genname += '_';
8759 embedded_genname += par_name;
8760 embedded_genname += "_defval";
8761 ActualPar *defval = par->get_defval();
8762 switch (defval->get_selection()) {
8763 case ActualPar::AP_ERROR:
8764 case ActualPar::AP_REF:
8765 break;
8766 case ActualPar::AP_VALUE: {
8767 Value *v = defval->get_Value();
8768 v->set_genname_prefix("const_");
8769 v->set_genname_recursive(embedded_genname);
8770 break; }
8771 case ActualPar::AP_TEMPLATE: {
8772 Template *t = defval->get_TemplateInstance()->get_Template();
8773 t->set_genname_prefix("template_");
8774 t->set_genname_recursive(embedded_genname);
8775 break; }
8776 default:
8777 FATAL_ERROR("FormalParList::set_genname()");
8778 }
8779 }
8780 }
8781 }
8782
8783 void FormalParList::chk(Definition::asstype_t deftype)
8784 {
8785 if (checked) return;
8786 checked = true;
8787 min_nof_pars = 0;
8788 is_startable = true;
8789 Error_Context cntxt(this, "In formal parameter list");
8790 for (size_t i = 0; i < pars_v.size(); i++) {
8791 FormalPar *par = pars_v[i];
8792 const Identifier& id = par->get_id();
8793 const string& name = id.get_name();
8794 const char *dispname = id.get_dispname().c_str();
8795 if (pars_m.has_key(name)) {
8796 par->error("Duplicate parameter with name `%s'", dispname);
8797 pars_m[name]->note("Previous definition of `%s' is here", dispname);
8798 } else {
8799 pars_m.add(name, par);
8800 if (parent_scope && parent_scope->has_ass_withId(id)) {
8801 par->error("Parameter name `%s' is not unique in the scope "
8802 "hierarchy", dispname);
8803 Reference ref(0, id.clone());
8804 Common::Assignment *ass = parent_scope->get_ass_bySRef(&ref);
8805 if (!ass) FATAL_ERROR("FormalParList::chk()");
8806 ass->note("Symbol `%s' is already defined here in a higher scope "
8807 "unit", dispname);
8808 }
8809 }
8810 Error_Context cntxt2(par, "In parameter `%s'", dispname);
8811 par->chk();
8812 // check whether the parameter type is allowed
8813 switch (deftype) {
8814 case Definition::A_TEMPLATE:
8815 switch (par->get_asstype()) {
8816 case Definition::A_PAR_VAL_IN:
8817 case Definition::A_PAR_TEMPL_IN:
8818 // these are allowed
8819 break;
8820 default:
8821 par->error("A template cannot have %s", par->get_assname());
8822 }
8823 break;
8824 case Definition::A_TESTCASE:
8825 switch (par->get_asstype()) {
8826 case Definition::A_PAR_TIMER:
8827 case Definition::A_PAR_PORT:
8828 // these are forbidden
8829 par->error("A testcase cannot have %s", par->get_assname());
8830 default:
8831 break;
8832 }
8833 default:
8834 // everything is allowed for functions and altsteps
8835 break;
8836 }
8837 //startability chk
8838 switch(par->get_asstype()) {
8839 case Common::Assignment::A_PAR_VAL_IN:
8840 case Common::Assignment::A_PAR_TEMPL_IN:
8841 case Common::Assignment::A_PAR_VAL_INOUT:
8842 case Common::Assignment::A_PAR_TEMPL_INOUT:
8843 if (is_startable && par->get_Type()->is_component_internal())
8844 is_startable = false;
8845 break;
8846 default:
8847 is_startable = false;
8848 break;
8849 }
8850 if (!par->has_defval()) min_nof_pars = i + 1;
8851 // the last parameter without a default value determines the minimum
8852 }
8853 }
8854
8855 // check that @lazy paramterization not used in cases currently unsupported
8856 void FormalParList::chk_noLazyParams() {
8857 Error_Context cntxt(this, "In formal parameter list");
8858 for (size_t i = 0; i < pars_v.size(); i++) {
8859 FormalPar *par = pars_v[i];
8860 if (par->get_lazy_eval()) {
8861 par->error("Formal parameter `%s' cannot be @lazy, not supported in this case.",
8862 par->get_id().get_dispname().c_str());
8863 }
8864 }
8865 }
8866
8867 void FormalParList::chk_startability(const char *p_what, const char *p_name)
8868 {
8869 if(!checked) FATAL_ERROR("FormalParList::chk_startability()");
8870 if (is_startable) return;
8871 for (size_t i = 0; i < pars_v.size(); i++) {
8872 FormalPar *par = pars_v[i];
8873 switch (par->get_asstype()) {
8874 case Common::Assignment::A_PAR_VAL_IN:
8875 case Common::Assignment::A_PAR_TEMPL_IN:
8876 case Common::Assignment::A_PAR_VAL_INOUT:
8877 case Common::Assignment::A_PAR_TEMPL_INOUT:
8878 if (par->get_Type()->is_component_internal()) {
8879 map<Type*,void> type_chain;
8880 char* err_str = mprintf("a parameter or embedded in a parameter of "
8881 "a function used in a start operation. "
8882 "%s `%s' cannot be started on a parallel test component "
8883 "because of `%s'", p_what, p_name, par->get_description().c_str());
8884 par->get_Type()->chk_component_internal(type_chain, err_str);
8885 Free(err_str);
8886 }
8887 break;
8888 default:
8889 par->error("%s `%s' cannot be started on a parallel test component "
8890 "because it has %s", p_what, p_name, par->get_description().c_str());
8891 }
8892 }
8893 }
8894
8895 void FormalParList::chk_compatibility(FormalParList* p_fp_list,
8896 const char* where)
8897 {
8898 size_t nof_type_pars = pars_v.size();
8899 size_t nof_function_pars = p_fp_list->pars_v.size();
8900 // check for the number of parameters
8901 if (nof_type_pars != nof_function_pars) {
8902 p_fp_list->error("Too %s parameters: %lu was expected instead of %lu",
8903 nof_type_pars < nof_function_pars ? "many" : "few",
8904 (unsigned long) nof_type_pars, (unsigned long) nof_function_pars);
8905 }
8906 size_t upper_limit =
8907 nof_type_pars < nof_function_pars ? nof_type_pars : nof_function_pars;
8908 for (size_t i = 0; i < upper_limit; i++) {
8909 FormalPar *type_par = pars_v[i];
8910 FormalPar *function_par = p_fp_list->pars_v[i];
8911 Error_Context cntxt(function_par, "In parameter #%lu",
8912 (unsigned long) (i + 1));
8913 FormalPar::asstype_t type_par_asstype = type_par->get_asstype();
8914 FormalPar::asstype_t function_par_asstype = function_par->get_asstype();
8915 // check for parameter kind equivalence
8916 // (in, out or inout / value or template)
8917 if (type_par_asstype != function_par_asstype) {
8918 function_par->error("The kind of the parameter is not the same as in "
8919 "type `%s': %s was expected instead of %s", where,
8920 type_par->get_assname(), function_par->get_assname());
8921 }
8922 // check for type equivalence
8923 if (type_par_asstype != FormalPar::A_PAR_TIMER &&
8924 function_par_asstype != FormalPar::A_PAR_TIMER) {
8925 Type *type_par_type = type_par->get_Type();
8926 Type *function_par_type = function_par->get_Type();
8927 if (!type_par_type->is_identical(function_par_type)) {
8928 function_par_type->error("The type of the parameter is not the same "
8929 "as in type `%s': `%s' was expected instead of `%s'", where,
8930 type_par_type->get_typename().c_str(),
8931 function_par_type->get_typename().c_str());
8932 } else if (type_par_type->get_sub_type() && function_par_type->get_sub_type() &&
8933 (type_par_type->get_sub_type()->get_subtypetype()==function_par_type->get_sub_type()->get_subtypetype()) &&
8934 (!type_par_type->get_sub_type()->is_compatible(function_par_type->get_sub_type()))) {
8935 // TODO: maybe equivalence should be checked, or maybe that is too strict
8936 function_par_type->error(
8937 "Subtype mismatch: subtype %s has no common value with subtype %s",
8938 type_par_type->get_sub_type()->to_string().c_str(),
8939 function_par_type->get_sub_type()->to_string().c_str());
8940 }
8941 }
8942 // check for template restriction equivalence
8943 if (type_par->get_template_restriction()!=
8944 function_par->get_template_restriction()) {
8945 function_par->error("The template restriction of the parameter is "
8946 "not the same as in type `%s': %s restriction was expected instead "
8947 "of %s restriction", where,
8948 type_par->get_template_restriction()==TR_NONE ? "no" :
8949 Template::get_restriction_name(type_par->get_template_restriction()),
8950 function_par->get_template_restriction()==TR_NONE ? "no" :
8951 Template::get_restriction_name(function_par->
8952 get_template_restriction()));
8953 }
8954 // check for @lazy equivalence
8955 if (type_par->get_lazy_eval()!=function_par->get_lazy_eval()) {
8956 function_par->error("Parameter @lazy-ness mismatch");
8957 }
8958 // check for name equivalence
8959 const Identifier& type_par_id = type_par->get_id();
8960 const Identifier& function_par_id = function_par->get_id();
8961 if (type_par_id != function_par_id) {
8962 function_par->warning("The name of the parameter is not the same "
8963 "as in type `%s': `%s' was expected instead of `%s'", where,
8964 type_par_id.get_dispname().c_str(),
8965 function_par_id.get_dispname().c_str());
8966 }
8967 }
8968 }
8969
8970 bool FormalParList::fold_named_and_chk(ParsedActualParameters *p_paps,
8971 ActualParList *p_aplist)
8972 {
8973 const size_t num_named = p_paps->get_nof_nps();
8974 const size_t num_unnamed = p_paps->get_nof_tis();
8975 size_t num_actual = num_unnamed;
8976
8977 // Construct a map to tell us what index a FormalPar has
8978 typedef map<FormalPar*, size_t> formalpar_map_t;
8979 formalpar_map_t formalpar_map;
8980
8981 size_t num_fp = get_nof_fps();
8982 for (size_t fpx = 0; fpx < num_fp; ++fpx) {
8983 FormalPar *fp = get_fp_byIndex(fpx);
8984 formalpar_map.add(fp, new size_t(fpx));
8985 }
8986
8987 // Go through the named parameters
8988 for (size_t i = 0; i < num_named; ++i) {
8989 NamedParam *np = p_paps->extract_np_byIndex(i);
8990 // We are now responsible for np.
8991
8992 if (has_fp_withName(*np->get_name())) {
8993 // there is a formal parameter with that name
8994 FormalPar *fp = get_fp_byName(*np->get_name());
8995 const size_t is_at = *formalpar_map[fp]; // the index of the formal par
8996 if (is_at >= num_actual) {
8997 // There is no actual par in the unnamed part.
8998 // Create one from the named param.
8999
9000 // First, pad the gap with '-'
9001 for (; num_actual < is_at; ++num_actual) {
9002 Template *not_used;
9003 if (pars_v[num_actual]->has_defval()) {
9004 not_used = new Template(Template::TEMPLATE_NOTUSED);
9005 }
9006 else { // cannot use '-' if no default value
9007 not_used = new Template(Template::TEMPLATE_ERROR);
9008 }
9009 TemplateInstance *new_ti = new TemplateInstance(0, 0, not_used);
9010 // Conjure a location info at the beginning of the unnamed part
9011 // (that is, the beginning of the actual parameter list)
9012 new_ti->set_location(p_paps->get_tis()->get_filename(),
9013 p_paps->get_tis()->get_first_line(),
9014 p_paps->get_tis()->get_first_column(), 0, 0);
9015 p_paps->get_tis()->add_ti(new_ti);
9016 }
9017 TemplateInstance * namedti = np->extract_ti();
9018 p_paps->get_tis()->add_ti(namedti);
9019 ++num_actual;
9020 } else {
9021 // There is already an actual par at that position, fetch it
9022 TemplateInstance * ti = p_paps->get_tis()->get_ti_byIndex(is_at);
9023 Template::templatetype_t tt = ti->get_Template()->get_templatetype();
9024
9025 if (is_at >= num_unnamed && !ti->get_Type() && !ti->get_DerivedRef()
9026 && (tt == Template::TEMPLATE_NOTUSED || tt == Template::TEMPLATE_ERROR)) {
9027 // NotUsed in the named part => padding
9028 np->error("Named parameter `%s' out of order",
9029 np->get_name()->get_dispname().c_str());
9030 } else {
9031 // attempt to override an original unnamed param with a named one
9032 np->error("Formal parameter `%s' assigned more than once",
9033 np->get_name()->get_dispname().c_str());
9034 }
9035 }
9036 }
9037 else { // no formal parameter with that name
9038 char * nam = 0;
9039 switch (my_def->get_asstype()) {
9040 case Common::Assignment::A_TYPE: {
9041 Type *t = my_def->get_Type();
9042
9043 switch (t ? t->get_typetype() : 0) {
9044 case Type::T_FUNCTION:
9045 nam = mcopystr("Function reference");
9046 break;
9047 case Type::T_ALTSTEP:
9048 nam = mcopystr("Altstep reference");
9049 break;
9050 case Type::T_TESTCASE:
9051 nam = mcopystr("Testcase reference");
9052 break;
9053 default:
9054 FATAL_ERROR("FormalParList::chk_actual_parlist() "
9055 "Unexpected type %s", t->get_typename().c_str());
9056 } // switch(typetype)
9057 break; }
9058 default:
9059 nam = mcopystr(my_def->get_assname());
9060 break;
9061 } // switch(asstype)
9062
9063 *nam &= ~('a'-'A'); // Make the first letter uppercase
9064 p_paps->get_tis()->error("%s `%s' has no formal parameter `%s'",
9065 nam,
9066 my_def->get_fullname().c_str(),
9067 np->get_name()->get_dispname().c_str());
9068 Free(nam);
9069 }
9070 delete np;
9071 }
9072
9073 // Cleanup
9074 for (size_t fpx = 0; fpx < num_fp; ++fpx) {
9075 delete formalpar_map.get_nth_elem(fpx);
9076 }
9077 formalpar_map.clear();
9078
9079 return chk_actual_parlist(p_paps->get_tis(), p_aplist);
9080 }
9081
9082 bool FormalParList::chk_actual_parlist(TemplateInstances *p_tis,
9083 ActualParList *p_aplist)
9084 {
9085 size_t formal_pars = pars_v.size();
9086 size_t actual_pars = p_tis->get_nof_tis();
9087 // p_aplist->get_nof_pars() is usually 0 on entry
9088 bool error_flag = false;
9089
9090 if (min_nof_pars == formal_pars) {
9091 // none of the parameters have default value
9092 if (actual_pars != formal_pars) {
9093 p_tis->error("Too %s parameters: %lu was expected "
9094 "instead of %lu", actual_pars < formal_pars ? "few" : "many",
9095 (unsigned long) formal_pars, (unsigned long) actual_pars);
9096 error_flag = true;
9097 }
9098 } else {
9099 // some parameters have default value
9100 if (actual_pars < min_nof_pars) {
9101 p_tis->error("Too few parameters: at least %lu "
9102 "was expected instead of %lu",
9103 (unsigned long) min_nof_pars, (unsigned long) actual_pars);
9104 error_flag = true;
9105 } else if (actual_pars > formal_pars) {
9106 p_tis->error("Too many parameters: at most %lu "
9107 "was expected instead of %lu",
9108 (unsigned long) formal_pars, (unsigned long) actual_pars);
9109 error_flag = true;
9110 }
9111 }
9112
9113 // Do not check actual parameters in excess of the formal ones
9114 size_t upper_limit = actual_pars < formal_pars ? actual_pars : formal_pars;
9115 for (size_t i = 0; i < upper_limit; i++) {
9116 TemplateInstance *ti = p_tis->get_ti_byIndex(i);
9117
9118 // the formal parameter for the current actual parameter
9119 FormalPar *fp = pars_v[i];
9120 Error_Context cntxt(ti, "In parameter #%lu for `%s'",
9121 (unsigned long) (i + 1), fp->get_id().get_dispname().c_str());
9122 if (!ti->get_Type() && !ti->get_DerivedRef() && ti->get_Template()
9123 ->get_templatetype() == Template::TEMPLATE_NOTUSED) {
9124 if (fp->has_defval()) {
9125 ActualPar *defval = fp->get_defval();
9126 p_aplist->add(new ActualPar(defval));
9127 if (defval->is_erroneous()) error_flag = true;
9128 } else {
9129 ti->error("Not used symbol (`-') cannot be used for parameter "
9130 "that does not have default value");
9131 p_aplist->add(new ActualPar());
9132 error_flag = true;
9133 }
9134 } else if (!ti->get_Type() && !ti->get_DerivedRef() && ti->get_Template()
9135 ->get_templatetype() == Template::TEMPLATE_ERROR) {
9136 ti->error("Parameter not specified");
9137 } else {
9138 ActualPar *ap = fp->chk_actual_par(ti, Type::EXPECTED_DYNAMIC_VALUE);
9139 p_aplist->add(ap);
9140 if (ap->is_erroneous()) error_flag = true;
9141 }
9142 }
9143
9144 // The rest of formal parameters have no corresponding actual parameters.
9145 // Create actual parameters for them based on their default values
9146 // (which must exist).
9147 for (size_t i = upper_limit; i < formal_pars; i++) {
9148 FormalPar *fp = pars_v[i];
9149 if (fp->has_defval()) {
9150 ActualPar *defval = fp->get_defval();
9151 p_aplist->add(new ActualPar(defval));
9152 if (defval->is_erroneous()) error_flag = true;
9153 } else {
9154 p_aplist->add(new ActualPar()); // erroneous
9155 error_flag = true;
9156 }
9157 }
9158 return error_flag;
9159 }
9160
9161 bool FormalParList::chk_activate_argument(ActualParList *p_aplist,
9162 const char* p_description)
9163 {
9164 bool ret_val = true;
9165 for(size_t i = 0; i < p_aplist->get_nof_pars(); i++) {
9166 ActualPar *t_ap = p_aplist->get_par(i);
9167 if(t_ap->get_selection() != ActualPar::AP_REF) continue;
9168 FormalPar *t_fp = pars_v[i];
9169 switch(t_fp->get_asstype()) {
9170 case Common::Assignment::A_PAR_VAL_OUT:
9171 case Common::Assignment::A_PAR_VAL_INOUT:
9172 case Common::Assignment::A_PAR_TEMPL_OUT:
9173 case Common::Assignment::A_PAR_TEMPL_INOUT:
9174 case Common::Assignment::A_PAR_TIMER:
9175 //the checking shall be performed for these parameter types
9176 break;
9177 case Common::Assignment::A_PAR_PORT:
9178 // port parameters are always correct because ports can be defined
9179 // only in component types
9180 continue;
9181 default:
9182 FATAL_ERROR("FormalParList::chk_activate_argument()");
9183 }
9184 Ref_base *t_ref = t_ap->get_Ref();
9185 Common::Assignment *t_par_ass = t_ref->get_refd_assignment();
9186 if(!t_par_ass) FATAL_ERROR("FormalParList::chk_activate_argument()");
9187 switch (t_par_ass->get_asstype()) {
9188 case Common::Assignment::A_VAR:
9189 case Common::Assignment::A_VAR_TEMPLATE:
9190 case Common::Assignment::A_TIMER:
9191 // it is not allowed to pass references of local variables or timers
9192 if (t_par_ass->is_local()) {
9193 t_ref->error("Parameter #%lu of %s refers to %s, which is a local "
9194 "definition within a statement block and may have shorter "
9195 "lifespan than the activated default. Only references to "
9196 "variables and timers defined in the component type can be passed "
9197 "to activated defaults", (unsigned long) (i + 1), p_description,
9198 t_par_ass->get_description().c_str());
9199 ret_val = false;
9200 }
9201 break;
9202 case Common::Assignment::A_PAR_VAL_IN:
9203 case Common::Assignment::A_PAR_VAL_OUT:
9204 case Common::Assignment::A_PAR_VAL_INOUT:
9205 case Common::Assignment::A_PAR_TEMPL_IN:
9206 case Common::Assignment::A_PAR_TEMPL_OUT:
9207 case Common::Assignment::A_PAR_TEMPL_INOUT:
9208 case Common::Assignment::A_PAR_TIMER: {
9209 // it is not allowed to pass references pointing to formal parameters
9210 // except for activate() statements within testcases
9211 // note: all defaults are deactivated at the end of the testcase
9212 FormalPar *t_refd_fp = dynamic_cast<FormalPar*>(t_par_ass);
9213 if (!t_refd_fp) FATAL_ERROR("FormalParList::chk_activate_argument()");
9214 FormalParList *t_fpl = t_refd_fp->get_my_parlist();
9215 if (!t_fpl || !t_fpl->my_def)
9216 FATAL_ERROR("FormalParList::chk_activate_argument()");
9217 if (t_fpl->my_def->get_asstype() != Common::Assignment::A_TESTCASE) {
9218 t_ref->error("Parameter #%lu of %s refers to %s, which may have "
9219 "shorter lifespan than the activated default. Only references to "
9220 "variables and timers defined in the component type can be passed "
9221 "to activated defaults", (unsigned long) (i + 1), p_description,
9222 t_par_ass->get_description().c_str());
9223 ret_val = false;
9224 } }
9225 default:
9226 break;
9227 }
9228 }
9229 return ret_val;
9230 }
9231
9232 char *FormalParList::generate_code(char *str, size_t display_unused /* = 0 */)
9233 {
9234 for (size_t i = 0; i < pars_v.size(); i++) {
9235 if (i > 0) str = mputstr(str, ", ");
9236 str = pars_v[i]->generate_code_fpar(str, i < display_unused);
9237 }
9238 return str;
9239 }
9240
9241 char* FormalParList::generate_code_defval(char* str)
9242 {
9243 for (size_t i = 0; i < pars_v.size(); i++) {
9244 str = pars_v[i]->generate_code_defval(str);
9245 }
9246 return str;
9247 }
9248
9249 void FormalParList::generate_code_defval(output_struct *target)
9250 {
9251 for (size_t i = 0; i < pars_v.size(); i++) {
9252 pars_v[i]->generate_code_defval(target);
9253 }
9254 }
9255
9256 char *FormalParList::generate_code_actual_parlist(char *str,
9257 const char *p_prefix)
9258 {
9259 for (size_t i = 0; i < pars_v.size(); i++) {
9260 if (i > 0) str = mputstr(str, ", ");
9261 str = mputstr(str, p_prefix);
9262 str = mputstr(str, pars_v[i]->get_id().get_name().c_str());
9263 }
9264 return str;
9265 }
9266
9267 char *FormalParList::generate_code_object(char *str, const char *p_prefix, char refch)
9268 {
9269 for (size_t i = 0; i < pars_v.size(); i++)
9270 str = pars_v[i]->generate_code_object(str, p_prefix, refch);
9271 return str;
9272 }
9273
9274 char *FormalParList::generate_shadow_objects(char *str) const
9275 {
9276 for (size_t i = 0; i < pars_v.size(); i++)
9277 str = pars_v[i]->generate_shadow_object(str);
9278 return str;
9279 }
9280
9281 char *FormalParList::generate_code_set_unbound(char *str) const
9282 {
9283 if (enable_set_bound_out_param) return str;
9284 for (size_t i = 0; i < pars_v.size(); i++)
9285 str = pars_v[i]->generate_code_set_unbound(str);
9286 return str;
9287 }
9288
9289
9290 void FormalParList::dump(unsigned level) const
9291 {
9292 size_t nof_pars = pars_v.size();
9293 DEBUG(level, "formal parameters: %lu pcs.", (unsigned long) nof_pars);
9294 for(size_t i = 0; i < nof_pars; i++) pars_v[i]->dump(level + 1);
9295 }
9296
9297 // =================================
9298 // ===== ActualPar
9299 // =================================
9300
9301 ActualPar::ActualPar(Value *v)
9302 : Node(), selection(AP_VALUE), my_scope(0), gen_restriction_check(TR_NONE),
9303 gen_post_restriction_check(TR_NONE)
9304 {
9305 if (!v) FATAL_ERROR("ActualPar::ActualPar()");
9306 val = v;
9307 }
9308
9309 ActualPar::ActualPar(TemplateInstance *t)
9310 : Node(), selection(AP_TEMPLATE), my_scope(0),
9311 gen_restriction_check(TR_NONE), gen_post_restriction_check(TR_NONE)
9312 {
9313 if (!t) FATAL_ERROR("ActualPar::ActualPar()");
9314 temp = t;
9315 }
9316
9317 ActualPar::ActualPar(Ref_base *r)
9318 : Node(), selection(AP_REF), my_scope(0), gen_restriction_check(TR_NONE),
9319 gen_post_restriction_check(TR_NONE)
9320 {
9321 if (!r) FATAL_ERROR("ActualPar::ActualPar()");
9322 ref = r;
9323 }
9324
9325 ActualPar::ActualPar(ActualPar *a)
9326 : Node(), selection(AP_DEFAULT), my_scope(0),
9327 gen_restriction_check(TR_NONE), gen_post_restriction_check(TR_NONE)
9328 {
9329 if (!a) FATAL_ERROR("ActualPar::ActualPar()");
9330 act = a;
9331 }
9332
9333 ActualPar::~ActualPar()
9334 {
9335 switch(selection) {
9336 case AP_ERROR:
9337 break;
9338 case AP_VALUE:
9339 delete val;
9340 break;
9341 case AP_TEMPLATE:
9342 delete temp;
9343 break;
9344 case AP_REF:
9345 delete ref;
9346 break;
9347 case AP_DEFAULT:
9348 break; // nothing to do with act
9349 default:
9350 FATAL_ERROR("ActualPar::~ActualPar()");
9351 }
9352 }
9353
9354 ActualPar *ActualPar::clone() const
9355 {
9356 FATAL_ERROR("ActualPar::clone");
9357 }
9358
9359 void ActualPar::set_fullname(const string& p_fullname)
9360 {
9361 Node::set_fullname(p_fullname);
9362 switch(selection) {
9363 case AP_ERROR:
9364 break;
9365 case AP_VALUE:
9366 val->set_fullname(p_fullname);
9367 break;
9368 case AP_TEMPLATE:
9369 temp->set_fullname(p_fullname);
9370 break;
9371 case AP_REF:
9372 ref->set_fullname(p_fullname);
9373 break;
9374 case AP_DEFAULT:
9375 break;
9376 default:
9377 FATAL_ERROR("ActualPar::set_fullname()");
9378 }
9379 }
9380
9381 void ActualPar::set_my_scope(Scope *p_scope)
9382 {
9383 my_scope = p_scope;
9384 switch(selection) {
9385 case AP_ERROR:
9386 break;
9387 case AP_VALUE:
9388 val->set_my_scope(p_scope);
9389 break;
9390 case AP_TEMPLATE:
9391 temp->set_my_scope(p_scope);
9392 break;
9393 case AP_REF:
9394 ref->set_my_scope(p_scope);
9395 break;
9396 case AP_DEFAULT:
9397 switch (act->selection) {
9398 case AP_REF:
9399 ref->set_my_scope(p_scope);
9400 break;
9401 case AP_VALUE:
9402 break;
9403 case AP_TEMPLATE:
9404 break;
9405 default:
9406 FATAL_ERROR("ActualPar::set_my_scope()");
9407 }
9408 break;
9409 default:
9410 FATAL_ERROR("ActualPar::set_my_scope()");
9411 }
9412 }
9413
9414 Value *ActualPar::get_Value() const
9415 {
9416 if (selection != AP_VALUE) FATAL_ERROR("ActualPar::get_Value()");
9417 return val;
9418 }
9419
9420 TemplateInstance *ActualPar::get_TemplateInstance() const
9421 {
9422 if (selection != AP_TEMPLATE)
9423 FATAL_ERROR("ActualPar::get_TemplateInstance()");
9424 return temp;
9425 }
9426
9427 Ref_base *ActualPar::get_Ref() const
9428 {
9429 if (selection != AP_REF) FATAL_ERROR("ActualPar::get_Ref()");
9430 return ref;
9431 }
9432
9433 ActualPar *ActualPar::get_ActualPar() const
9434 {
9435 if (selection != AP_DEFAULT) FATAL_ERROR("ActualPar::get_ActualPar()");
9436 return act;
9437 }
9438
9439 void ActualPar::chk_recursions(ReferenceChain& refch)
9440 {
9441 switch (selection) {
9442 case AP_VALUE:
9443 refch.mark_state();
9444 val->chk_recursions(refch);
9445 refch.prev_state();
9446 break;
9447 case AP_TEMPLATE: {
9448 Ref_base *derived_ref = temp->get_DerivedRef();
9449 if (derived_ref) {
9450 ActualParList *parlist = derived_ref->get_parlist();
9451 if (parlist) {
9452 refch.mark_state();
9453 parlist->chk_recursions(refch);
9454 refch.prev_state();
9455 }
9456 }
9457
9458 Ttcn::Def_Template* defTemp = temp->get_Referenced_Base_Template();
9459 if (defTemp) {
9460 refch.mark_state();
9461 refch.add(defTemp->get_fullname());
9462 refch.prev_state();
9463 }
9464 refch.mark_state();
9465 temp->get_Template()->chk_recursions(refch);
9466 refch.prev_state();
9467 }
9468 default:
9469 break;
9470 }
9471 }
9472
9473 bool ActualPar::has_single_expr()
9474 {
9475 switch (selection) {
9476 case AP_VALUE:
9477 return val->has_single_expr();
9478 case AP_TEMPLATE:
9479 if (gen_restriction_check!=TR_NONE ||
9480 gen_post_restriction_check!=TR_NONE) return false;
9481 return temp->has_single_expr();
9482 case AP_REF:
9483 if (gen_restriction_check!=TR_NONE ||
9484 gen_post_restriction_check!=TR_NONE) return false;
9485 if (use_runtime_2 && ref->get_subrefs() != NULL) {
9486 FieldOrArrayRefs* subrefs = ref->get_subrefs();
9487 for (size_t i = 0; i < subrefs->get_nof_refs(); ++i) {
9488 if (FieldOrArrayRef::ARRAY_REF == subrefs->get_ref(i)->get_type()) {
9489 return false;
9490 }
9491 }
9492 }
9493 return ref->has_single_expr();
9494 case AP_DEFAULT:
9495 return true;
9496 default:
9497 FATAL_ERROR("ActualPar::has_single_expr()");
9498 return false;
9499 }
9500 }
9501
9502 void ActualPar::set_code_section(
9503 GovernedSimple::code_section_t p_code_section)
9504 {
9505 switch (selection) {
9506 case AP_VALUE:
9507 val->set_code_section(p_code_section);
9508 break;
9509 case AP_TEMPLATE:
9510 temp->set_code_section(p_code_section);
9511 break;
9512 case AP_REF:
9513 ref->set_code_section(p_code_section);
9514 default:
9515 break;
9516 }
9517 }
9518
9519 void ActualPar::generate_code(expression_struct *expr, bool copy_needed, bool lazy_param, bool used_as_lvalue) const
9520 {
9521 switch (selection) {
9522 case AP_VALUE:
9523 if (lazy_param) { // copy_needed doesn't matter in this case
9524 LazyParamData::init(used_as_lvalue);
9525 LazyParamData::generate_code(expr, val, my_scope);
9526 LazyParamData::clean();
9527 if (val->get_valuetype() == Value::V_REFD) {
9528 // check if the reference is a parameter, mark it as used if it is
9529 Reference* ref = dynamic_cast<Reference*>(val->get_reference());
9530 if (ref != NULL) {
9531 ref->ref_usage_found();
9532 }
9533 }
9534 } else {
9535 if (copy_needed) expr->expr = mputprintf(expr->expr, "%s(",
9536 val->get_my_governor()->get_genname_value(my_scope).c_str());
9537 if (use_runtime_2 && TypeConv::needs_conv_refd(val)) {
9538 // Generate everything to preamble to be able to tackle the wrapper
9539 // constructor call. TODO: Reduce the number of temporaries created.
9540 const string& tmp_id = val->get_temporary_id();
9541 const char *tmp_id_str = tmp_id.c_str();
9542 expr->preamble = mputprintf(expr->preamble, "%s %s;\n",
9543 val->get_my_governor()->get_genname_value(my_scope).c_str(),
9544 tmp_id_str);
9545 expr->preamble = TypeConv::gen_conv_code_refd(expr->preamble,
9546 tmp_id_str, val);
9547 expr->expr = mputstr(expr->expr, tmp_id_str);
9548 } else val->generate_code_expr(expr);
9549 if (copy_needed) expr->expr = mputc(expr->expr, ')');
9550 }
9551 break;
9552 case AP_TEMPLATE:
9553 if (lazy_param) { // copy_needed doesn't matter in this case
9554 LazyParamData::init(used_as_lvalue);
9555 LazyParamData::generate_code(expr, temp, gen_restriction_check, my_scope);
9556 LazyParamData::clean();
9557 if (temp->get_DerivedRef() != NULL ||
9558 temp->get_Template()->get_templatetype() == Template::TEMPLATE_REFD) {
9559 // check if the reference is a parameter, mark it as used if it is
9560 Reference* ref = dynamic_cast<Reference*>(temp->get_DerivedRef() != NULL ?
9561 temp->get_DerivedRef() : temp->get_Template()->get_reference());
9562 if (ref != NULL) {
9563 ref->ref_usage_found();
9564 }
9565 }
9566 } else {
9567 if (copy_needed)
9568 expr->expr = mputprintf(expr->expr, "%s(", temp->get_Template()
9569 ->get_my_governor()->get_genname_template(my_scope).c_str());
9570 if (use_runtime_2 && TypeConv::needs_conv_refd(temp->get_Template())) {
9571 const string& tmp_id = temp->get_Template()->get_temporary_id();
9572 const char *tmp_id_str = tmp_id.c_str();
9573 expr->preamble = mputprintf(expr->preamble, "%s %s;\n",
9574 temp->get_Template()->get_my_governor()
9575 ->get_genname_template(my_scope).c_str(), tmp_id_str);
9576 expr->preamble = TypeConv::gen_conv_code_refd(expr->preamble,
9577 tmp_id_str, temp->get_Template());
9578 // Not incorporated into gen_conv_code() yet.
9579 if (gen_restriction_check != TR_NONE)
9580 expr->preamble = Template::generate_restriction_check_code(
9581 expr->preamble, tmp_id_str, gen_restriction_check);
9582 expr->expr = mputstr(expr->expr, tmp_id_str);
9583 } else temp->generate_code(expr, gen_restriction_check);
9584 if (copy_needed) expr->expr = mputc(expr->expr, ')');
9585 }
9586 break;
9587 case AP_REF:
9588 if (lazy_param) FATAL_ERROR("ActualPar::generate_code()"); // syntax error should have already happened
9589 if (copy_needed) FATAL_ERROR("ActualPar::generate_code()");
9590 if (gen_restriction_check != TR_NONE ||
9591 gen_post_restriction_check != TR_NONE) {
9592 // generate runtime check for restricted templates
9593 // code for reference + restriction check
9594 Common::Assignment *ass = ref->get_refd_assignment();
9595 const string& tmp_id= my_scope->get_scope_mod_gen()->get_temporary_id();
9596 const char *tmp_id_str = tmp_id.c_str();
9597 expression_struct ref_expr;
9598 Code::init_expr(&ref_expr);
9599 ref->generate_code_const_ref(&ref_expr);
9600 ref_expr.preamble = mputprintf(ref_expr.preamble, "%s& %s = %s;\n",
9601 ass->get_Type()->get_genname_template(ref->get_my_scope()).c_str(),
9602 tmp_id_str, ref_expr.expr);
9603 if (gen_restriction_check != TR_NONE) {
9604 ref_expr.preamble = Template::generate_restriction_check_code(
9605 ref_expr.preamble, tmp_id_str, gen_restriction_check);
9606 }
9607 if (gen_post_restriction_check != TR_NONE) {
9608 ref_expr.postamble = Template::generate_restriction_check_code(
9609 ref_expr.postamble, tmp_id_str, gen_post_restriction_check);
9610 }
9611 // copy content of ref_expr to expr
9612 expr->preamble = mputstr(expr->preamble, ref_expr.preamble);
9613 expr->expr = mputprintf(expr->expr, "%s", tmp_id_str);
9614 expr->postamble = mputstr(expr->postamble, ref_expr.postamble);
9615 Code::free_expr(&ref_expr);
9616 } else {
9617 ref->generate_code(expr);
9618 }
9619 break;
9620 case AP_DEFAULT:
9621 if (copy_needed) FATAL_ERROR("ActualPar::generate_code()");
9622 switch (act->selection) {
9623 case AP_REF:
9624 if (lazy_param) {
9625 LazyParamData::generate_code_ap_default_ref(expr, act->ref, my_scope);
9626 } else {
9627 act->ref->generate_code(expr);
9628 }
9629 break;
9630 case AP_VALUE:
9631 if (lazy_param) {
9632 LazyParamData::generate_code_ap_default_value(expr, act->val, my_scope);
9633 } else {
9634 expr->expr = mputstr(expr->expr, act->val->get_genname_own(my_scope).c_str());
9635 }
9636 break;
9637 case AP_TEMPLATE:
9638 if (lazy_param) {
9639 LazyParamData::generate_code_ap_default_ti(expr, act->temp, my_scope);
9640 } else {
9641 expr->expr = mputstr(expr->expr, act->temp->get_Template()->get_genname_own(my_scope).c_str());
9642 }
9643 break;
9644 default:
9645 FATAL_ERROR("ActualPar::generate_code()");
9646 }
9647 break;
9648 default:
9649 FATAL_ERROR("ActualPar::generate_code()");
9650 }
9651 }
9652
9653 char *ActualPar::rearrange_init_code(char *str, Common::Module* usage_mod)
9654 {
9655 switch (selection) {
9656 case AP_VALUE:
9657 str = val->rearrange_init_code(str, usage_mod);
9658 break;
9659 case AP_TEMPLATE:
9660 str = temp->rearrange_init_code(str, usage_mod);
9661 case AP_REF:
9662 break;
9663 case AP_DEFAULT:
9664 str = act->rearrange_init_code_defval(str, usage_mod);
9665 break;
9666 default:
9667 FATAL_ERROR("ActualPar::rearrange_init_code()");
9668 }
9669 return str;
9670 }
9671
9672 char *ActualPar::rearrange_init_code_defval(char *str, Common::Module* usage_mod)
9673 {
9674 switch (selection) {
9675 case AP_VALUE:
9676 if (val->get_my_scope()->get_scope_mod_gen() == usage_mod) {
9677 str = val->generate_code_init(str, val->get_lhs_name().c_str());
9678 }
9679 break;
9680 case AP_TEMPLATE: {
9681 str = temp->rearrange_init_code(str, usage_mod);
9682 Template *t = temp->get_Template();
9683 if (t->get_my_scope()->get_scope_mod_gen() == usage_mod) {
9684 Ref_base *dref = temp->get_DerivedRef();
9685 if (dref) {
9686 expression_struct expr;
9687 Code::init_expr(&expr);
9688 expr.expr = mputprintf(expr.expr, "%s = ", t->get_lhs_name().c_str());
9689 dref->generate_code(&expr);
9690 str = Code::merge_free_expr(str, &expr, false);
9691 }
9692 str = t->generate_code_init(str, t->get_lhs_name().c_str());
9693 }
9694 break; }
9695 default:
9696 FATAL_ERROR("ActualPar::rearrange_init_code_defval()");
9697 }
9698 return str;
9699 }
9700
9701 void ActualPar::append_stringRepr(string& str) const
9702 {
9703 switch (selection) {
9704 case AP_VALUE:
9705 str += val->get_stringRepr();
9706 break;
9707 case AP_TEMPLATE:
9708 temp->append_stringRepr(str);
9709 break;
9710 case AP_REF:
9711 str += ref->get_dispname();
9712 break;
9713 case AP_DEFAULT:
9714 str += '-';
9715 break;
9716 default:
9717 str += "<erroneous actual parameter>";
9718 }
9719 }
9720
9721 void ActualPar::dump(unsigned level) const
9722 {
9723 switch (selection) {
9724 case AP_VALUE:
9725 DEBUG(level, "actual parameter: value");
9726 val->dump(level + 1);
9727 break;
9728 case AP_TEMPLATE:
9729 DEBUG(level, "actual parameter: template");
9730 temp->dump(level + 1);
9731 break;
9732 case AP_REF:
9733 DEBUG(level, "actual parameter: reference");
9734 ref->dump(level + 1);
9735 break;
9736 case AP_DEFAULT:
9737 DEBUG(level, "actual parameter: default");
9738 break;
9739 default:
9740 DEBUG(level, "actual parameter: erroneous");
9741 }
9742 }
9743
9744 // =================================
9745 // ===== ActualParList
9746 // =================================
9747
9748 ActualParList::ActualParList(const ActualParList& p)
9749 : Node(p)
9750 {
9751 size_t nof_pars = p.params.size();
9752 for (size_t i = 0; i < nof_pars; i++) params.add(p.params[i]->clone());
9753 }
9754
9755 ActualParList::~ActualParList()
9756 {
9757 size_t nof_pars = params.size();
9758 for (size_t i = 0; i < nof_pars; i++) delete params[i];
9759 params.clear();
9760 }
9761
9762 ActualParList *ActualParList::clone() const
9763 {
9764 return new ActualParList(*this);
9765 }
9766
9767 void ActualParList::set_fullname(const string& p_fullname)
9768 {
9769 Node::set_fullname(p_fullname);
9770 size_t nof_pars = params.size();
9771 for(size_t i = 0; i < nof_pars; i++)
9772 params[i]->set_fullname(p_fullname +
9773 ".<parameter" + Int2string(i + 1) + ">");
9774 }
9775
9776 void ActualParList::set_my_scope(Scope *p_scope)
9777 {
9778 size_t nof_pars = params.size();
9779 for (size_t i = 0; i < nof_pars; i++) params[i]->set_my_scope(p_scope);
9780 }
9781
9782 void ActualParList::chk_recursions(ReferenceChain& refch)
9783 {
9784 size_t nof_pars = params.size();
9785 for (size_t i = 0; i < nof_pars; i++)
9786 params[i]->chk_recursions(refch);
9787 }
9788
9789 void ActualParList::generate_code_noalias(expression_struct *expr, FormalParList *p_fpl)
9790 {
9791 size_t nof_pars = params.size();
9792 for (size_t i = 0; i < nof_pars; i++) {
9793 if (i > 0) expr->expr = mputstr(expr->expr, ", ");
9794 params[i]->generate_code(expr, false, p_fpl && p_fpl->get_fp_byIndex(i)->get_lazy_eval(), p_fpl && p_fpl->get_fp_byIndex(i)->get_used_as_lvalue());
9795 }
9796 }
9797
9798 void ActualParList::generate_code_alias(expression_struct *expr,
9799 FormalParList *p_fpl, Type *p_comptype, bool p_compself)
9800 {
9801 size_t nof_pars = params.size();
9802 // collect all value and template definitions that are passed by reference
9803 map<Common::Assignment*, void> value_refs, template_refs;
9804 for (size_t i = 0; i < nof_pars; i++) {
9805 ActualPar *par = params[i];
9806 if (par->get_selection() == ActualPar::AP_DEFAULT)
9807 par = par->get_ActualPar();
9808 if (par->get_selection() == ActualPar::AP_REF) {
9809 Common::Assignment *ass = par->get_Ref()->get_refd_assignment();
9810 switch (ass->get_asstype()) {
9811 case Common::Assignment::A_VAR:
9812 case Common::Assignment::A_PAR_VAL_IN:
9813 case Common::Assignment::A_PAR_VAL_OUT:
9814 case Common::Assignment::A_PAR_VAL_INOUT:
9815 if (!value_refs.has_key(ass)) value_refs.add(ass, 0);
9816 break;
9817 case Common::Assignment::A_VAR_TEMPLATE:
9818 case Common::Assignment::A_PAR_TEMPL_IN:
9819 case Common::Assignment::A_PAR_TEMPL_OUT:
9820 case Common::Assignment::A_PAR_TEMPL_INOUT:
9821 if (!template_refs.has_key(ass)) template_refs.add(ass, 0);
9822 default:
9823 break;
9824 }
9825 }
9826 }
9827 // walk through the parameter list and generate the code
9828 // add an extra copy constructor call to the referenced value and template
9829 // parameters if the referred definition is also passed by reference to
9830 // another parameter
9831 for (size_t i = 0; i < nof_pars; i++) {
9832 if (i > 0) expr->expr = mputstr(expr->expr, ", ");
9833 ActualPar *par = params[i];
9834 bool copy_needed = false;
9835 // the copy constructor call is not needed if the parameter is copied
9836 // into a shadow object in the body of the called function
9837 if (!p_fpl || !p_fpl->get_fp_byIndex(i)->get_used_as_lvalue()) {
9838 switch (par->get_selection()) {
9839 case ActualPar::AP_VALUE: {
9840 Value *v = par->get_Value();
9841 if (v->get_valuetype() == Value::V_REFD) {
9842 Common::Assignment *t_ass =
9843 v->get_reference()->get_refd_assignment();
9844 if (value_refs.has_key(t_ass)) {
9845 // a reference to the same variable is also passed to the called
9846 // definition
9847 copy_needed = true;
9848 } else if (p_comptype || p_compself) {
9849 // the called definition has a 'runs on' clause so it can access
9850 // component variables
9851 switch (t_ass->get_asstype()) {
9852 case Common::Assignment::A_PAR_VAL_OUT:
9853 case Common::Assignment::A_PAR_VAL_INOUT:
9854 // the parameter may be an alias of a component variable
9855 copy_needed = true;
9856 break;
9857 case Common::Assignment::A_VAR:
9858 // copy is needed if t_ass is a component variable that is
9859 // visible by the called definition
9860 if (!t_ass->is_local()) copy_needed = true;
9861 /** \todo component type compatibility: check whether t_ass is
9862 * visible from p_comptype (otherwise copy is not needed) */
9863 default:
9864 break;
9865 }
9866 }
9867 }
9868 break; }
9869 case ActualPar::AP_TEMPLATE: {
9870 TemplateInstance *ti = par->get_TemplateInstance();
9871 if (!ti->get_DerivedRef()) {
9872 Template *t = ti->get_Template();
9873 if (t->get_templatetype() == Template::TEMPLATE_REFD) {
9874 Common::Assignment *t_ass =
9875 t->get_reference()->get_refd_assignment();
9876 if (template_refs.has_key(t_ass)) {
9877 // a reference to the same variable is also passed to the called
9878 // definition
9879 copy_needed = true;
9880 } else if (p_comptype || p_compself) {
9881 // the called definition has a 'runs on' clause so it can access
9882 // component variables
9883 switch (t_ass->get_asstype()) {
9884 case Common::Assignment::A_PAR_TEMPL_OUT:
9885 case Common::Assignment::A_PAR_TEMPL_INOUT:
9886 // the parameter may be an alias of a component variable
9887 copy_needed = true;
9888 break;
9889 case Common::Assignment::A_VAR_TEMPLATE:
9890 // copy is needed if t_ass is a component variable that is
9891 // visible by the called definition
9892 if (!t_ass->is_local()) copy_needed = true;
9893 /** \todo component type compatibility: check whether t_ass is
9894 * visible from p_comptype (otherwise copy is not needed) */
9895 default:
9896 break;
9897 }
9898 }
9899 }
9900 } }
9901 default:
9902 break;
9903 }
9904 }
9905
9906 if (use_runtime_2 && ActualPar::AP_REF == par->get_selection()) {
9907 // if the parameter references an element of a record of/set of, then
9908 // the record of object needs to know, so it doesn't delete the referenced
9909 // element
9910 Ref_base* ref = par->get_Ref();
9911 FieldOrArrayRefs* subrefs = ref->get_subrefs();
9912 if (subrefs != NULL) {
9913 Common::Assignment* ass = ref->get_refd_assignment();
9914 size_t ref_i;
9915 for (ref_i = 0; ref_i < subrefs->get_nof_refs(); ++ref_i) {
9916 FieldOrArrayRef* subref = subrefs->get_ref(ref_i);
9917 if (FieldOrArrayRef::ARRAY_REF == subref->get_type()) {
9918 // set the referenced index in each array in the subrefs
9919 expression_struct array_expr;
9920 Code::init_expr(&array_expr);
9921 // the array object's name contains the reference, followed by
9922 // the subrefs before the current array ref
9923 array_expr.expr = mcopystr(LazyParamData::in_lazy() ?
9924 LazyParamData::add_ref_genname(ass, ref->get_my_scope()).c_str() :
9925 ass->get_genname_from_scope(ref->get_my_scope()).c_str());
9926 if (ref_i > 0) {
9927 subrefs->generate_code(&array_expr, ass, ref_i);
9928 }
9929 expression_struct index_expr;
9930 Code::init_expr(&index_expr);
9931 subrefs->get_ref(ref_i)->get_val()->generate_code_expr(&index_expr);
9932 // insert any preambles the array object or the index might have
9933 if (array_expr.preamble != NULL) {
9934 expr->preamble = mputstr(expr->preamble, array_expr.preamble);
9935 expr->postamble = mputstr(expr->postamble, array_expr.preamble);
9936 }
9937 if (index_expr.preamble != NULL) {
9938 expr->preamble = mputstr(expr->preamble, index_expr.preamble);
9939 expr->postamble = mputstr(expr->postamble, index_expr.preamble);
9940 }
9941 // let the array object know that the index is referenced before
9942 // calling the function, and let it know that it's now longer
9943 // referenced after the function call (this is done with the help
9944 // of the RefdIndexHandler's constructor and destructor)
9945 string tmp_id = ref->get_my_scope()->get_scope_mod_gen()->get_temporary_id();
9946 expr->preamble = mputprintf(expr->preamble,
9947 "RefdIndexHandler %s(&%s, %s);\n",
9948 tmp_id.c_str(), array_expr.expr, index_expr.expr);
9949 // insert any postambles the array object or the index might have
9950 if (array_expr.postamble != NULL) {
9951 expr->preamble = mputstr(expr->preamble, array_expr.postamble);
9952 expr->postamble = mputstr(expr->postamble, array_expr.postamble);
9953 }
9954 if (index_expr.postamble != NULL) {
9955 expr->preamble = mputstr(expr->preamble, index_expr.postamble);
9956 expr->postamble = mputstr(expr->postamble, index_expr.postamble);
9957 }
9958 Code::free_expr(&array_expr);
9959 Code::free_expr(&index_expr);
9960 } // if (FieldOrArrayRef::ARRAY_REF == subref->get_type())
9961 } // for cycle
9962 } // if (subrefs != NULL)
9963 } // if (ActualPar::AP_REF == par->get_selection())
9964
9965 par->generate_code(expr, copy_needed, p_fpl && p_fpl->get_fp_byIndex(i)->get_lazy_eval(), p_fpl && p_fpl->get_fp_byIndex(i)->get_used_as_lvalue());
9966 }
9967 value_refs.clear();
9968 template_refs.clear();
9969 }
9970
9971 char *ActualParList::rearrange_init_code(char *str, Common::Module* usage_mod)
9972 {
9973 for (size_t i = 0; i < params.size(); i++)
9974 str = params[i]->rearrange_init_code(str, usage_mod);
9975 return str;
9976 }
9977
9978 void ActualParList::dump(unsigned level) const
9979 {
9980 DEBUG(level, "actual parameter list: %lu parameters",
9981 (unsigned long) params.size());
9982 for (size_t i = 0; i < params.size(); i++)
9983 params[i]->dump(level + 1);
9984 }
9985 }
This page took 0.254682 seconds and 4 git commands to generate.