1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
8 #include "Attributes.hh"
10 #include "../CompilerError.hh"
12 #include "TtcnTemplate.hh"
16 // ==== Qualifier ====
18 Qualifier
* Qualifier::clone() const
20 return new Qualifier(*this);
23 const Identifier
* Qualifier::get_identifier(size_t p_index
) const
25 FieldOrArrayRef
* ref
= get_ref(p_index
);
26 return (ref
->get_type()==FieldOrArrayRef::FIELD_REF
) ?
31 Qualifier
* Qualifier::get_qualifier_without_first_id() const
33 Qualifier
* temp
= this->clone();
38 string
Qualifier::get_stringRepr() const
41 append_stringRepr(str_repr
);
42 if (str_repr
[0]=='.') str_repr
.replace(0, 1, ""); // remove leading dot if present
46 void Qualifier::dump(unsigned level
) const
48 const string
& rep
= get_stringRepr();
49 DEBUG(level
, "(%s)", rep
.c_str());
52 // ==== Qualifiers ====
54 Qualifiers::~Qualifiers()
56 for(size_t i
= 0; i
< qualifiers
.size(); i
++)
63 Qualifiers::Qualifiers(const Qualifiers
& p
)
66 for(size_t i
= 0; i
< p
.get_nof_qualifiers(); i
++)
68 qualifiers
.add(p
.get_qualifier(i
)->clone());
72 void Qualifiers::add_qualifier(Qualifier
* p_qualifier
)
74 if(p_qualifier
->get_nof_identifiers() > 0)
75 qualifiers
.add(p_qualifier
);
80 void Qualifiers::delete_qualifier(size_t p_index
)
82 delete qualifiers
[p_index
];
83 qualifiers
.replace(p_index
,1,NULL
);
86 const Qualifier
* Qualifiers::get_qualifier(size_t p_index
) const
88 return qualifiers
[p_index
];
91 Qualifiers
* Qualifiers::clone() const
93 return new Qualifiers(*this);
96 void Qualifiers::set_fullname(const string
& p_fullname
)
98 Node::set_fullname(p_fullname
);
99 for(size_t i
= 0; i
< qualifiers
.size(); i
++)
101 qualifiers
[i
]->set_fullname(p_fullname
+ ".<qualifier "
102 + Int2string(i
) + ">");
106 bool Qualifiers::has_qualifier(Qualifier
* p_qualifier
) const
111 for(size_t i
= 0; i
< qualifiers
.size() && !result
; i
++)
113 if(qualifiers
[i
]->get_nof_identifiers()
114 ==p_qualifier
->get_nof_identifiers())
118 while(index
< p_qualifier
->get_nof_identifiers() && same
)
120 same
= (*qualifiers
[i
]->get_identifier(index
)
121 == *p_qualifier
->get_identifier(index
));
131 void Qualifiers::dump(unsigned level
) const
133 DEBUG(level
, "has %lu qualifiers",
134 (unsigned long)get_nof_qualifiers());
135 for(size_t i
= 0; i
< qualifiers
.size(); i
++)
136 qualifiers
[i
]->dump(level
);
139 // ==== ErroneousDescriptor ====
141 ErroneousDescriptor::~ErroneousDescriptor()
143 for (size_t i
=0; i
<descr_m
.size(); i
++) delete descr_m
.get_nth_elem(i
);
145 for (size_t i
=0; i
<values_m
.size(); i
++) delete values_m
.get_nth_elem(i
);
149 // ==== ErroneousAttributeSpec ====
151 ErroneousAttributeSpec::ErroneousAttributeSpec(bool p_is_raw
, indicator_t p_indicator
, TemplateInstance
* p_tmpl_inst
, bool p_has_all_keyword
):
152 is_raw(p_is_raw
), has_all_keyword(p_has_all_keyword
), indicator(p_indicator
),
153 tmpl_inst(p_tmpl_inst
), type(0), value(0)
155 if (!tmpl_inst
) FATAL_ERROR("ErroneousAttributeSpec::ErroneousAttributeSpec()");
158 ErroneousAttributeSpec::ErroneousAttributeSpec(const ErroneousAttributeSpec
& p
)
159 : Node(p
), Location(p
), is_raw(p
.is_raw
), has_all_keyword(p
.has_all_keyword
),
160 indicator(p
.indicator
), type(0), value(0)
162 tmpl_inst
= p
.tmpl_inst
? tmpl_inst
->clone() : 0;
165 ErroneousAttributeSpec::~ErroneousAttributeSpec()
171 ErroneousAttributeSpec
* ErroneousAttributeSpec::clone() const
173 return new ErroneousAttributeSpec(*this);
176 void ErroneousAttributeSpec::set_fullname(const string
& p_fullname
)
178 Node::set_fullname(p_fullname
);
179 tmpl_inst
->set_fullname(p_fullname
+".<template_instance>");
182 void ErroneousAttributeSpec::set_my_scope(Scope
*p_scope
)
184 tmpl_inst
->set_my_scope(p_scope
);
187 void ErroneousAttributeSpec::dump(unsigned level
) const
189 DEBUG(level
, "raw: %s", is_raw
? "yes" : "no");
190 DEBUG(level
, "indicator:");
193 DEBUG(level
+1, "before");
196 DEBUG(level
+1, "value");
199 DEBUG(level
+1, "after");
202 DEBUG(level
+1, "<invalid>");
205 FATAL_ERROR("ErroneousAttributeSpec::dump()");
207 DEBUG(level
, "template instance:");
208 tmpl_inst
->dump(level
+1);
211 bool ErroneousAttributeSpec::get_is_omit() const
213 return (tmpl_inst
->get_Template()->get_templatetype()==Template::OMIT_VALUE
);
216 void ErroneousAttributeSpec::chk()
218 if (get_is_omit()) { // special case, no type needed
219 if ((indicator
==I_BEFORE
)||(indicator
==I_AFTER
)) {
220 if (!has_all_keyword
) {
222 "Keyword `all' is expected after `omit' when omitting all fields %s the specified field",
223 get_indicator_str(indicator
));
226 if (has_all_keyword
) {
228 "Unexpected `all' keyword after `omit' when omitting one field");
234 if (has_all_keyword
) {
236 "Unexpected `all' keyword after the in-line template");
239 // determine the type of the tmpl_inst
240 type
= tmpl_inst
->get_expr_governor(Type::EXPECTED_TEMPLATE
);
242 tmpl_inst
->get_Template()->set_lowerid_to_ref();
243 type
= tmpl_inst
->get_expr_governor(Type::EXPECTED_TEMPLATE
);
246 tmpl_inst
->error("Cannot determine the type of the in-line template");
250 Type
* type_last
= type
->get_type_refd_last();
251 if (!type_last
|| type_last
->get_typetype()==Type::T_ERROR
) {
256 switch (type_last
->get_typetype_ttcn3()) {
263 tmpl_inst
->error("An in-line template of type `%s' cannot be used as a `raw' erroneous value",
264 type_last
->get_typename().c_str());
268 if (tmpl_inst
->get_DerivedRef()) {
269 tmpl_inst
->error("Reference to a constant value was expected instead of an in-line modified template");
274 Template
* templ
= tmpl_inst
->get_Template();
276 if (templ
->is_Value()) {
277 value
= templ
->get_Value();
278 value
->set_my_governor(type
);
279 type
->chk_this_value_ref(value
);
280 type
->chk_this_value(value
, 0, Type::EXPECTED_CONSTANT
,
281 INCOMPLETE_NOT_ALLOWED
, OMIT_NOT_ALLOWED
, SUB_CHK
);
282 //{ FIXME: make this work
283 // ReferenceChain refch(type, "While checking embedded recursions");
284 // value->chk_recursions(refch);
286 value
->set_code_section(GovernedSimple::CS_PRE_INIT
);
288 tmpl_inst
->error("A specific value without matching symbols was expected");
294 const char* ErroneousAttributeSpec::get_indicator_str(indicator_t i
)
297 case I_BEFORE
: return "before";
298 case I_VALUE
: return "value";
299 case I_AFTER
: return "after";
300 default: FATAL_ERROR("ErroneousAttributeSpec::get_indicator_str()");
305 char* ErroneousAttributeSpec::generate_code_str(char *str
, string genname
)
307 if (get_is_omit()) return str
;
308 if (!type
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_str()");
309 if (!value
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_str()");
310 if (first_genname
.empty()) { // this is the first use
311 str
= mputprintf(str
, "static %s %s;\n",
312 type
->get_genname_value(value
->get_my_scope()).c_str(), genname
.c_str());
313 first_genname
= genname
;
315 str
= mputprintf(str
, "static %s& %s = %s;\n",
316 type
->get_genname_value(value
->get_my_scope()).c_str(),
317 genname
.c_str(), first_genname
.c_str());
322 char* ErroneousAttributeSpec::generate_code_init_str(char *str
, string genname
)
324 if (get_is_omit()) return str
;
325 if (!type
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_init_str()");
326 if (!value
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_init_str()");
327 str
= value
->generate_code_init(str
, genname
.c_str());
331 string
ErroneousAttributeSpec::get_typedescriptor_str()
333 if (get_is_omit() || is_raw
) return string("NULL");
334 if (!type
) FATAL_ERROR("ErroneousAttributeSpec::get_typedescriptor_str()");
335 if (!value
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_ti_str()");
336 return ( "&" + type
->get_genname_typedescriptor(value
->get_my_scope()) + "_descr_" );
339 void ErroneousAttributeSpec::chk_recursions(ReferenceChain
& refch
)
343 value
->chk_recursions(refch
);
348 // ==== ErroneousValues ====
350 char* ErroneousValues::generate_code_embedded_str(char *str
, string genname
)
352 if (before
) str
= generate_code_embedded_str(str
, genname
+"_before", before
);
353 if (value
) str
= generate_code_embedded_str(str
, genname
+"_value", value
);
354 if (after
) str
= generate_code_embedded_str(str
, genname
+"_after", after
);
358 char* ErroneousValues::generate_code_embedded_str(char *str
, string genname
, ErroneousAttributeSpec
* attr_spec
)
360 str
= attr_spec
->generate_code_str(str
, genname
+"_errval");
361 str
= mputprintf(str
, "static Erroneous_value_t %s = { %s, %s, %s };\n", genname
.c_str(),
362 attr_spec
->get_is_raw() ? "true" : "false",
363 attr_spec
->get_is_omit() ? "NULL" : ("&"+genname
+"_errval").c_str(),
364 attr_spec
->get_typedescriptor_str().c_str());
368 char* ErroneousValues::generate_code_init_str(char *str
, string genname
)
370 if (before
) str
= before
->generate_code_init_str(str
, genname
+"_before_errval");
371 if (value
) str
= value
->generate_code_init_str(str
, genname
+"_value_errval");
372 if (after
) str
= after
->generate_code_init_str(str
, genname
+"_after_errval");
376 char* ErroneousValues::generate_code_struct_str(char *str
, string genname
, int field_index
)
378 str
= mputprintf(str
, "{ %d, %s, %s, %s, %s }", field_index
,
379 ("\""+field_name
+"\"").c_str(),
380 before
? ("&"+genname
+"_before").c_str() : "NULL",
381 value
? ("&"+genname
+"_value").c_str() : "NULL",
382 after
? ("&"+genname
+"_after").c_str() : "NULL");
386 void ErroneousValues::chk_recursions(ReferenceChain
& refch
)
388 if (before
) before
->chk_recursions(refch
);
389 if (value
) value
->chk_recursions(refch
);
390 if (after
) after
->chk_recursions(refch
);
393 // ==== ErroneousDescriptor ====
395 char* ErroneousDescriptor::generate_code_embedded_str(char *str
, string genname
)
398 for (size_t i
=0; i
<values_m
.size(); i
++) {
399 str
= values_m
.get_nth_elem(i
)->generate_code_embedded_str(str
, genname
+"_v"+Int2string((int)values_m
.get_nth_key(i
)));
401 // embedded descriptors
402 for (size_t i
=0; i
<descr_m
.size(); i
++) {
403 str
= descr_m
.get_nth_elem(i
)->generate_code_embedded_str(str
, genname
+"_d"+Int2string((int)descr_m
.get_nth_key(i
)));
406 if (values_m
.size()>0) {
407 str
= mputprintf(str
, "static Erroneous_values_t %s_valsvec[%d] = { ", genname
.c_str(), (int)values_m
.size());
408 for (size_t i
=0; i
<values_m
.size(); i
++) {
409 if (i
>0) str
= mputstr(str
, ", ");
410 int key_i
= (int)values_m
.get_nth_key(i
);
411 str
= values_m
.get_nth_elem(i
)->generate_code_struct_str(str
, genname
+"_v"+Int2string(key_i
), key_i
);
413 str
= mputstr(str
, " };\n");
415 // embedded descriptor vector
416 if (descr_m
.size()>0) {
417 str
= mputprintf(str
, "static Erroneous_descriptor_t %s_embvec[%d] = { ", genname
.c_str(), (int)descr_m
.size());
418 for (size_t i
=0; i
<descr_m
.size(); i
++) {
419 if (i
>0) str
= mputstr(str
, ", ");
420 int key_i
= (int)descr_m
.get_nth_key(i
);
421 str
= descr_m
.get_nth_elem(i
)->generate_code_struct_str(str
, genname
+"_d"+Int2string(key_i
), key_i
);
423 str
= mputstr(str
, " };\n");
428 char* ErroneousDescriptor::generate_code_init_str(char *str
, string genname
)
430 for (size_t i
=0; i
<values_m
.size(); i
++) {
431 str
= values_m
.get_nth_elem(i
)->generate_code_init_str(str
, genname
+"_v"+Int2string((int)values_m
.get_nth_key(i
)));
433 for (size_t i
=0; i
<descr_m
.size(); i
++) {
434 str
= descr_m
.get_nth_elem(i
)->generate_code_init_str(str
, genname
+"_d"+Int2string((int)descr_m
.get_nth_key(i
)));
439 char* ErroneousDescriptor::generate_code_struct_str(char *str
, string genname
, int field_index
)
441 string genname_values_vec
= genname
+ "_valsvec";
442 string genname_embedded_vec
= genname
+ "_embvec";
443 str
= mputprintf(str
, "{ %d, %d, %s, %d, %s, %d, %s, %d, %s }",
445 omit_before
, (omit_before
==-1)?"NULL":("\""+omit_before_name
+"\"").c_str(),
446 omit_after
, (omit_after
==-1)?"NULL":("\""+omit_after_name
+"\"").c_str(),
447 (int)values_m
.size(), (values_m
.size()>0) ? genname_values_vec
.c_str(): "NULL",
448 (int)descr_m
.size(), (descr_m
.size()>0) ? genname_embedded_vec
.c_str(): "NULL");
452 void ErroneousDescriptor::chk_recursions(ReferenceChain
& refch
)
454 for (size_t i
=0; i
<values_m
.size(); i
++) {
455 values_m
.get_nth_elem(i
)->chk_recursions(refch
);
457 for (size_t i
=0; i
<descr_m
.size(); i
++) {
458 descr_m
.get_nth_elem(i
)->chk_recursions(refch
);
462 char* ErroneousDescriptor::generate_code_str(char *str
, string genname
)
464 genname
+= "_err_descr";
465 str
= generate_code_embedded_str(str
, genname
);
466 str
= mputprintf(str
, "static Erroneous_descriptor_t %s = ", genname
.c_str());
467 str
= generate_code_struct_str(str
, genname
, -1);
468 str
= mputstr(str
, ";\n");
472 // ==== ErroneousAttributes ====
474 ErroneousAttributes::ErroneousAttributes(Type
* p_type
):
475 type(p_type
), err_descr_tree(NULL
)
477 if (!type
) FATAL_ERROR("ErroneousAttributes::ErroneousAttributes()");
480 ErroneousAttributes::~ErroneousAttributes()
482 for (size_t i
=0; i
<spec_vec
.size(); i
++) {
486 if (err_descr_tree
) delete err_descr_tree
;
489 ErroneousAttributes::ErroneousAttributes(const ErroneousAttributes
& p
)
490 : Node(p
), type(p
.type
), err_descr_tree(NULL
)
494 ErroneousAttributes
* ErroneousAttributes::clone() const
496 return new ErroneousAttributes(*this);
499 void ErroneousAttributes::set_fullname(const string
& p_fullname
)
501 Node::set_fullname(p_fullname
);
502 for (size_t i
=0; i
<spec_vec
.size(); i
++) {
503 spec_vec
[i
]->set_fullname(p_fullname
+".<erroneous_attr_spec_"+Int2string(i
)+">");
507 void ErroneousAttributes::dump(unsigned level
) const
509 DEBUG(level
, "erroneous attributes:");
510 for (size_t i
=0; i
<spec_vec
.size(); i
++) {
511 spec_vec
[i
]->dump(level
+1);
515 void ErroneousAttributes::add_spec(ErroneousAttributeSpec
* err_attr_spec
)
517 if (!err_attr_spec
) FATAL_ERROR("ErroneousAttributes::add_spec()");
518 spec_vec
.add(err_attr_spec
);
521 void ErroneousAttributes::add_pair(const Qualifier
* qualifier
, ErroneousAttributeSpec
* err_attr_spec
)
523 if (!qualifier
|| !err_attr_spec
) FATAL_ERROR("ErroneousAttributes::add()");
525 f
.qualifier
= qualifier
;
526 f
.err_attr
= err_attr_spec
;
530 void ErroneousAttributes::chk()
532 // check that encodings of erroneous type and templateinstance type match
533 for (size_t i
=0; i
<spec_vec
.size(); i
++) {
534 ErroneousAttributeSpec
* act_attr
= spec_vec
[i
];
535 Type
* ti_type
= act_attr
->get_type();
536 if ((act_attr
->get_indicator()!=ErroneousAttributeSpec::I_INVALID
) && ti_type
) {
537 if (act_attr
->get_is_raw()) {
538 switch (ti_type
->get_typetype_ttcn3()) {
540 if (!type
->has_encoding(Type::CT_PER
) && !type
->has_encoding(Type::CT_RAW
)) {
541 act_attr
->error("A `raw' %s value was used for erroneous type `%s' which has no RAW or PER encodings.",
542 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
546 if (!type
->has_encoding(Type::CT_TEXT
) && !type
->has_encoding(Type::CT_XER
) &&
547 !type
->has_encoding(Type::CT_JSON
)) {
548 act_attr
->error("A `raw' %s value was used for erroneous type `%s' which has no TEXT, XER or JSON encodings.",
549 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
553 if (!type
->has_encoding(Type::CT_XER
) && !type
->has_encoding(Type::CT_JSON
)) {
554 act_attr
->error("A `raw' %s value was used for erroneous type `%s' which has no XER or JSON encoding.",
555 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
562 // the two types must have at least one common encoding
563 if (!((type
->has_encoding(Type::CT_BER
)&&ti_type
->has_encoding(Type::CT_BER
)) ||
564 (type
->has_encoding(Type::CT_PER
)&&ti_type
->has_encoding(Type::CT_PER
)) ||
565 (type
->has_encoding(Type::CT_RAW
)&&ti_type
->has_encoding(Type::CT_RAW
)) ||
566 (type
->has_encoding(Type::CT_TEXT
)&&ti_type
->has_encoding(Type::CT_TEXT
)) ||
567 (type
->has_encoding(Type::CT_XER
)&&ti_type
->has_encoding(Type::CT_XER
)) ||
568 (type
->has_encoding(Type::CT_JSON
)&&ti_type
->has_encoding(Type::CT_JSON
)))) {
569 act_attr
->error("Type `%s' and type `%s' have no common encoding",
570 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
576 // for every erroneous field calculate the corresponding index and type arrays
577 // for example: x[5].z -> [3,5,2] and [MyRec,MyRecOf,MyUnion]
578 // MyRec.x field has index 3, etc.
579 for (size_t i
=0; i
<field_array
.size(); i
++) {
580 bool b
= type
->get_subrefs_as_array(field_array
[i
].qualifier
, field_array
[i
].subrefs_array
, field_array
[i
].type_array
);
581 if (!b
) FATAL_ERROR("ErroneousAttributes::chk()");
583 // check the qualifiers and build the tree
584 err_descr_tree
= build_descr_tree(field_array
);
587 ErroneousDescriptor
* ErroneousAttributes::build_descr_tree(dynamic_array
<field_err_t
>& fld_array
)
589 ErroneousDescriptor
* err_descr
= new ErroneousDescriptor();
590 const Qualifier
* omit_before_qual
= NULL
, * omit_after_qual
= NULL
;
591 map
< size_t, dynamic_array
<field_err_t
> > embedded_fld_array_m
; // used for recursive calls
592 for (size_t i
=0; i
<fld_array
.size(); i
++) {
593 field_err_t
& act_field_err
= fld_array
[i
];
594 ErroneousAttributeSpec::indicator_t act_indicator
= act_field_err
.err_attr
->get_indicator();
595 bool is_omit
= act_field_err
.err_attr
->get_is_omit();
596 if (act_field_err
.subrefs_array
.size()<1) FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
597 size_t fld_idx
= act_field_err
.subrefs_array
[0];
598 if (omit_before_qual
&& (err_descr
->omit_before
!=-1) && (err_descr
->omit_before
>(int)fld_idx
)) {
599 act_field_err
.qualifier
->error(
600 "Field `%s' cannot be referenced because all fields before field `%s' have been omitted",
601 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_before_qual
->get_stringRepr().c_str());
604 if (omit_after_qual
&& (err_descr
->omit_after
!=-1) && (err_descr
->omit_after
<(int)fld_idx
)) {
605 act_field_err
.qualifier
->error(
606 "Field `%s' cannot be referenced because all fields after field `%s' have been omitted",
607 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_after_qual
->get_stringRepr().c_str());
610 if (act_field_err
.subrefs_array
.size()==1) { // erroneous value
611 if (act_field_err
.type_array
.size()!=1) FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
612 if ((act_field_err
.type_array
[0]->get_typetype()==Type::T_SET_A
) &&
613 is_omit
&& (act_indicator
!=ErroneousAttributeSpec::I_VALUE
)) {
614 act_field_err
.qualifier
->error(
615 "Cannot omit all fields %s `%s' which is a field of an ASN.1 SET type",
616 ErroneousAttributeSpec::get_indicator_str(act_indicator
), act_field_err
.qualifier
->get_stringRepr().c_str());
617 act_field_err
.qualifier
->note(
618 "The order of fields in ASN.1 SET types changes depending on tagging (see X.690 9.3). "
619 "Fields can be omitted individually, independently of the field order which depends on tagging");
622 switch (act_field_err
.type_array
[0]->get_typetype_ttcn3()) {
623 case Type::T_CHOICE_T
:
624 if (act_indicator
!=ErroneousAttributeSpec::I_VALUE
) {
625 act_field_err
.qualifier
->error(
626 "Indicator `%s' cannot be used with reference `%s' which points to a field of a union type",
627 ErroneousAttributeSpec::get_indicator_str(act_indicator
), act_field_err
.qualifier
->get_stringRepr().c_str());
633 if (is_omit
&& (act_indicator
==ErroneousAttributeSpec::I_AFTER
) &&
634 (fld_idx
==act_field_err
.type_array
[0]->get_nof_comps()-1)) {
635 act_field_err
.qualifier
->error(
636 "There is nothing to omit after the last field (%s) of a record/set type",
637 act_field_err
.qualifier
->get_stringRepr().c_str());
643 if (is_omit
&& (act_indicator
==ErroneousAttributeSpec::I_BEFORE
) &&
645 act_field_err
.qualifier
->error(
646 "There is nothing to omit before the first field (%s)",
647 act_field_err
.qualifier
->get_stringRepr().c_str());
654 // check for duplicate value+indicator
655 if (err_descr
->values_m
.has_key(fld_idx
)) {
656 ErroneousValues
* evs
= err_descr
->values_m
[fld_idx
];
657 if ( (evs
->before
&& (act_indicator
==ErroneousAttributeSpec::I_BEFORE
)) ||
658 (evs
->value
&& (act_indicator
==ErroneousAttributeSpec::I_VALUE
)) ||
659 (evs
->after
&& (act_indicator
==ErroneousAttributeSpec::I_AFTER
)) ) {
660 act_field_err
.qualifier
->error(
661 "Duplicate reference to field `%s' with indicator `%s'",
662 act_field_err
.qualifier
->get_stringRepr().c_str(), ErroneousAttributeSpec::get_indicator_str(act_indicator
));
666 // when overwriting a value check if embedded values were used
667 if ((act_indicator
==ErroneousAttributeSpec::I_VALUE
) && embedded_fld_array_m
.has_key(fld_idx
)) {
668 act_field_err
.qualifier
->error(
669 "Reference to field `%s' with indicator `value' would invalidate previously specified erroneous data",
670 act_field_err
.qualifier
->get_stringRepr().c_str());
673 // if before/after omit then check that no references to omitted regions and no duplication of omit before/after rule
674 if ((act_indicator
==ErroneousAttributeSpec::I_BEFORE
) && is_omit
) {
675 if (omit_before_qual
&& (err_descr
->omit_before
!=-1)) {
676 act_field_err
.qualifier
->error(
677 "Duplicate rule for omitting all fields before the specified field. Used on field `%s' but previously already used on field `%s'",
678 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_before_qual
->get_stringRepr().c_str());
681 bool is_invalid
= false;
682 for (size_t j
=0; j
<err_descr
->values_m
.size(); j
++) {
683 if (err_descr
->values_m
.get_nth_key(j
)<fld_idx
) {
689 for (size_t j
=0; j
<embedded_fld_array_m
.size(); j
++) {
690 if (embedded_fld_array_m
.get_nth_key(j
)<fld_idx
) {
697 act_field_err
.qualifier
->error(
698 "Omitting fields before field `%s' would invalidate previously specified erroneous data",
699 act_field_err
.qualifier
->get_stringRepr().c_str());
702 // save valid omit before data
703 omit_before_qual
= act_field_err
.qualifier
;
704 err_descr
->omit_before
= fld_idx
;
705 err_descr
->omit_before_name
= omit_before_qual
->get_stringRepr();
708 if ((act_indicator
==ErroneousAttributeSpec::I_AFTER
) && is_omit
) {
709 if (omit_after_qual
&& (err_descr
->omit_after
!=-1)) {
710 act_field_err
.qualifier
->error(
711 "Duplicate rule for omitting all fields after the specified field. Used on field `%s' but previously already used on field `%s'",
712 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_after_qual
->get_stringRepr().c_str());
715 bool is_invalid
= false;
716 for (size_t j
=0; j
<err_descr
->values_m
.size(); j
++) {
717 if (err_descr
->values_m
.get_nth_key(j
)>fld_idx
) {
723 for (size_t j
=0; j
<embedded_fld_array_m
.size(); j
++) {
724 if (embedded_fld_array_m
.get_nth_key(j
)>fld_idx
) {
731 act_field_err
.qualifier
->error(
732 "Omitting fields after field `%s' would invalidate previously specified erroneous data",
733 act_field_err
.qualifier
->get_stringRepr().c_str());
736 // save valid omit after data
737 omit_after_qual
= act_field_err
.qualifier
;
738 err_descr
->omit_after
= fld_idx
;
739 err_descr
->omit_after_name
= omit_after_qual
->get_stringRepr();
742 // if not before/after omit then save this into values_m
743 bool has_key
= err_descr
->values_m
.has_key(fld_idx
);
744 ErroneousValues
* evs
= has_key
? err_descr
->values_m
[fld_idx
] : new ErroneousValues(act_field_err
.qualifier
->get_stringRepr());
745 switch (act_indicator
) {
746 case ErroneousAttributeSpec::I_BEFORE
: evs
->before
= act_field_err
.err_attr
; break;
747 case ErroneousAttributeSpec::I_VALUE
: evs
->value
= act_field_err
.err_attr
; break;
748 case ErroneousAttributeSpec::I_AFTER
: evs
->after
= act_field_err
.err_attr
; break;
749 default: FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
752 err_descr
->values_m
.add(fld_idx
, evs
);
754 } else { // embedded err.value
755 if ((err_descr
->values_m
.has_key(fld_idx
)) && (err_descr
->values_m
[fld_idx
]->value
)) {
756 act_field_err
.qualifier
->error(
757 "Field `%s' is embedded into a field which was previously overwritten or omitted",
758 act_field_err
.qualifier
->get_stringRepr().c_str());
761 // add the embedded field to the map
762 bool has_idx
= embedded_fld_array_m
.has_key(fld_idx
);
763 dynamic_array
<field_err_t
>* emb_fld_array
= has_idx
? embedded_fld_array_m
[fld_idx
] : new dynamic_array
<field_err_t
>();
764 field_err_t emb_field_err
= act_field_err
;
765 emb_field_err
.subrefs_array
.remove(0); // remove the first reference
766 emb_field_err
.type_array
.remove(0);
767 emb_fld_array
->add(emb_field_err
);
769 embedded_fld_array_m
.add(fld_idx
, emb_fld_array
);
773 // recursive calls to create embedded descriptors
774 for (size_t i
=0; i
<embedded_fld_array_m
.size(); i
++) {
775 dynamic_array
<field_err_t
>* emb_fld_array
= embedded_fld_array_m
.get_nth_elem(i
);
776 err_descr
->descr_m
.add(embedded_fld_array_m
.get_nth_key(i
), build_descr_tree(*emb_fld_array
));
777 delete emb_fld_array
;
779 embedded_fld_array_m
.clear();
783 // ==== AttributeSpec ====
785 AttributeSpec
* AttributeSpec::clone() const
787 return new AttributeSpec(*this);
790 void AttributeSpec::set_fullname(const string
& p_fullname
)
792 Node::set_fullname(p_fullname
);
795 void AttributeSpec::dump(unsigned level
) const
797 DEBUG(level
,"spec: %s", spec
.c_str());
800 // ==== SingleWithAttrib ====
802 SingleWithAttrib::SingleWithAttrib(const SingleWithAttrib
& p
)
803 : Node(p
), Location(p
), attribKeyword(p
.attribKeyword
),
804 hasOverride(p
.hasOverride
)
806 attribQualifiers
= p
.attribQualifiers
? p
.attribQualifiers
->clone() : 0;
807 attribSpec
= p
.attribSpec
->clone();
810 SingleWithAttrib::SingleWithAttrib(
811 attribtype_t p_attribKeyword
, bool p_hasOverride
,
812 Qualifiers
*p_attribQualifiers
, AttributeSpec
* p_attribSpec
)
813 : Node(), Location(), attribKeyword(p_attribKeyword
),
814 hasOverride(p_hasOverride
), attribQualifiers(p_attribQualifiers
),
815 attribSpec(p_attribSpec
)
818 FATAL_ERROR("SingleWithAttrib::SingleWithAttrib()");
821 SingleWithAttrib::~SingleWithAttrib()
823 delete attribQualifiers
;
827 SingleWithAttrib
* SingleWithAttrib::clone() const
829 return new SingleWithAttrib(*this);
832 void SingleWithAttrib::set_fullname(const string
& p_fullname
)
834 Node::set_fullname(p_fullname
);
836 attribQualifiers
->set_fullname(p_fullname
+ ".<attribute qualifiers>");
837 attribSpec
->set_fullname(p_fullname
+ ".<attribute specification>");
840 void SingleWithAttrib::dump(unsigned level
) const
842 DEBUG(level
,"attribute");
843 switch(attribKeyword
)
846 DEBUG(level
+ 1,"keyword: encode");
849 DEBUG(level
+ 1,"keyword: variant");
852 DEBUG(level
+ 1,"keyword: display");
855 DEBUG(level
+ 1,"keyword: extension");
858 DEBUG(level
+ 1,"keyword: optional");
861 DEBUG(level
+1, "keyword: erroneous");
864 DEBUG(level
+1, "invalid keyword");
867 FATAL_ERROR("SingleWithAttrib::dump()");
870 DEBUG(level
+ 1, hasOverride
? "has override" : "hasn't got override");
873 attribSpec
->dump(level
+ 1);
875 attribQualifiers
->dump(level
+ 1);
878 // ==== MultiWithAttrib ====
880 MultiWithAttrib::MultiWithAttrib(const MultiWithAttrib
& p
)
881 : Node(p
), Location(p
)
883 for(size_t i
= 0; i
< p
.get_nof_elements(); i
++)
885 elements
.add(p
.get_element(i
)->clone());
889 MultiWithAttrib
* MultiWithAttrib::clone() const
891 return new MultiWithAttrib(*this);
894 void MultiWithAttrib::set_fullname(const string
& p_fullname
)
896 Node::set_fullname(p_fullname
);
897 for(size_t i
= 0; i
< elements
.size(); i
++)
899 elements
[i
]->set_fullname(p_fullname
+ ".<singlewithattribute "
900 + Int2string(i
) + ">");
904 MultiWithAttrib::~MultiWithAttrib()
906 for(size_t i
= 0; i
< elements
.size(); i
++)
913 const SingleWithAttrib
* MultiWithAttrib::get_element(size_t p_index
) const
915 return elements
[p_index
];
918 SingleWithAttrib
* MultiWithAttrib::get_element_for_modification(
921 return elements
[p_index
];
924 void MultiWithAttrib::delete_element(size_t p_index
)
926 delete elements
[p_index
];
927 elements
.replace(p_index
,1,NULL
);
930 void MultiWithAttrib::dump(unsigned level
) const
932 DEBUG(level
,"with attrib parameters (%lu pcs)",
933 (unsigned long) elements
.size());
934 for(size_t i
= 0; i
< elements
.size(); i
++)
936 elements
[i
]->dump(level
+ 1);
940 // ==== WithAttribPath ====
942 WithAttribPath::WithAttribPath(const WithAttribPath
& p
)
943 : Node(p
), had_global_variants(false), attributes_checked(false),
944 cached(false), s_o_encode(false), parent(p
.parent
)
946 m_w_attrib
= p
.m_w_attrib
? p
.m_w_attrib
->clone() : 0;
949 WithAttribPath::~WithAttribPath()
955 WithAttribPath
* WithAttribPath::clone() const
957 return new WithAttribPath(*this);
960 void WithAttribPath::set_fullname(const string
& p_fullname
)
962 Node::set_fullname(p_fullname
);
963 if (m_w_attrib
) m_w_attrib
->set_fullname(p_fullname
964 + ".<multiwithattribute>");
967 void WithAttribPath::chk_no_qualif()
969 if(attributes_checked
)
975 const SingleWithAttrib
*swa
;
976 for(int i
= m_w_attrib
->get_nof_elements() - 1; i
>= 0; i
--)
978 swa
= m_w_attrib
->get_element(i
);
979 if( swa
->get_attribQualifiers()
980 && swa
->get_attribQualifiers()->get_nof_qualifiers() != 0)
982 swa
->error("field qualifiers are only allowed"
983 " for record, set and union types");
984 m_w_attrib
->delete_element(i
);
988 attributes_checked
= true;
991 void WithAttribPath::dump(unsigned int level
) const
993 DEBUG(level
, "WithAttribPath");
994 if (!m_w_attrib
) return;
995 DEBUG(level
+1, "%lu elements",
996 (unsigned long)m_w_attrib
->get_nof_elements());
997 for (size_t i
=0; i
< m_w_attrib
->get_nof_elements(); ++i
) {
998 const SingleWithAttrib
* a
= m_w_attrib
->get_element(i
);
1005 * Checks whether there is inconsistency among global attributes or not.
1006 * Only the last encode can have effect so we can throw out the others.
1007 * This is because encode is not an attribute, but a "context".
1008 * If there is an overriding variant/display/extension then the
1009 * following attributes from the same type should be omitted.
1011 void WithAttribPath::chk_global_attrib(bool erroneous_allowed
)
1016 if (!erroneous_allowed
) {
1017 for(size_t i
= m_w_attrib
->get_nof_elements(); i
> 0; i
--) {
1018 const SingleWithAttrib
* const temp_attrib
=
1019 m_w_attrib
->get_element(i
-1);
1020 if (temp_attrib
->get_attribKeyword()==SingleWithAttrib::AT_ERRONEOUS
) {
1021 temp_attrib
->error("The `erroneous' attribute can be used only on "
1022 "template and constant definitions");
1027 bool has_encode
= false;
1028 bool has_override_variant
= false;
1029 bool has_override_display
= false;
1030 bool has_override_extension
= false;
1031 bool has_override_optional
= false;
1032 for(size_t i
= m_w_attrib
->get_nof_elements(); i
> 0; i
--)
1034 const SingleWithAttrib
* const temp_attrib
=
1035 m_w_attrib
->get_element(i
-1);
1036 switch(temp_attrib
->get_attribKeyword())
1038 case SingleWithAttrib::AT_ENCODE
:
1042 temp_attrib
->warning("Only the last encode "
1043 "of the with statement will have effect");
1044 m_w_attrib
->delete_element(i
-1);
1050 case SingleWithAttrib::AT_ERRONEOUS
:
1052 if (temp_attrib
->has_override()) {
1053 temp_attrib
->error("Override cannot be used with erroneous");
1062 for(size_t i
= 0; i
< m_w_attrib
->get_nof_elements();)
1064 const SingleWithAttrib
* const temp_attrib
= m_w_attrib
->get_element(i
);
1065 switch(temp_attrib
->get_attribKeyword())
1067 case SingleWithAttrib::AT_VARIANT
:
1069 if(has_override_variant
)
1071 temp_attrib
->warning("Only the first override"
1072 " variant of the with statement will have effect");
1073 m_w_attrib
->delete_element(i
);
1075 if(temp_attrib
->has_override())
1076 has_override_variant
= true;
1081 case SingleWithAttrib::AT_DISPLAY
:
1083 if(has_override_display
)
1085 temp_attrib
->warning("Only the first override"
1086 " display of the with statement will have effect");
1087 m_w_attrib
->delete_element(i
);
1089 if(temp_attrib
->has_override())
1090 has_override_display
= true;
1095 case SingleWithAttrib::AT_EXTENSION
:
1097 if(has_override_extension
)
1099 temp_attrib
->warning("Only the first override"
1100 " extension of the with statement will have effect");
1101 m_w_attrib
->delete_element(i
);
1103 if(temp_attrib
->has_override())
1104 has_override_extension
= true;
1109 case SingleWithAttrib::AT_OPTIONAL
:
1111 if ("implicit omit" != temp_attrib
->get_attribSpec().get_spec() &&
1112 "explicit omit" != temp_attrib
->get_attribSpec().get_spec()) {
1113 temp_attrib
->error("Value of optional attribute can only be "
1114 "either 'explicit omit' or 'implicit omit' not '%s'",
1115 temp_attrib
->get_attribSpec().get_spec().c_str());
1117 if(has_override_optional
)
1119 temp_attrib
->warning("Only the first override"
1120 " optional of the with statement will have effect");
1121 m_w_attrib
->delete_element(i
);
1123 if(temp_attrib
->has_override())
1124 has_override_optional
= true;
1136 void WithAttribPath::set_with_attr(MultiWithAttrib
* p_m_w_attr
)
1138 if(m_w_attrib
) FATAL_ERROR("WithAttribPath::set_with_attr()");
1139 m_w_attrib
= p_m_w_attr
;
1140 attributes_checked
= false;
1144 * Finds the real attributes checking the inherited ones with its own.
1145 * Only qualifierless attributes are handled.
1146 * The stepped_over_encode is needed because it can happen that we
1147 * override an encode and later (inner) find variants without encode.
1148 * As those were the overridden encode's variants we can not add them to
1151 void WithAttribPath::qualifierless_attrib_finder(
1152 vector
<SingleWithAttrib
>& p_result
,
1153 bool& stepped_over_encode
)
1157 for(size_t i
= 0; i
< cache
.size(); i
++)
1159 p_result
.add(cache
[i
]);
1161 stepped_over_encode
= s_o_encode
;
1166 parent
->qualifierless_attrib_finder(p_result
,stepped_over_encode
);
1168 stepped_over_encode
= false;
1172 // These two refer only to the attributes of this type
1173 int self_encode_index
= -1; // the index of the "encode" attribute
1174 bool self_has_variant
= false; // flag for the presence of a "variant"
1175 // The following refer to all the attributes, including those collected
1176 // from the parent and all the ancestors.
1177 bool par_has_override_encode
= false;
1178 bool par_has_encode
= false;
1179 // True if there is an encode attribute in the local attribute list,
1180 // it differs from the parents encode
1181 // and the parent does not overwrite it.
1182 bool new_local_encode_context
= false;
1183 bool par_has_override_variant
= false;
1184 bool par_has_override_display
= false;
1185 bool par_has_override_extension
= false;
1186 bool par_has_override_optional
= false;
1188 //checking the owned attributes
1189 const SingleWithAttrib
* act_single
;
1190 const Qualifiers
* act_qualifiers
;
1191 const size_t m_w_attrib_nof_elements
= m_w_attrib
->get_nof_elements();
1192 for(size_t i
= 0; i
< m_w_attrib_nof_elements
;i
++)
1194 act_single
= m_w_attrib
->get_element(i
);
1195 act_qualifiers
= act_single
->get_attribQualifiers();
1198 if(!act_qualifiers
|| act_qualifiers
->get_nof_qualifiers() == 0)
1200 switch(act_single
->get_attribKeyword())
1202 case SingleWithAttrib::AT_ENCODE
:
1203 self_encode_index
= i
;
1206 case SingleWithAttrib::AT_VARIANT
: {
1207 // Ignore JSON variants, these should not produce warnings
1208 const string
& spec
= act_single
->get_attribSpec().get_spec();
1210 while (i2
< spec
.size()) {
1211 if (spec
[i2
] != ' ' && spec
[i2
] != '\t') {
1216 if (i2
== spec
.size() || spec
.find("JSON", i2
) != i2
) {
1217 self_has_variant
= true;
1221 case SingleWithAttrib::AT_DISPLAY
:
1222 case SingleWithAttrib::AT_EXTENSION
:
1223 case SingleWithAttrib::AT_OPTIONAL
:
1224 case SingleWithAttrib::AT_ERRONEOUS
:
1225 case SingleWithAttrib::AT_INVALID
:
1229 FATAL_ERROR("WithAttribPath::attrib_finder()");
1234 // Here p_result contains attributes collected from outer scopes only.
1235 size_t p_result_size
= p_result
.size();
1236 // gather information on the attributes collected from the parents
1237 for(size_t i
= 0; i
< p_result_size
; i
++)
1239 act_single
= p_result
[i
];
1241 switch(act_single
->get_attribKeyword())
1243 case SingleWithAttrib::AT_ENCODE
:
1244 par_has_encode
= true;
1245 par_has_override_encode
|= act_single
->has_override();
1246 if(self_encode_index
!= -1)
1248 // We also have an encode. See if they differ.
1249 new_local_encode_context
= (act_single
->get_attribSpec().get_spec()
1250 != m_w_attrib
->get_element(self_encode_index
)->
1251 get_attribSpec().get_spec());
1255 case SingleWithAttrib::AT_VARIANT
:
1256 par_has_override_variant
|= act_single
->has_override();
1258 case SingleWithAttrib::AT_DISPLAY
:
1259 par_has_override_display
|= act_single
->has_override();
1261 case SingleWithAttrib::AT_EXTENSION
:
1262 par_has_override_extension
|= act_single
->has_override();
1264 case SingleWithAttrib::AT_OPTIONAL
:
1265 par_has_override_optional
|= act_single
->has_override();
1267 case SingleWithAttrib::AT_ERRONEOUS
:
1268 case SingleWithAttrib::AT_INVALID
:
1272 FATAL_ERROR("WithAttribPath::attrib_finder()");
1276 if(!par_has_encode
&& self_encode_index
== -1 && self_has_variant
)
1278 // There is no encode, but there is at least one variant.
1279 // Find them and issue warnings.
1280 for(size_t i
= 0; i
< m_w_attrib_nof_elements
; i
++)
1282 act_single
= m_w_attrib
->get_element(i
);
1283 if(act_single
->get_attribKeyword() == SingleWithAttrib::AT_VARIANT
)
1284 act_single
->warning("This variant does not belong to an encode");
1288 // If we have an encode (self_encode_index != -1) and it differs from
1289 // the outer encode (new_local_encode_context) and the outer wasn't
1290 // an "override encode" (!par_has_override_encode),
1291 // remove the outer encode and all the variants that belong to it.
1292 for(size_t i
= p_result
.size(); i
> 0; i
--)
1294 switch(p_result
[i
-1]->get_attribKeyword())
1296 case SingleWithAttrib::AT_ENCODE
:
1297 case SingleWithAttrib::AT_VARIANT
:
1298 if (self_encode_index
!= -1 && new_local_encode_context
1299 && !par_has_override_encode
)
1300 p_result
.replace(i
-1,1,NULL
);
1302 case SingleWithAttrib::AT_DISPLAY
:
1303 case SingleWithAttrib::AT_EXTENSION
:
1304 case SingleWithAttrib::AT_OPTIONAL
:
1305 case SingleWithAttrib::AT_ERRONEOUS
:
1306 case SingleWithAttrib::AT_INVALID
:
1311 //adding the right ones from the local attributes
1312 for(size_t i
= 0; i
< m_w_attrib_nof_elements
; i
++)
1314 act_single
= m_w_attrib
->get_element(i
);
1315 act_qualifiers
= act_single
->get_attribQualifiers();
1316 if(!act_qualifiers
|| act_qualifiers
->get_nof_qualifiers() == 0)
1318 switch(act_single
->get_attribKeyword())
1320 case SingleWithAttrib::AT_ENCODE
:
1321 if((par_has_encode
&& !par_has_override_encode
1322 && new_local_encode_context
)
1323 || (!par_has_encode
))
1325 p_result
.add_front(m_w_attrib
->get_element_for_modification(i
));
1326 stepped_over_encode
= false;
1327 }else if(new_local_encode_context
){
1328 //par_has_encode && par_has_override_encode
1329 stepped_over_encode
= true;
1331 stepped_over_encode
= false;
1334 case SingleWithAttrib::AT_VARIANT
:
1335 if((!par_has_encode
&& !par_has_override_variant
)
1336 || (par_has_encode
&& self_encode_index
== -1
1337 && !stepped_over_encode
&& !par_has_override_variant
)
1338 || (par_has_encode
&& self_encode_index
!= -1
1339 && !par_has_override_encode
&& !par_has_override_variant
)
1340 || (!par_has_encode
&& self_encode_index
!= -1))
1341 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1343 case SingleWithAttrib::AT_DISPLAY
:
1344 if(!par_has_override_display
)
1345 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1347 case SingleWithAttrib::AT_EXTENSION
:
1348 if(!par_has_override_extension
)
1349 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1351 case SingleWithAttrib::AT_OPTIONAL
:
1352 if (!par_has_override_optional
)
1353 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1355 case SingleWithAttrib::AT_ERRONEOUS
:
1356 case SingleWithAttrib::AT_INVALID
:
1366 * Be very cautious this function gives back only the qualifierless attributes.
1367 * Because of types giving back all the attribs, so that they are already
1368 * in their final encode context would mean that attributes coming from
1369 * the parent path would need to be cloned and qualified as many times
1370 * as many components the type has.
1372 const vector
<SingleWithAttrib
>& WithAttribPath::get_real_attrib()
1375 qualifierless_attrib_finder(cache
,s_o_encode
);
1381 bool WithAttribPath::has_attribs()
1383 if (had_global_variants
) return true;
1384 else if (get_real_attrib().size() > 0) return true;
1385 else if (m_w_attrib
) {
1386 for (size_t i
= 0; i
< m_w_attrib
->get_nof_elements(); i
++) {
1387 const Qualifiers
*qualifiers
=
1388 m_w_attrib
->get_element(i
)->get_attribQualifiers();
1389 if (qualifiers
&& qualifiers
->get_nof_qualifiers() > 0) return true;