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
15 * Szabo, Janos Zoltan – initial implementation
16 * Zalanyi, Balazs Andor
18 ******************************************************************************/
19 #include "Attributes.hh"
21 #include "../CompilerError.hh"
23 #include "TtcnTemplate.hh"
27 // ==== Qualifier ====
29 Qualifier
* Qualifier::clone() const
31 return new Qualifier(*this);
34 const Identifier
* Qualifier::get_identifier(size_t p_index
) const
36 FieldOrArrayRef
* ref
= get_ref(p_index
);
37 return (ref
->get_type()==FieldOrArrayRef::FIELD_REF
) ?
42 Qualifier
* Qualifier::get_qualifier_without_first_id() const
44 Qualifier
* temp
= this->clone();
49 string
Qualifier::get_stringRepr() const
52 append_stringRepr(str_repr
);
53 if (str_repr
[0]=='.') str_repr
.replace(0, 1, ""); // remove leading dot if present
57 void Qualifier::dump(unsigned level
) const
59 const string
& rep
= get_stringRepr();
60 DEBUG(level
, "(%s)", rep
.c_str());
63 // ==== Qualifiers ====
65 Qualifiers::~Qualifiers()
67 for(size_t i
= 0; i
< qualifiers
.size(); i
++)
74 Qualifiers::Qualifiers(const Qualifiers
& p
)
77 for(size_t i
= 0; i
< p
.get_nof_qualifiers(); i
++)
79 qualifiers
.add(p
.get_qualifier(i
)->clone());
83 void Qualifiers::add_qualifier(Qualifier
* p_qualifier
)
85 if(p_qualifier
->get_nof_identifiers() > 0)
86 qualifiers
.add(p_qualifier
);
91 void Qualifiers::delete_qualifier(size_t p_index
)
93 delete qualifiers
[p_index
];
94 qualifiers
.replace(p_index
,1,NULL
);
97 const Qualifier
* Qualifiers::get_qualifier(size_t p_index
) const
99 return qualifiers
[p_index
];
102 Qualifiers
* Qualifiers::clone() const
104 return new Qualifiers(*this);
107 void Qualifiers::set_fullname(const string
& p_fullname
)
109 Node::set_fullname(p_fullname
);
110 for(size_t i
= 0; i
< qualifiers
.size(); i
++)
112 qualifiers
[i
]->set_fullname(p_fullname
+ ".<qualifier "
113 + Int2string(i
) + ">");
117 bool Qualifiers::has_qualifier(Qualifier
* p_qualifier
) const
122 for(size_t i
= 0; i
< qualifiers
.size() && !result
; i
++)
124 if(qualifiers
[i
]->get_nof_identifiers()
125 ==p_qualifier
->get_nof_identifiers())
129 while(index
< p_qualifier
->get_nof_identifiers() && same
)
131 same
= (*qualifiers
[i
]->get_identifier(index
)
132 == *p_qualifier
->get_identifier(index
));
142 void Qualifiers::dump(unsigned level
) const
144 DEBUG(level
, "has %lu qualifiers",
145 (unsigned long)get_nof_qualifiers());
146 for(size_t i
= 0; i
< qualifiers
.size(); i
++)
147 qualifiers
[i
]->dump(level
);
150 // ==== ErroneousDescriptor ====
152 ErroneousDescriptor::~ErroneousDescriptor()
154 for (size_t i
=0; i
<descr_m
.size(); i
++) delete descr_m
.get_nth_elem(i
);
156 for (size_t i
=0; i
<values_m
.size(); i
++) delete values_m
.get_nth_elem(i
);
160 // ==== ErroneousAttributeSpec ====
162 ErroneousAttributeSpec::ErroneousAttributeSpec(bool p_is_raw
, indicator_t p_indicator
, TemplateInstance
* p_tmpl_inst
, bool p_has_all_keyword
):
163 is_raw(p_is_raw
), has_all_keyword(p_has_all_keyword
), indicator(p_indicator
),
164 tmpl_inst(p_tmpl_inst
), type(0), value(0)
166 if (!tmpl_inst
) FATAL_ERROR("ErroneousAttributeSpec::ErroneousAttributeSpec()");
169 ErroneousAttributeSpec::ErroneousAttributeSpec(const ErroneousAttributeSpec
& p
)
170 : Node(p
), Location(p
), is_raw(p
.is_raw
), has_all_keyword(p
.has_all_keyword
),
171 indicator(p
.indicator
), type(0), value(0)
173 tmpl_inst
= p
.tmpl_inst
? tmpl_inst
->clone() : 0;
176 ErroneousAttributeSpec::~ErroneousAttributeSpec()
182 ErroneousAttributeSpec
* ErroneousAttributeSpec::clone() const
184 return new ErroneousAttributeSpec(*this);
187 void ErroneousAttributeSpec::set_fullname(const string
& p_fullname
)
189 Node::set_fullname(p_fullname
);
190 tmpl_inst
->set_fullname(p_fullname
+".<template_instance>");
193 void ErroneousAttributeSpec::set_my_scope(Scope
*p_scope
)
195 tmpl_inst
->set_my_scope(p_scope
);
198 void ErroneousAttributeSpec::dump(unsigned level
) const
200 DEBUG(level
, "raw: %s", is_raw
? "yes" : "no");
201 DEBUG(level
, "indicator:");
204 DEBUG(level
+1, "before");
207 DEBUG(level
+1, "value");
210 DEBUG(level
+1, "after");
213 DEBUG(level
+1, "<invalid>");
216 FATAL_ERROR("ErroneousAttributeSpec::dump()");
218 DEBUG(level
, "template instance:");
219 tmpl_inst
->dump(level
+1);
222 bool ErroneousAttributeSpec::get_is_omit() const
224 return (tmpl_inst
->get_Template()->get_templatetype()==Template::OMIT_VALUE
);
227 void ErroneousAttributeSpec::chk()
229 if (get_is_omit()) { // special case, no type needed
230 if ((indicator
==I_BEFORE
)||(indicator
==I_AFTER
)) {
231 if (!has_all_keyword
) {
233 "Keyword `all' is expected after `omit' when omitting all fields %s the specified field",
234 get_indicator_str(indicator
));
237 if (has_all_keyword
) {
239 "Unexpected `all' keyword after `omit' when omitting one field");
245 if (has_all_keyword
) {
247 "Unexpected `all' keyword after the in-line template");
250 // determine the type of the tmpl_inst
251 type
= tmpl_inst
->get_expr_governor(Type::EXPECTED_TEMPLATE
);
253 tmpl_inst
->get_Template()->set_lowerid_to_ref();
254 type
= tmpl_inst
->get_expr_governor(Type::EXPECTED_TEMPLATE
);
257 tmpl_inst
->error("Cannot determine the type of the in-line template");
261 Type
* type_last
= type
->get_type_refd_last();
262 if (!type_last
|| type_last
->get_typetype()==Type::T_ERROR
) {
267 switch (type_last
->get_typetype_ttcn3()) {
274 tmpl_inst
->error("An in-line template of type `%s' cannot be used as a `raw' erroneous value",
275 type_last
->get_typename().c_str());
279 if (tmpl_inst
->get_DerivedRef()) {
280 tmpl_inst
->error("Reference to a constant value was expected instead of an in-line modified template");
285 Template
* templ
= tmpl_inst
->get_Template();
287 if (templ
->is_Value()) {
288 value
= templ
->get_Value();
289 value
->set_my_governor(type
);
290 type
->chk_this_value_ref(value
);
291 type
->chk_this_value(value
, 0, Type::EXPECTED_CONSTANT
,
292 INCOMPLETE_NOT_ALLOWED
, OMIT_NOT_ALLOWED
, SUB_CHK
);
293 //{ FIXME: make this work
294 // ReferenceChain refch(type, "While checking embedded recursions");
295 // value->chk_recursions(refch);
297 value
->set_code_section(GovernedSimple::CS_PRE_INIT
);
299 tmpl_inst
->error("A specific value without matching symbols was expected");
305 const char* ErroneousAttributeSpec::get_indicator_str(indicator_t i
)
308 case I_BEFORE
: return "before";
309 case I_VALUE
: return "value";
310 case I_AFTER
: return "after";
311 default: FATAL_ERROR("ErroneousAttributeSpec::get_indicator_str()");
316 char* ErroneousAttributeSpec::generate_code_str(char *str
, string genname
)
318 if (get_is_omit()) return str
;
319 if (!type
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_str()");
320 if (!value
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_str()");
321 if (first_genname
.empty()) { // this is the first use
322 str
= mputprintf(str
, "static %s %s;\n",
323 type
->get_genname_value(value
->get_my_scope()).c_str(), genname
.c_str());
324 first_genname
= genname
;
326 str
= mputprintf(str
, "static %s& %s = %s;\n",
327 type
->get_genname_value(value
->get_my_scope()).c_str(),
328 genname
.c_str(), first_genname
.c_str());
333 char* ErroneousAttributeSpec::generate_code_init_str(char *str
, string genname
)
335 if (get_is_omit()) return str
;
336 if (!type
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_init_str()");
337 if (!value
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_init_str()");
338 str
= value
->generate_code_init(str
, genname
.c_str());
342 string
ErroneousAttributeSpec::get_typedescriptor_str()
344 if (get_is_omit() || is_raw
) return string("NULL");
345 if (!type
) FATAL_ERROR("ErroneousAttributeSpec::get_typedescriptor_str()");
346 if (!value
) FATAL_ERROR("ErroneousAttributeSpec::generate_code_ti_str()");
347 return ( "&" + type
->get_genname_typedescriptor(value
->get_my_scope()) + "_descr_" );
350 void ErroneousAttributeSpec::chk_recursions(ReferenceChain
& refch
)
354 value
->chk_recursions(refch
);
359 // ==== ErroneousValues ====
361 char* ErroneousValues::generate_code_embedded_str(char *str
, string genname
)
363 if (before
) str
= generate_code_embedded_str(str
, genname
+"_before", before
);
364 if (value
) str
= generate_code_embedded_str(str
, genname
+"_value", value
);
365 if (after
) str
= generate_code_embedded_str(str
, genname
+"_after", after
);
369 char* ErroneousValues::generate_code_embedded_str(char *str
, string genname
, ErroneousAttributeSpec
* attr_spec
)
371 str
= attr_spec
->generate_code_str(str
, genname
+"_errval");
372 str
= mputprintf(str
, "static Erroneous_value_t %s = { %s, %s, %s };\n", genname
.c_str(),
373 attr_spec
->get_is_raw() ? "true" : "false",
374 attr_spec
->get_is_omit() ? "NULL" : ("&"+genname
+"_errval").c_str(),
375 attr_spec
->get_typedescriptor_str().c_str());
379 char* ErroneousValues::generate_code_init_str(char *str
, string genname
)
381 if (before
) str
= before
->generate_code_init_str(str
, genname
+"_before_errval");
382 if (value
) str
= value
->generate_code_init_str(str
, genname
+"_value_errval");
383 if (after
) str
= after
->generate_code_init_str(str
, genname
+"_after_errval");
387 char* ErroneousValues::generate_code_struct_str(char *str
, string genname
, int field_index
)
389 str
= mputprintf(str
, "{ %d, %s, %s, %s, %s }", field_index
,
390 ("\""+field_name
+"\"").c_str(),
391 before
? ("&"+genname
+"_before").c_str() : "NULL",
392 value
? ("&"+genname
+"_value").c_str() : "NULL",
393 after
? ("&"+genname
+"_after").c_str() : "NULL");
397 void ErroneousValues::chk_recursions(ReferenceChain
& refch
)
399 if (before
) before
->chk_recursions(refch
);
400 if (value
) value
->chk_recursions(refch
);
401 if (after
) after
->chk_recursions(refch
);
404 // ==== ErroneousDescriptor ====
406 char* ErroneousDescriptor::generate_code_embedded_str(char *str
, string genname
)
409 for (size_t i
=0; i
<values_m
.size(); i
++) {
410 str
= values_m
.get_nth_elem(i
)->generate_code_embedded_str(str
, genname
+"_v"+Int2string((int)values_m
.get_nth_key(i
)));
412 // embedded descriptors
413 for (size_t i
=0; i
<descr_m
.size(); i
++) {
414 str
= descr_m
.get_nth_elem(i
)->generate_code_embedded_str(str
, genname
+"_d"+Int2string((int)descr_m
.get_nth_key(i
)));
417 if (values_m
.size()>0) {
418 str
= mputprintf(str
, "static Erroneous_values_t %s_valsvec[%d] = { ", genname
.c_str(), (int)values_m
.size());
419 for (size_t i
=0; i
<values_m
.size(); i
++) {
420 if (i
>0) str
= mputstr(str
, ", ");
421 int key_i
= (int)values_m
.get_nth_key(i
);
422 str
= values_m
.get_nth_elem(i
)->generate_code_struct_str(str
, genname
+"_v"+Int2string(key_i
), key_i
);
424 str
= mputstr(str
, " };\n");
426 // embedded descriptor vector
427 if (descr_m
.size()>0) {
428 str
= mputprintf(str
, "static Erroneous_descriptor_t %s_embvec[%d] = { ", genname
.c_str(), (int)descr_m
.size());
429 for (size_t i
=0; i
<descr_m
.size(); i
++) {
430 if (i
>0) str
= mputstr(str
, ", ");
431 int key_i
= (int)descr_m
.get_nth_key(i
);
432 str
= descr_m
.get_nth_elem(i
)->generate_code_struct_str(str
, genname
+"_d"+Int2string(key_i
), key_i
);
434 str
= mputstr(str
, " };\n");
439 char* ErroneousDescriptor::generate_code_init_str(char *str
, string genname
)
441 for (size_t i
=0; i
<values_m
.size(); i
++) {
442 str
= values_m
.get_nth_elem(i
)->generate_code_init_str(str
, genname
+"_v"+Int2string((int)values_m
.get_nth_key(i
)));
444 for (size_t i
=0; i
<descr_m
.size(); i
++) {
445 str
= descr_m
.get_nth_elem(i
)->generate_code_init_str(str
, genname
+"_d"+Int2string((int)descr_m
.get_nth_key(i
)));
450 char* ErroneousDescriptor::generate_code_struct_str(char *str
, string genname
, int field_index
)
452 string genname_values_vec
= genname
+ "_valsvec";
453 string genname_embedded_vec
= genname
+ "_embvec";
454 str
= mputprintf(str
, "{ %d, %d, %s, %d, %s, %d, %s, %d, %s }",
456 omit_before
, (omit_before
==-1)?"NULL":("\""+omit_before_name
+"\"").c_str(),
457 omit_after
, (omit_after
==-1)?"NULL":("\""+omit_after_name
+"\"").c_str(),
458 (int)values_m
.size(), (values_m
.size()>0) ? genname_values_vec
.c_str(): "NULL",
459 (int)descr_m
.size(), (descr_m
.size()>0) ? genname_embedded_vec
.c_str(): "NULL");
463 void ErroneousDescriptor::chk_recursions(ReferenceChain
& refch
)
465 for (size_t i
=0; i
<values_m
.size(); i
++) {
466 values_m
.get_nth_elem(i
)->chk_recursions(refch
);
468 for (size_t i
=0; i
<descr_m
.size(); i
++) {
469 descr_m
.get_nth_elem(i
)->chk_recursions(refch
);
473 char* ErroneousDescriptor::generate_code_str(char *str
, string genname
)
475 genname
+= "_err_descr";
476 str
= generate_code_embedded_str(str
, genname
);
477 str
= mputprintf(str
, "static Erroneous_descriptor_t %s = ", genname
.c_str());
478 str
= generate_code_struct_str(str
, genname
, -1);
479 str
= mputstr(str
, ";\n");
483 // ==== ErroneousAttributes ====
485 ErroneousAttributes::ErroneousAttributes(Type
* p_type
):
486 type(p_type
), err_descr_tree(NULL
)
488 if (!type
) FATAL_ERROR("ErroneousAttributes::ErroneousAttributes()");
491 ErroneousAttributes::~ErroneousAttributes()
493 for (size_t i
=0; i
<spec_vec
.size(); i
++) {
497 if (err_descr_tree
) delete err_descr_tree
;
500 ErroneousAttributes::ErroneousAttributes(const ErroneousAttributes
& p
)
501 : Node(p
), type(p
.type
), err_descr_tree(NULL
)
505 ErroneousAttributes
* ErroneousAttributes::clone() const
507 return new ErroneousAttributes(*this);
510 void ErroneousAttributes::set_fullname(const string
& p_fullname
)
512 Node::set_fullname(p_fullname
);
513 for (size_t i
=0; i
<spec_vec
.size(); i
++) {
514 spec_vec
[i
]->set_fullname(p_fullname
+".<erroneous_attr_spec_"+Int2string(i
)+">");
518 void ErroneousAttributes::dump(unsigned level
) const
520 DEBUG(level
, "erroneous attributes:");
521 for (size_t i
=0; i
<spec_vec
.size(); i
++) {
522 spec_vec
[i
]->dump(level
+1);
526 void ErroneousAttributes::add_spec(ErroneousAttributeSpec
* err_attr_spec
)
528 if (!err_attr_spec
) FATAL_ERROR("ErroneousAttributes::add_spec()");
529 spec_vec
.add(err_attr_spec
);
532 void ErroneousAttributes::add_pair(const Qualifier
* qualifier
, ErroneousAttributeSpec
* err_attr_spec
)
534 if (!qualifier
|| !err_attr_spec
) FATAL_ERROR("ErroneousAttributes::add()");
536 f
.qualifier
= qualifier
;
537 f
.err_attr
= err_attr_spec
;
541 void ErroneousAttributes::chk()
543 // check that encodings of erroneous type and templateinstance type match
544 for (size_t i
=0; i
<spec_vec
.size(); i
++) {
545 ErroneousAttributeSpec
* act_attr
= spec_vec
[i
];
546 Type
* ti_type
= act_attr
->get_type();
547 if ((act_attr
->get_indicator()!=ErroneousAttributeSpec::I_INVALID
) && ti_type
) {
548 if (act_attr
->get_is_raw()) {
549 switch (ti_type
->get_typetype_ttcn3()) {
551 if (!type
->has_encoding(Type::CT_PER
) && !type
->has_encoding(Type::CT_RAW
)) {
552 act_attr
->error("A `raw' %s value was used for erroneous type `%s' which has no RAW or PER encodings.",
553 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
557 if (!type
->has_encoding(Type::CT_TEXT
) && !type
->has_encoding(Type::CT_XER
) &&
558 !type
->has_encoding(Type::CT_JSON
)) {
559 act_attr
->error("A `raw' %s value was used for erroneous type `%s' which has no TEXT, XER or JSON encodings.",
560 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
564 if (!type
->has_encoding(Type::CT_XER
) && !type
->has_encoding(Type::CT_JSON
)) {
565 act_attr
->error("A `raw' %s value was used for erroneous type `%s' which has no XER or JSON encoding.",
566 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
573 // the two types must have at least one common encoding
574 if (!((type
->has_encoding(Type::CT_BER
)&&ti_type
->has_encoding(Type::CT_BER
)) ||
575 (type
->has_encoding(Type::CT_PER
)&&ti_type
->has_encoding(Type::CT_PER
)) ||
576 (type
->has_encoding(Type::CT_RAW
)&&ti_type
->has_encoding(Type::CT_RAW
)) ||
577 (type
->has_encoding(Type::CT_TEXT
)&&ti_type
->has_encoding(Type::CT_TEXT
)) ||
578 (type
->has_encoding(Type::CT_XER
)&&ti_type
->has_encoding(Type::CT_XER
)) ||
579 (type
->has_encoding(Type::CT_JSON
)&&ti_type
->has_encoding(Type::CT_JSON
)))) {
580 act_attr
->error("Type `%s' and type `%s' have no common encoding",
581 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
587 // for every erroneous field calculate the corresponding index and type arrays
588 // for example: x[5].z -> [3,5,2] and [MyRec,MyRecOf,MyUnion]
589 // MyRec.x field has index 3, etc.
590 for (size_t i
=0; i
<field_array
.size(); i
++) {
591 bool b
= type
->get_subrefs_as_array(field_array
[i
].qualifier
, field_array
[i
].subrefs_array
, field_array
[i
].type_array
);
592 if (!b
) FATAL_ERROR("ErroneousAttributes::chk()");
594 // check the qualifiers and build the tree
595 err_descr_tree
= build_descr_tree(field_array
);
598 ErroneousDescriptor
* ErroneousAttributes::build_descr_tree(dynamic_array
<field_err_t
>& fld_array
)
600 ErroneousDescriptor
* err_descr
= new ErroneousDescriptor();
601 const Qualifier
* omit_before_qual
= NULL
, * omit_after_qual
= NULL
;
602 map
< size_t, dynamic_array
<field_err_t
> > embedded_fld_array_m
; // used for recursive calls
603 for (size_t i
=0; i
<fld_array
.size(); i
++) {
604 field_err_t
& act_field_err
= fld_array
[i
];
605 ErroneousAttributeSpec::indicator_t act_indicator
= act_field_err
.err_attr
->get_indicator();
606 bool is_omit
= act_field_err
.err_attr
->get_is_omit();
607 if (act_field_err
.subrefs_array
.size()<1) FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
608 size_t fld_idx
= act_field_err
.subrefs_array
[0];
609 if (omit_before_qual
&& (err_descr
->omit_before
!=-1) && (err_descr
->omit_before
>(int)fld_idx
)) {
610 act_field_err
.qualifier
->error(
611 "Field `%s' cannot be referenced because all fields before field `%s' have been omitted",
612 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_before_qual
->get_stringRepr().c_str());
615 if (omit_after_qual
&& (err_descr
->omit_after
!=-1) && (err_descr
->omit_after
<(int)fld_idx
)) {
616 act_field_err
.qualifier
->error(
617 "Field `%s' cannot be referenced because all fields after field `%s' have been omitted",
618 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_after_qual
->get_stringRepr().c_str());
621 if (act_field_err
.subrefs_array
.size()==1) { // erroneous value
622 if (act_field_err
.type_array
.size()!=1) FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
623 if ((act_field_err
.type_array
[0]->get_typetype()==Type::T_SET_A
) &&
624 is_omit
&& (act_indicator
!=ErroneousAttributeSpec::I_VALUE
)) {
625 act_field_err
.qualifier
->error(
626 "Cannot omit all fields %s `%s' which is a field of an ASN.1 SET type",
627 ErroneousAttributeSpec::get_indicator_str(act_indicator
), act_field_err
.qualifier
->get_stringRepr().c_str());
628 act_field_err
.qualifier
->note(
629 "The order of fields in ASN.1 SET types changes depending on tagging (see X.690 9.3). "
630 "Fields can be omitted individually, independently of the field order which depends on tagging");
633 switch (act_field_err
.type_array
[0]->get_typetype_ttcn3()) {
634 case Type::T_CHOICE_T
:
635 if (act_indicator
!=ErroneousAttributeSpec::I_VALUE
) {
636 act_field_err
.qualifier
->error(
637 "Indicator `%s' cannot be used with reference `%s' which points to a field of a union type",
638 ErroneousAttributeSpec::get_indicator_str(act_indicator
), act_field_err
.qualifier
->get_stringRepr().c_str());
644 if (is_omit
&& (act_indicator
==ErroneousAttributeSpec::I_AFTER
) &&
645 (fld_idx
==act_field_err
.type_array
[0]->get_nof_comps()-1)) {
646 act_field_err
.qualifier
->error(
647 "There is nothing to omit after the last field (%s) of a record/set type",
648 act_field_err
.qualifier
->get_stringRepr().c_str());
654 if (is_omit
&& (act_indicator
==ErroneousAttributeSpec::I_BEFORE
) &&
656 act_field_err
.qualifier
->error(
657 "There is nothing to omit before the first field (%s)",
658 act_field_err
.qualifier
->get_stringRepr().c_str());
665 // check for duplicate value+indicator
666 if (err_descr
->values_m
.has_key(fld_idx
)) {
667 ErroneousValues
* evs
= err_descr
->values_m
[fld_idx
];
668 if ( (evs
->before
&& (act_indicator
==ErroneousAttributeSpec::I_BEFORE
)) ||
669 (evs
->value
&& (act_indicator
==ErroneousAttributeSpec::I_VALUE
)) ||
670 (evs
->after
&& (act_indicator
==ErroneousAttributeSpec::I_AFTER
)) ) {
671 act_field_err
.qualifier
->error(
672 "Duplicate reference to field `%s' with indicator `%s'",
673 act_field_err
.qualifier
->get_stringRepr().c_str(), ErroneousAttributeSpec::get_indicator_str(act_indicator
));
677 // when overwriting a value check if embedded values were used
678 if ((act_indicator
==ErroneousAttributeSpec::I_VALUE
) && embedded_fld_array_m
.has_key(fld_idx
)) {
679 act_field_err
.qualifier
->error(
680 "Reference to field `%s' with indicator `value' would invalidate previously specified erroneous data",
681 act_field_err
.qualifier
->get_stringRepr().c_str());
684 // if before/after omit then check that no references to omitted regions and no duplication of omit before/after rule
685 if ((act_indicator
==ErroneousAttributeSpec::I_BEFORE
) && is_omit
) {
686 if (omit_before_qual
&& (err_descr
->omit_before
!=-1)) {
687 act_field_err
.qualifier
->error(
688 "Duplicate rule for omitting all fields before the specified field. Used on field `%s' but previously already used on field `%s'",
689 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_before_qual
->get_stringRepr().c_str());
692 bool is_invalid
= false;
693 for (size_t j
=0; j
<err_descr
->values_m
.size(); j
++) {
694 if (err_descr
->values_m
.get_nth_key(j
)<fld_idx
) {
700 for (size_t j
=0; j
<embedded_fld_array_m
.size(); j
++) {
701 if (embedded_fld_array_m
.get_nth_key(j
)<fld_idx
) {
708 act_field_err
.qualifier
->error(
709 "Omitting fields before field `%s' would invalidate previously specified erroneous data",
710 act_field_err
.qualifier
->get_stringRepr().c_str());
713 // save valid omit before data
714 omit_before_qual
= act_field_err
.qualifier
;
715 err_descr
->omit_before
= fld_idx
;
716 err_descr
->omit_before_name
= omit_before_qual
->get_stringRepr();
719 if ((act_indicator
==ErroneousAttributeSpec::I_AFTER
) && is_omit
) {
720 if (omit_after_qual
&& (err_descr
->omit_after
!=-1)) {
721 act_field_err
.qualifier
->error(
722 "Duplicate rule for omitting all fields after the specified field. Used on field `%s' but previously already used on field `%s'",
723 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_after_qual
->get_stringRepr().c_str());
726 bool is_invalid
= false;
727 for (size_t j
=0; j
<err_descr
->values_m
.size(); j
++) {
728 if (err_descr
->values_m
.get_nth_key(j
)>fld_idx
) {
734 for (size_t j
=0; j
<embedded_fld_array_m
.size(); j
++) {
735 if (embedded_fld_array_m
.get_nth_key(j
)>fld_idx
) {
742 act_field_err
.qualifier
->error(
743 "Omitting fields after field `%s' would invalidate previously specified erroneous data",
744 act_field_err
.qualifier
->get_stringRepr().c_str());
747 // save valid omit after data
748 omit_after_qual
= act_field_err
.qualifier
;
749 err_descr
->omit_after
= fld_idx
;
750 err_descr
->omit_after_name
= omit_after_qual
->get_stringRepr();
753 // if not before/after omit then save this into values_m
754 bool has_key
= err_descr
->values_m
.has_key(fld_idx
);
755 ErroneousValues
* evs
= has_key
? err_descr
->values_m
[fld_idx
] : new ErroneousValues(act_field_err
.qualifier
->get_stringRepr());
756 switch (act_indicator
) {
757 case ErroneousAttributeSpec::I_BEFORE
: evs
->before
= act_field_err
.err_attr
; break;
758 case ErroneousAttributeSpec::I_VALUE
: evs
->value
= act_field_err
.err_attr
; break;
759 case ErroneousAttributeSpec::I_AFTER
: evs
->after
= act_field_err
.err_attr
; break;
760 default: FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
763 err_descr
->values_m
.add(fld_idx
, evs
);
765 } else { // embedded err.value
766 if ((err_descr
->values_m
.has_key(fld_idx
)) && (err_descr
->values_m
[fld_idx
]->value
)) {
767 act_field_err
.qualifier
->error(
768 "Field `%s' is embedded into a field which was previously overwritten or omitted",
769 act_field_err
.qualifier
->get_stringRepr().c_str());
772 // add the embedded field to the map
773 bool has_idx
= embedded_fld_array_m
.has_key(fld_idx
);
774 dynamic_array
<field_err_t
>* emb_fld_array
= has_idx
? embedded_fld_array_m
[fld_idx
] : new dynamic_array
<field_err_t
>();
775 field_err_t emb_field_err
= act_field_err
;
776 emb_field_err
.subrefs_array
.remove(0); // remove the first reference
777 emb_field_err
.type_array
.remove(0);
778 emb_fld_array
->add(emb_field_err
);
780 embedded_fld_array_m
.add(fld_idx
, emb_fld_array
);
784 // recursive calls to create embedded descriptors
785 for (size_t i
=0; i
<embedded_fld_array_m
.size(); i
++) {
786 dynamic_array
<field_err_t
>* emb_fld_array
= embedded_fld_array_m
.get_nth_elem(i
);
787 err_descr
->descr_m
.add(embedded_fld_array_m
.get_nth_key(i
), build_descr_tree(*emb_fld_array
));
788 delete emb_fld_array
;
790 embedded_fld_array_m
.clear();
794 // ==== AttributeSpec ====
796 AttributeSpec
* AttributeSpec::clone() const
798 return new AttributeSpec(*this);
801 void AttributeSpec::set_fullname(const string
& p_fullname
)
803 Node::set_fullname(p_fullname
);
806 void AttributeSpec::dump(unsigned level
) const
808 DEBUG(level
,"spec: %s", spec
.c_str());
811 // ==== SingleWithAttrib ====
813 SingleWithAttrib::SingleWithAttrib(const SingleWithAttrib
& p
)
814 : Node(p
), Location(p
), attribKeyword(p
.attribKeyword
),
815 hasOverride(p
.hasOverride
)
817 attribQualifiers
= p
.attribQualifiers
? p
.attribQualifiers
->clone() : 0;
818 attribSpec
= p
.attribSpec
->clone();
821 SingleWithAttrib::SingleWithAttrib(
822 attribtype_t p_attribKeyword
, bool p_hasOverride
,
823 Qualifiers
*p_attribQualifiers
, AttributeSpec
* p_attribSpec
)
824 : Node(), Location(), attribKeyword(p_attribKeyword
),
825 hasOverride(p_hasOverride
), attribQualifiers(p_attribQualifiers
),
826 attribSpec(p_attribSpec
)
829 FATAL_ERROR("SingleWithAttrib::SingleWithAttrib()");
832 SingleWithAttrib::~SingleWithAttrib()
834 delete attribQualifiers
;
838 SingleWithAttrib
* SingleWithAttrib::clone() const
840 return new SingleWithAttrib(*this);
843 void SingleWithAttrib::set_fullname(const string
& p_fullname
)
845 Node::set_fullname(p_fullname
);
847 attribQualifiers
->set_fullname(p_fullname
+ ".<attribute qualifiers>");
848 attribSpec
->set_fullname(p_fullname
+ ".<attribute specification>");
851 void SingleWithAttrib::dump(unsigned level
) const
853 DEBUG(level
,"attribute");
854 switch(attribKeyword
)
857 DEBUG(level
+ 1,"keyword: encode");
860 DEBUG(level
+ 1,"keyword: variant");
863 DEBUG(level
+ 1,"keyword: display");
866 DEBUG(level
+ 1,"keyword: extension");
869 DEBUG(level
+ 1,"keyword: optional");
872 DEBUG(level
+1, "keyword: erroneous");
875 DEBUG(level
+1, "invalid keyword");
878 FATAL_ERROR("SingleWithAttrib::dump()");
881 DEBUG(level
+ 1, hasOverride
? "has override" : "hasn't got override");
884 attribSpec
->dump(level
+ 1);
886 attribQualifiers
->dump(level
+ 1);
889 // ==== MultiWithAttrib ====
891 MultiWithAttrib::MultiWithAttrib(const MultiWithAttrib
& p
)
892 : Node(p
), Location(p
)
894 for(size_t i
= 0; i
< p
.get_nof_elements(); i
++)
896 elements
.add(p
.get_element(i
)->clone());
900 MultiWithAttrib
* MultiWithAttrib::clone() const
902 return new MultiWithAttrib(*this);
905 void MultiWithAttrib::set_fullname(const string
& p_fullname
)
907 Node::set_fullname(p_fullname
);
908 for(size_t i
= 0; i
< elements
.size(); i
++)
910 elements
[i
]->set_fullname(p_fullname
+ ".<singlewithattribute "
911 + Int2string(i
) + ">");
915 MultiWithAttrib::~MultiWithAttrib()
917 for(size_t i
= 0; i
< elements
.size(); i
++)
924 const SingleWithAttrib
* MultiWithAttrib::get_element(size_t p_index
) const
926 return elements
[p_index
];
929 SingleWithAttrib
* MultiWithAttrib::get_element_for_modification(
932 return elements
[p_index
];
935 void MultiWithAttrib::delete_element(size_t p_index
)
937 delete elements
[p_index
];
938 elements
.replace(p_index
,1,NULL
);
941 void MultiWithAttrib::dump(unsigned level
) const
943 DEBUG(level
,"with attrib parameters (%lu pcs)",
944 (unsigned long) elements
.size());
945 for(size_t i
= 0; i
< elements
.size(); i
++)
947 elements
[i
]->dump(level
+ 1);
951 // ==== WithAttribPath ====
953 WithAttribPath::WithAttribPath(const WithAttribPath
& p
)
954 : Node(p
), had_global_variants(false), attributes_checked(false),
955 cached(false), s_o_encode(false), parent(p
.parent
)
957 m_w_attrib
= p
.m_w_attrib
? p
.m_w_attrib
->clone() : 0;
960 WithAttribPath::~WithAttribPath()
966 WithAttribPath
* WithAttribPath::clone() const
968 return new WithAttribPath(*this);
971 void WithAttribPath::set_fullname(const string
& p_fullname
)
973 Node::set_fullname(p_fullname
);
974 if (m_w_attrib
) m_w_attrib
->set_fullname(p_fullname
975 + ".<multiwithattribute>");
978 void WithAttribPath::chk_no_qualif()
980 if(attributes_checked
)
986 const SingleWithAttrib
*swa
;
987 for(int i
= m_w_attrib
->get_nof_elements() - 1; i
>= 0; i
--)
989 swa
= m_w_attrib
->get_element(i
);
990 if( swa
->get_attribQualifiers()
991 && swa
->get_attribQualifiers()->get_nof_qualifiers() != 0)
993 swa
->error("field qualifiers are only allowed"
994 " for record, set and union types");
995 m_w_attrib
->delete_element(i
);
999 attributes_checked
= true;
1002 void WithAttribPath::dump(unsigned int level
) const
1004 DEBUG(level
, "WithAttribPath");
1005 if (!m_w_attrib
) return;
1006 DEBUG(level
+1, "%lu elements",
1007 (unsigned long)m_w_attrib
->get_nof_elements());
1008 for (size_t i
=0; i
< m_w_attrib
->get_nof_elements(); ++i
) {
1009 const SingleWithAttrib
* a
= m_w_attrib
->get_element(i
);
1016 * Checks whether there is inconsistency among global attributes or not.
1017 * Only the last encode can have effect so we can throw out the others.
1018 * This is because encode is not an attribute, but a "context".
1019 * If there is an overriding variant/display/extension then the
1020 * following attributes from the same type should be omitted.
1022 void WithAttribPath::chk_global_attrib(bool erroneous_allowed
)
1027 if (!erroneous_allowed
) {
1028 for(size_t i
= m_w_attrib
->get_nof_elements(); i
> 0; i
--) {
1029 const SingleWithAttrib
* const temp_attrib
=
1030 m_w_attrib
->get_element(i
-1);
1031 if (temp_attrib
->get_attribKeyword()==SingleWithAttrib::AT_ERRONEOUS
) {
1032 temp_attrib
->error("The `erroneous' attribute can be used only on "
1033 "template and constant definitions");
1038 bool has_encode
= false;
1039 bool has_override_variant
= false;
1040 bool has_override_display
= false;
1041 bool has_override_extension
= false;
1042 bool has_override_optional
= false;
1043 for(size_t i
= m_w_attrib
->get_nof_elements(); i
> 0; i
--)
1045 const SingleWithAttrib
* const temp_attrib
=
1046 m_w_attrib
->get_element(i
-1);
1047 switch(temp_attrib
->get_attribKeyword())
1049 case SingleWithAttrib::AT_ENCODE
:
1053 temp_attrib
->warning("Only the last encode "
1054 "of the with statement will have effect");
1055 m_w_attrib
->delete_element(i
-1);
1061 case SingleWithAttrib::AT_ERRONEOUS
:
1063 if (temp_attrib
->has_override()) {
1064 temp_attrib
->error("Override cannot be used with erroneous");
1073 for(size_t i
= 0; i
< m_w_attrib
->get_nof_elements();)
1075 const SingleWithAttrib
* const temp_attrib
= m_w_attrib
->get_element(i
);
1076 switch(temp_attrib
->get_attribKeyword())
1078 case SingleWithAttrib::AT_VARIANT
:
1080 if(has_override_variant
)
1082 temp_attrib
->warning("Only the first override"
1083 " variant of the with statement will have effect");
1084 m_w_attrib
->delete_element(i
);
1086 if(temp_attrib
->has_override())
1087 has_override_variant
= true;
1092 case SingleWithAttrib::AT_DISPLAY
:
1094 if(has_override_display
)
1096 temp_attrib
->warning("Only the first override"
1097 " display of the with statement will have effect");
1098 m_w_attrib
->delete_element(i
);
1100 if(temp_attrib
->has_override())
1101 has_override_display
= true;
1106 case SingleWithAttrib::AT_EXTENSION
:
1108 if(has_override_extension
)
1110 temp_attrib
->warning("Only the first override"
1111 " extension of the with statement will have effect");
1112 m_w_attrib
->delete_element(i
);
1114 if(temp_attrib
->has_override())
1115 has_override_extension
= true;
1120 case SingleWithAttrib::AT_OPTIONAL
:
1122 if ("implicit omit" != temp_attrib
->get_attribSpec().get_spec() &&
1123 "explicit omit" != temp_attrib
->get_attribSpec().get_spec()) {
1124 temp_attrib
->error("Value of optional attribute can only be "
1125 "either 'explicit omit' or 'implicit omit' not '%s'",
1126 temp_attrib
->get_attribSpec().get_spec().c_str());
1128 if(has_override_optional
)
1130 temp_attrib
->warning("Only the first override"
1131 " optional of the with statement will have effect");
1132 m_w_attrib
->delete_element(i
);
1134 if(temp_attrib
->has_override())
1135 has_override_optional
= true;
1147 void WithAttribPath::set_with_attr(MultiWithAttrib
* p_m_w_attr
)
1149 if(m_w_attrib
) FATAL_ERROR("WithAttribPath::set_with_attr()");
1150 m_w_attrib
= p_m_w_attr
;
1151 attributes_checked
= false;
1155 * Finds the real attributes checking the inherited ones with its own.
1156 * Only qualifierless attributes are handled.
1157 * The stepped_over_encode is needed because it can happen that we
1158 * override an encode and later (inner) find variants without encode.
1159 * As those were the overridden encode's variants we can not add them to
1162 void WithAttribPath::qualifierless_attrib_finder(
1163 vector
<SingleWithAttrib
>& p_result
,
1164 bool& stepped_over_encode
)
1168 for(size_t i
= 0; i
< cache
.size(); i
++)
1170 p_result
.add(cache
[i
]);
1172 stepped_over_encode
= s_o_encode
;
1177 parent
->qualifierless_attrib_finder(p_result
,stepped_over_encode
);
1179 stepped_over_encode
= false;
1183 // These two refer only to the attributes of this type
1184 int self_encode_index
= -1; // the index of the "encode" attribute
1185 bool self_has_variant
= false; // flag for the presence of a "variant"
1186 // The following refer to all the attributes, including those collected
1187 // from the parent and all the ancestors.
1188 bool par_has_override_encode
= false;
1189 bool par_has_encode
= false;
1190 // True if there is an encode attribute in the local attribute list,
1191 // it differs from the parents encode
1192 // and the parent does not overwrite it.
1193 bool new_local_encode_context
= false;
1194 bool par_has_override_variant
= false;
1195 bool par_has_override_display
= false;
1196 bool par_has_override_extension
= false;
1197 bool par_has_override_optional
= false;
1199 //checking the owned attributes
1200 const SingleWithAttrib
* act_single
;
1201 const Qualifiers
* act_qualifiers
;
1202 const size_t m_w_attrib_nof_elements
= m_w_attrib
->get_nof_elements();
1203 for(size_t i
= 0; i
< m_w_attrib_nof_elements
;i
++)
1205 act_single
= m_w_attrib
->get_element(i
);
1206 act_qualifiers
= act_single
->get_attribQualifiers();
1209 if(!act_qualifiers
|| act_qualifiers
->get_nof_qualifiers() == 0)
1211 switch(act_single
->get_attribKeyword())
1213 case SingleWithAttrib::AT_ENCODE
:
1214 self_encode_index
= i
;
1217 case SingleWithAttrib::AT_VARIANT
: {
1218 // Ignore JSON variants, these should not produce warnings
1219 const string
& spec
= act_single
->get_attribSpec().get_spec();
1221 while (i2
< spec
.size()) {
1222 if (spec
[i2
] != ' ' && spec
[i2
] != '\t') {
1227 if (i2
== spec
.size() || spec
.find("JSON", i2
) != i2
) {
1228 self_has_variant
= true;
1232 case SingleWithAttrib::AT_DISPLAY
:
1233 case SingleWithAttrib::AT_EXTENSION
:
1234 case SingleWithAttrib::AT_OPTIONAL
:
1235 case SingleWithAttrib::AT_ERRONEOUS
:
1236 case SingleWithAttrib::AT_INVALID
:
1240 FATAL_ERROR("WithAttribPath::attrib_finder()");
1245 // Here p_result contains attributes collected from outer scopes only.
1246 size_t p_result_size
= p_result
.size();
1247 // gather information on the attributes collected from the parents
1248 for(size_t i
= 0; i
< p_result_size
; i
++)
1250 act_single
= p_result
[i
];
1252 switch(act_single
->get_attribKeyword())
1254 case SingleWithAttrib::AT_ENCODE
:
1255 par_has_encode
= true;
1256 par_has_override_encode
|= act_single
->has_override();
1257 if(self_encode_index
!= -1)
1259 // We also have an encode. See if they differ.
1260 new_local_encode_context
= (act_single
->get_attribSpec().get_spec()
1261 != m_w_attrib
->get_element(self_encode_index
)->
1262 get_attribSpec().get_spec());
1266 case SingleWithAttrib::AT_VARIANT
:
1267 par_has_override_variant
|= act_single
->has_override();
1269 case SingleWithAttrib::AT_DISPLAY
:
1270 par_has_override_display
|= act_single
->has_override();
1272 case SingleWithAttrib::AT_EXTENSION
:
1273 par_has_override_extension
|= act_single
->has_override();
1275 case SingleWithAttrib::AT_OPTIONAL
:
1276 par_has_override_optional
|= act_single
->has_override();
1278 case SingleWithAttrib::AT_ERRONEOUS
:
1279 case SingleWithAttrib::AT_INVALID
:
1283 FATAL_ERROR("WithAttribPath::attrib_finder()");
1287 if(!par_has_encode
&& self_encode_index
== -1 && self_has_variant
)
1289 // There is no encode, but there is at least one variant.
1290 // Find them and issue warnings.
1291 for(size_t i
= 0; i
< m_w_attrib_nof_elements
; i
++)
1293 act_single
= m_w_attrib
->get_element(i
);
1294 if(act_single
->get_attribKeyword() == SingleWithAttrib::AT_VARIANT
)
1295 act_single
->warning("This variant does not belong to an encode");
1299 // If we have an encode (self_encode_index != -1) and it differs from
1300 // the outer encode (new_local_encode_context) and the outer wasn't
1301 // an "override encode" (!par_has_override_encode),
1302 // remove the outer encode and all the variants that belong to it.
1303 for(size_t i
= p_result
.size(); i
> 0; i
--)
1305 switch(p_result
[i
-1]->get_attribKeyword())
1307 case SingleWithAttrib::AT_ENCODE
:
1308 case SingleWithAttrib::AT_VARIANT
:
1309 if (self_encode_index
!= -1 && new_local_encode_context
1310 && !par_has_override_encode
)
1311 p_result
.replace(i
-1,1,NULL
);
1313 case SingleWithAttrib::AT_DISPLAY
:
1314 case SingleWithAttrib::AT_EXTENSION
:
1315 case SingleWithAttrib::AT_OPTIONAL
:
1316 case SingleWithAttrib::AT_ERRONEOUS
:
1317 case SingleWithAttrib::AT_INVALID
:
1322 //adding the right ones from the local attributes
1323 for(size_t i
= 0; i
< m_w_attrib_nof_elements
; i
++)
1325 act_single
= m_w_attrib
->get_element(i
);
1326 act_qualifiers
= act_single
->get_attribQualifiers();
1327 if(!act_qualifiers
|| act_qualifiers
->get_nof_qualifiers() == 0)
1329 switch(act_single
->get_attribKeyword())
1331 case SingleWithAttrib::AT_ENCODE
:
1332 if((par_has_encode
&& !par_has_override_encode
1333 && new_local_encode_context
)
1334 || (!par_has_encode
))
1336 p_result
.add_front(m_w_attrib
->get_element_for_modification(i
));
1337 stepped_over_encode
= false;
1338 }else if(new_local_encode_context
){
1339 //par_has_encode && par_has_override_encode
1340 stepped_over_encode
= true;
1342 stepped_over_encode
= false;
1345 case SingleWithAttrib::AT_VARIANT
:
1346 if((!par_has_encode
&& !par_has_override_variant
)
1347 || (par_has_encode
&& self_encode_index
== -1
1348 && !stepped_over_encode
&& !par_has_override_variant
)
1349 || (par_has_encode
&& self_encode_index
!= -1
1350 && !par_has_override_encode
&& !par_has_override_variant
)
1351 || (!par_has_encode
&& self_encode_index
!= -1))
1352 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1354 case SingleWithAttrib::AT_DISPLAY
:
1355 if(!par_has_override_display
)
1356 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1358 case SingleWithAttrib::AT_EXTENSION
:
1359 if(!par_has_override_extension
)
1360 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1362 case SingleWithAttrib::AT_OPTIONAL
:
1363 if (!par_has_override_optional
)
1364 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1366 case SingleWithAttrib::AT_ERRONEOUS
:
1367 case SingleWithAttrib::AT_INVALID
:
1377 * Be very cautious this function gives back only the qualifierless attributes.
1378 * Because of types giving back all the attribs, so that they are already
1379 * in their final encode context would mean that attributes coming from
1380 * the parent path would need to be cloned and qualified as many times
1381 * as many components the type has.
1383 const vector
<SingleWithAttrib
>& WithAttribPath::get_real_attrib()
1386 qualifierless_attrib_finder(cache
,s_o_encode
);
1392 bool WithAttribPath::has_attribs()
1394 if (had_global_variants
) return true;
1395 else if (get_real_attrib().size() > 0) return true;
1396 else if (m_w_attrib
) {
1397 for (size_t i
= 0; i
< m_w_attrib
->get_nof_elements(); i
++) {
1398 const Qualifiers
*qualifiers
=
1399 m_w_attrib
->get_element(i
)->get_attribQualifiers();
1400 if (qualifiers
&& qualifiers
->get_nof_qualifiers() > 0) return true;