1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 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 act_attr
->error("A `raw' %s value was used for erroneous type `%s' which has no TEXT or XER encodings.",
548 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
552 if (!type
->has_encoding(Type::CT_XER
)) {
553 act_attr
->error("A `raw' %s value was used for erroneous type `%s' which has no XER encoding.",
554 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
561 // the two types must have at least one common encoding
562 if (!((type
->has_encoding(Type::CT_BER
)&&ti_type
->has_encoding(Type::CT_BER
)) ||
563 (type
->has_encoding(Type::CT_PER
)&&ti_type
->has_encoding(Type::CT_PER
)) ||
564 (type
->has_encoding(Type::CT_RAW
)&&ti_type
->has_encoding(Type::CT_RAW
)) ||
565 (type
->has_encoding(Type::CT_TEXT
)&&ti_type
->has_encoding(Type::CT_TEXT
)) ||
566 (type
->has_encoding(Type::CT_XER
)&&ti_type
->has_encoding(Type::CT_XER
)) ||
567 (type
->has_encoding(Type::CT_JSON
)&&ti_type
->has_encoding(Type::CT_JSON
)))) {
568 act_attr
->error("Type `%s' and type `%s' have no common encoding",
569 ti_type
->get_typename().c_str(), type
->get_typename().c_str());
575 // for every erroneous field calculate the corresponding index and type arrays
576 // for example: x[5].z -> [3,5,2] and [MyRec,MyRecOf,MyUnion]
577 // MyRec.x field has index 3, etc.
578 for (size_t i
=0; i
<field_array
.size(); i
++) {
579 bool b
= type
->get_subrefs_as_array(field_array
[i
].qualifier
, field_array
[i
].subrefs_array
, field_array
[i
].type_array
);
580 if (!b
) FATAL_ERROR("ErroneousAttributes::chk()");
582 // check the qualifiers and build the tree
583 err_descr_tree
= build_descr_tree(field_array
);
586 ErroneousDescriptor
* ErroneousAttributes::build_descr_tree(dynamic_array
<field_err_t
>& fld_array
)
588 ErroneousDescriptor
* err_descr
= new ErroneousDescriptor();
589 const Qualifier
* omit_before_qual
= NULL
, * omit_after_qual
= NULL
;
590 map
< size_t, dynamic_array
<field_err_t
> > embedded_fld_array_m
; // used for recursive calls
591 for (size_t i
=0; i
<fld_array
.size(); i
++) {
592 field_err_t
& act_field_err
= fld_array
[i
];
593 ErroneousAttributeSpec::indicator_t act_indicator
= act_field_err
.err_attr
->get_indicator();
594 bool is_omit
= act_field_err
.err_attr
->get_is_omit();
595 if (act_field_err
.subrefs_array
.size()<1) FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
596 size_t fld_idx
= act_field_err
.subrefs_array
[0];
597 if (omit_before_qual
&& (err_descr
->omit_before
!=-1) && (err_descr
->omit_before
>(int)fld_idx
)) {
598 act_field_err
.qualifier
->error(
599 "Field `%s' cannot be referenced because all fields before field `%s' have been omitted",
600 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_before_qual
->get_stringRepr().c_str());
603 if (omit_after_qual
&& (err_descr
->omit_after
!=-1) && (err_descr
->omit_after
<(int)fld_idx
)) {
604 act_field_err
.qualifier
->error(
605 "Field `%s' cannot be referenced because all fields after field `%s' have been omitted",
606 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_after_qual
->get_stringRepr().c_str());
609 if (act_field_err
.subrefs_array
.size()==1) { // erroneous value
610 if (act_field_err
.type_array
.size()!=1) FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
611 if ((act_field_err
.type_array
[0]->get_typetype()==Type::T_SET_A
) &&
612 is_omit
&& (act_indicator
!=ErroneousAttributeSpec::I_VALUE
)) {
613 act_field_err
.qualifier
->error(
614 "Cannot omit all fields %s `%s' which is a field of an ASN.1 SET type",
615 ErroneousAttributeSpec::get_indicator_str(act_indicator
), act_field_err
.qualifier
->get_stringRepr().c_str());
616 act_field_err
.qualifier
->note(
617 "The order of fields in ASN.1 SET types changes depending on tagging (see X.690 9.3). "
618 "Fields can be omitted individually, independently of the field order which depends on tagging");
621 switch (act_field_err
.type_array
[0]->get_typetype_ttcn3()) {
622 case Type::T_CHOICE_T
:
623 if (act_indicator
!=ErroneousAttributeSpec::I_VALUE
) {
624 act_field_err
.qualifier
->error(
625 "Indicator `%s' cannot be used with reference `%s' which points to a field of a union type",
626 ErroneousAttributeSpec::get_indicator_str(act_indicator
), act_field_err
.qualifier
->get_stringRepr().c_str());
632 if (is_omit
&& (act_indicator
==ErroneousAttributeSpec::I_AFTER
) &&
633 (fld_idx
==act_field_err
.type_array
[0]->get_nof_comps()-1)) {
634 act_field_err
.qualifier
->error(
635 "There is nothing to omit after the last field (%s) of a record/set type",
636 act_field_err
.qualifier
->get_stringRepr().c_str());
642 if (is_omit
&& (act_indicator
==ErroneousAttributeSpec::I_BEFORE
) &&
644 act_field_err
.qualifier
->error(
645 "There is nothing to omit before the first field (%s)",
646 act_field_err
.qualifier
->get_stringRepr().c_str());
653 // check for duplicate value+indicator
654 if (err_descr
->values_m
.has_key(fld_idx
)) {
655 ErroneousValues
* evs
= err_descr
->values_m
[fld_idx
];
656 if ( (evs
->before
&& (act_indicator
==ErroneousAttributeSpec::I_BEFORE
)) ||
657 (evs
->value
&& (act_indicator
==ErroneousAttributeSpec::I_VALUE
)) ||
658 (evs
->after
&& (act_indicator
==ErroneousAttributeSpec::I_AFTER
)) ) {
659 act_field_err
.qualifier
->error(
660 "Duplicate reference to field `%s' with indicator `%s'",
661 act_field_err
.qualifier
->get_stringRepr().c_str(), ErroneousAttributeSpec::get_indicator_str(act_indicator
));
665 // when overwriting a value check if embedded values were used
666 if ((act_indicator
==ErroneousAttributeSpec::I_VALUE
) && embedded_fld_array_m
.has_key(fld_idx
)) {
667 act_field_err
.qualifier
->error(
668 "Reference to field `%s' with indicator `value' would invalidate previously specified erroneous data",
669 act_field_err
.qualifier
->get_stringRepr().c_str());
672 // if before/after omit then check that no references to omitted regions and no duplication of omit before/after rule
673 if ((act_indicator
==ErroneousAttributeSpec::I_BEFORE
) && is_omit
) {
674 if (omit_before_qual
&& (err_descr
->omit_before
!=-1)) {
675 act_field_err
.qualifier
->error(
676 "Duplicate rule for omitting all fields before the specified field. Used on field `%s' but previously already used on field `%s'",
677 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_before_qual
->get_stringRepr().c_str());
680 bool is_invalid
= false;
681 for (size_t j
=0; j
<err_descr
->values_m
.size(); j
++) {
682 if (err_descr
->values_m
.get_nth_key(j
)<fld_idx
) {
688 for (size_t j
=0; j
<embedded_fld_array_m
.size(); j
++) {
689 if (embedded_fld_array_m
.get_nth_key(j
)<fld_idx
) {
696 act_field_err
.qualifier
->error(
697 "Omitting fields before field `%s' would invalidate previously specified erroneous data",
698 act_field_err
.qualifier
->get_stringRepr().c_str());
701 // save valid omit before data
702 omit_before_qual
= act_field_err
.qualifier
;
703 err_descr
->omit_before
= fld_idx
;
704 err_descr
->omit_before_name
= omit_before_qual
->get_stringRepr();
707 if ((act_indicator
==ErroneousAttributeSpec::I_AFTER
) && is_omit
) {
708 if (omit_after_qual
&& (err_descr
->omit_after
!=-1)) {
709 act_field_err
.qualifier
->error(
710 "Duplicate rule for omitting all fields after the specified field. Used on field `%s' but previously already used on field `%s'",
711 act_field_err
.qualifier
->get_stringRepr().c_str(), omit_after_qual
->get_stringRepr().c_str());
714 bool is_invalid
= false;
715 for (size_t j
=0; j
<err_descr
->values_m
.size(); j
++) {
716 if (err_descr
->values_m
.get_nth_key(j
)>fld_idx
) {
722 for (size_t j
=0; j
<embedded_fld_array_m
.size(); j
++) {
723 if (embedded_fld_array_m
.get_nth_key(j
)>fld_idx
) {
730 act_field_err
.qualifier
->error(
731 "Omitting fields after field `%s' would invalidate previously specified erroneous data",
732 act_field_err
.qualifier
->get_stringRepr().c_str());
735 // save valid omit after data
736 omit_after_qual
= act_field_err
.qualifier
;
737 err_descr
->omit_after
= fld_idx
;
738 err_descr
->omit_after_name
= omit_after_qual
->get_stringRepr();
741 // if not before/after omit then save this into values_m
742 bool has_key
= err_descr
->values_m
.has_key(fld_idx
);
743 ErroneousValues
* evs
= has_key
? err_descr
->values_m
[fld_idx
] : new ErroneousValues(act_field_err
.qualifier
->get_stringRepr());
744 switch (act_indicator
) {
745 case ErroneousAttributeSpec::I_BEFORE
: evs
->before
= act_field_err
.err_attr
; break;
746 case ErroneousAttributeSpec::I_VALUE
: evs
->value
= act_field_err
.err_attr
; break;
747 case ErroneousAttributeSpec::I_AFTER
: evs
->after
= act_field_err
.err_attr
; break;
748 default: FATAL_ERROR("ErroneousAttributes::build_descr_tree()");
751 err_descr
->values_m
.add(fld_idx
, evs
);
753 } else { // embedded err.value
754 if ((err_descr
->values_m
.has_key(fld_idx
)) && (err_descr
->values_m
[fld_idx
]->value
)) {
755 act_field_err
.qualifier
->error(
756 "Field `%s' is embedded into a field which was previously overwritten or omitted",
757 act_field_err
.qualifier
->get_stringRepr().c_str());
760 // add the embedded field to the map
761 bool has_idx
= embedded_fld_array_m
.has_key(fld_idx
);
762 dynamic_array
<field_err_t
>* emb_fld_array
= has_idx
? embedded_fld_array_m
[fld_idx
] : new dynamic_array
<field_err_t
>();
763 field_err_t emb_field_err
= act_field_err
;
764 emb_field_err
.subrefs_array
.remove(0); // remove the first reference
765 emb_field_err
.type_array
.remove(0);
766 emb_fld_array
->add(emb_field_err
);
768 embedded_fld_array_m
.add(fld_idx
, emb_fld_array
);
772 // recursive calls to create embedded descriptors
773 for (size_t i
=0; i
<embedded_fld_array_m
.size(); i
++) {
774 dynamic_array
<field_err_t
>* emb_fld_array
= embedded_fld_array_m
.get_nth_elem(i
);
775 err_descr
->descr_m
.add(embedded_fld_array_m
.get_nth_key(i
), build_descr_tree(*emb_fld_array
));
776 delete emb_fld_array
;
778 embedded_fld_array_m
.clear();
782 // ==== AttributeSpec ====
784 AttributeSpec
* AttributeSpec::clone() const
786 return new AttributeSpec(*this);
789 void AttributeSpec::set_fullname(const string
& p_fullname
)
791 Node::set_fullname(p_fullname
);
794 void AttributeSpec::dump(unsigned level
) const
796 DEBUG(level
,"spec: %s", spec
.c_str());
799 // ==== SingleWithAttrib ====
801 SingleWithAttrib::SingleWithAttrib(const SingleWithAttrib
& p
)
802 : Node(p
), Location(p
), attribKeyword(p
.attribKeyword
),
803 hasOverride(p
.hasOverride
)
805 attribQualifiers
= p
.attribQualifiers
? p
.attribQualifiers
->clone() : 0;
806 attribSpec
= p
.attribSpec
->clone();
809 SingleWithAttrib::SingleWithAttrib(
810 attribtype_t p_attribKeyword
, bool p_hasOverride
,
811 Qualifiers
*p_attribQualifiers
, AttributeSpec
* p_attribSpec
)
812 : Node(), Location(), attribKeyword(p_attribKeyword
),
813 hasOverride(p_hasOverride
), attribQualifiers(p_attribQualifiers
),
814 attribSpec(p_attribSpec
)
817 FATAL_ERROR("SingleWithAttrib::SingleWithAttrib()");
820 SingleWithAttrib::~SingleWithAttrib()
822 delete attribQualifiers
;
826 SingleWithAttrib
* SingleWithAttrib::clone() const
828 return new SingleWithAttrib(*this);
831 void SingleWithAttrib::set_fullname(const string
& p_fullname
)
833 Node::set_fullname(p_fullname
);
835 attribQualifiers
->set_fullname(p_fullname
+ ".<attribute qualifiers>");
836 attribSpec
->set_fullname(p_fullname
+ ".<attribute specification>");
839 void SingleWithAttrib::dump(unsigned level
) const
841 DEBUG(level
,"attribute");
842 switch(attribKeyword
)
845 DEBUG(level
+ 1,"keyword: encode");
848 DEBUG(level
+ 1,"keyword: variant");
851 DEBUG(level
+ 1,"keyword: display");
854 DEBUG(level
+ 1,"keyword: extension");
857 DEBUG(level
+ 1,"keyword: optional");
860 DEBUG(level
+1, "keyword: erroneous");
863 DEBUG(level
+1, "invalid keyword");
866 FATAL_ERROR("SingleWithAttrib::dump()");
869 DEBUG(level
+ 1, hasOverride
? "has override" : "hasn't got override");
872 attribSpec
->dump(level
+ 1);
874 attribQualifiers
->dump(level
+ 1);
877 // ==== MultiWithAttrib ====
879 MultiWithAttrib::MultiWithAttrib(const MultiWithAttrib
& p
)
880 : Node(p
), Location(p
)
882 for(size_t i
= 0; i
< p
.get_nof_elements(); i
++)
884 elements
.add(p
.get_element(i
)->clone());
888 MultiWithAttrib
* MultiWithAttrib::clone() const
890 return new MultiWithAttrib(*this);
893 void MultiWithAttrib::set_fullname(const string
& p_fullname
)
895 Node::set_fullname(p_fullname
);
896 for(size_t i
= 0; i
< elements
.size(); i
++)
898 elements
[i
]->set_fullname(p_fullname
+ ".<singlewithattribute "
899 + Int2string(i
) + ">");
903 MultiWithAttrib::~MultiWithAttrib()
905 for(size_t i
= 0; i
< elements
.size(); i
++)
912 const SingleWithAttrib
* MultiWithAttrib::get_element(size_t p_index
) const
914 return elements
[p_index
];
917 SingleWithAttrib
* MultiWithAttrib::get_element_for_modification(
920 return elements
[p_index
];
923 void MultiWithAttrib::delete_element(size_t p_index
)
925 delete elements
[p_index
];
926 elements
.replace(p_index
,1,NULL
);
929 void MultiWithAttrib::dump(unsigned level
) const
931 DEBUG(level
,"with attrib parameters (%lu pcs)",
932 (unsigned long) elements
.size());
933 for(size_t i
= 0; i
< elements
.size(); i
++)
935 elements
[i
]->dump(level
+ 1);
939 // ==== WithAttribPath ====
941 WithAttribPath::WithAttribPath(const WithAttribPath
& p
)
942 : Node(p
), had_global_variants(false), attributes_checked(false),
943 cached(false), s_o_encode(false), parent(p
.parent
)
945 m_w_attrib
= p
.m_w_attrib
? p
.m_w_attrib
->clone() : 0;
948 WithAttribPath::~WithAttribPath()
954 WithAttribPath
* WithAttribPath::clone() const
956 return new WithAttribPath(*this);
959 void WithAttribPath::set_fullname(const string
& p_fullname
)
961 Node::set_fullname(p_fullname
);
962 if (m_w_attrib
) m_w_attrib
->set_fullname(p_fullname
963 + ".<multiwithattribute>");
966 void WithAttribPath::chk_no_qualif()
968 if(attributes_checked
)
974 const SingleWithAttrib
*swa
;
975 for(int i
= m_w_attrib
->get_nof_elements() - 1; i
>= 0; i
--)
977 swa
= m_w_attrib
->get_element(i
);
978 if( swa
->get_attribQualifiers()
979 && swa
->get_attribQualifiers()->get_nof_qualifiers() != 0)
981 swa
->error("field qualifiers are only allowed"
982 " for record, set and union types");
983 m_w_attrib
->delete_element(i
);
987 attributes_checked
= true;
990 void WithAttribPath::dump(unsigned int level
) const
992 DEBUG(level
, "WithAttribPath");
993 if (!m_w_attrib
) return;
994 DEBUG(level
+1, "%lu elements",
995 (unsigned long)m_w_attrib
->get_nof_elements());
996 for (size_t i
=0; i
< m_w_attrib
->get_nof_elements(); ++i
) {
997 const SingleWithAttrib
* a
= m_w_attrib
->get_element(i
);
1004 * Checks whether there is inconsistency among global attributes or not.
1005 * Only the last encode can have effect so we can throw out the others.
1006 * This is because encode is not an attribute, but a "context".
1007 * If there is an overriding variant/display/extension then the
1008 * following attributes from the same type should be omitted.
1010 void WithAttribPath::chk_global_attrib(bool erroneous_allowed
)
1015 if (!erroneous_allowed
) {
1016 for(size_t i
= m_w_attrib
->get_nof_elements(); i
> 0; i
--) {
1017 const SingleWithAttrib
* const temp_attrib
=
1018 m_w_attrib
->get_element(i
-1);
1019 if (temp_attrib
->get_attribKeyword()==SingleWithAttrib::AT_ERRONEOUS
) {
1020 temp_attrib
->error("The `erroneous' attribute can be used only on "
1021 "template and constant definitions");
1026 bool has_encode
= false;
1027 bool has_override_variant
= false;
1028 bool has_override_display
= false;
1029 bool has_override_extension
= false;
1030 bool has_override_optional
= false;
1031 for(size_t i
= m_w_attrib
->get_nof_elements(); i
> 0; i
--)
1033 const SingleWithAttrib
* const temp_attrib
=
1034 m_w_attrib
->get_element(i
-1);
1035 switch(temp_attrib
->get_attribKeyword())
1037 case SingleWithAttrib::AT_ENCODE
:
1041 temp_attrib
->warning("Only the last encode "
1042 "of the with statement will have effect");
1043 m_w_attrib
->delete_element(i
-1);
1049 case SingleWithAttrib::AT_ERRONEOUS
:
1051 if (temp_attrib
->has_override()) {
1052 temp_attrib
->error("Override cannot be used with erroneous");
1061 for(size_t i
= 0; i
< m_w_attrib
->get_nof_elements();)
1063 const SingleWithAttrib
* const temp_attrib
= m_w_attrib
->get_element(i
);
1064 switch(temp_attrib
->get_attribKeyword())
1066 case SingleWithAttrib::AT_VARIANT
:
1068 if(has_override_variant
)
1070 temp_attrib
->warning("Only the first override"
1071 " variant of the with statement will have effect");
1072 m_w_attrib
->delete_element(i
);
1074 if(temp_attrib
->has_override())
1075 has_override_variant
= true;
1080 case SingleWithAttrib::AT_DISPLAY
:
1082 if(has_override_display
)
1084 temp_attrib
->warning("Only the first override"
1085 " display of the with statement will have effect");
1086 m_w_attrib
->delete_element(i
);
1088 if(temp_attrib
->has_override())
1089 has_override_display
= true;
1094 case SingleWithAttrib::AT_EXTENSION
:
1096 if(has_override_extension
)
1098 temp_attrib
->warning("Only the first override"
1099 " extension of the with statement will have effect");
1100 m_w_attrib
->delete_element(i
);
1102 if(temp_attrib
->has_override())
1103 has_override_extension
= true;
1108 case SingleWithAttrib::AT_OPTIONAL
:
1110 if ("implicit omit" != temp_attrib
->get_attribSpec().get_spec() &&
1111 "explicit omit" != temp_attrib
->get_attribSpec().get_spec()) {
1112 temp_attrib
->error("Value of optional attribute can only be "
1113 "either 'explicit omit' or 'implicit omit' not '%s'",
1114 temp_attrib
->get_attribSpec().get_spec().c_str());
1116 if(has_override_optional
)
1118 temp_attrib
->warning("Only the first override"
1119 " optional of the with statement will have effect");
1120 m_w_attrib
->delete_element(i
);
1122 if(temp_attrib
->has_override())
1123 has_override_optional
= true;
1135 void WithAttribPath::set_with_attr(MultiWithAttrib
* p_m_w_attr
)
1137 if(m_w_attrib
) FATAL_ERROR("WithAttribPath::set_with_attr()");
1138 m_w_attrib
= p_m_w_attr
;
1139 attributes_checked
= false;
1143 * Finds the real attributes checking the inherited ones with its own.
1144 * Only qualifierless attributes are handled.
1145 * The stepped_over_encode is needed because it can happen that we
1146 * override an encode and later (inner) find variants without encode.
1147 * As those were the overridden encode's variants we can not add them to
1150 void WithAttribPath::qualifierless_attrib_finder(
1151 vector
<SingleWithAttrib
>& p_result
,
1152 bool& stepped_over_encode
)
1156 for(size_t i
= 0; i
< cache
.size(); i
++)
1158 p_result
.add(cache
[i
]);
1160 stepped_over_encode
= s_o_encode
;
1165 parent
->qualifierless_attrib_finder(p_result
,stepped_over_encode
);
1167 stepped_over_encode
= false;
1171 // These two refer only to the attributes of this type
1172 int self_encode_index
= -1; // the index of the "encode" attribute
1173 bool self_has_variant
= false; // flag for the presence of a "variant"
1174 // The following refer to all the attributes, including those collected
1175 // from the parent and all the ancestors.
1176 bool par_has_override_encode
= false;
1177 bool par_has_encode
= false;
1178 // True if there is an encode attribute in the local attribute list,
1179 // it differs from the parents encode
1180 // and the parent does not overwrite it.
1181 bool new_local_encode_context
= false;
1182 bool par_has_override_variant
= false;
1183 bool par_has_override_display
= false;
1184 bool par_has_override_extension
= false;
1185 bool par_has_override_optional
= false;
1187 //checking the owned attributes
1188 const SingleWithAttrib
* act_single
;
1189 const Qualifiers
* act_qualifiers
;
1190 const size_t m_w_attrib_nof_elements
= m_w_attrib
->get_nof_elements();
1191 for(size_t i
= 0; i
< m_w_attrib_nof_elements
;i
++)
1193 act_single
= m_w_attrib
->get_element(i
);
1194 act_qualifiers
= act_single
->get_attribQualifiers();
1197 if(!act_qualifiers
|| act_qualifiers
->get_nof_qualifiers() == 0)
1199 switch(act_single
->get_attribKeyword())
1201 case SingleWithAttrib::AT_ENCODE
:
1202 self_encode_index
= i
;
1205 case SingleWithAttrib::AT_VARIANT
: {
1206 // Ignore JSON variants, these should not produce warnings
1207 const string
& spec
= act_single
->get_attribSpec().get_spec();
1209 while (i
< spec
.size()) {
1210 if (spec
[i
] != ' ' && spec
[i
] != '\t') {
1215 if (i
== spec
.size() || spec
.find("JSON", i
) != i
) {
1216 self_has_variant
= true;
1220 case SingleWithAttrib::AT_DISPLAY
:
1221 case SingleWithAttrib::AT_EXTENSION
:
1222 case SingleWithAttrib::AT_OPTIONAL
:
1223 case SingleWithAttrib::AT_ERRONEOUS
:
1224 case SingleWithAttrib::AT_INVALID
:
1228 FATAL_ERROR("WithAttribPath::attrib_finder()");
1233 // Here p_result contains attributes collected from outer scopes only.
1234 size_t p_result_size
= p_result
.size();
1235 // gather information on the attributes collected from the parents
1236 for(size_t i
= 0; i
< p_result_size
; i
++)
1238 act_single
= p_result
[i
];
1240 switch(act_single
->get_attribKeyword())
1242 case SingleWithAttrib::AT_ENCODE
:
1243 par_has_encode
= true;
1244 par_has_override_encode
|= act_single
->has_override();
1245 if(self_encode_index
!= -1)
1247 // We also have an encode. See if they differ.
1248 new_local_encode_context
= (act_single
->get_attribSpec().get_spec()
1249 != m_w_attrib
->get_element(self_encode_index
)->
1250 get_attribSpec().get_spec());
1254 case SingleWithAttrib::AT_VARIANT
:
1255 par_has_override_variant
|= act_single
->has_override();
1257 case SingleWithAttrib::AT_DISPLAY
:
1258 par_has_override_display
|= act_single
->has_override();
1260 case SingleWithAttrib::AT_EXTENSION
:
1261 par_has_override_extension
|= act_single
->has_override();
1263 case SingleWithAttrib::AT_OPTIONAL
:
1264 par_has_override_optional
|= act_single
->has_override();
1266 case SingleWithAttrib::AT_ERRONEOUS
:
1267 case SingleWithAttrib::AT_INVALID
:
1271 FATAL_ERROR("WithAttribPath::attrib_finder()");
1275 if(!par_has_encode
&& self_encode_index
== -1 && self_has_variant
)
1277 // There is no encode, but there is at least one variant.
1278 // Find them and issue warnings.
1279 for(size_t i
= 0; i
< m_w_attrib_nof_elements
; i
++)
1281 act_single
= m_w_attrib
->get_element(i
);
1282 if(act_single
->get_attribKeyword() == SingleWithAttrib::AT_VARIANT
)
1283 act_single
->warning("This variant does not belong to an encode");
1287 // If we have an encode (self_encode_index != -1) and it differs from
1288 // the outer encode (new_local_encode_context) and the outer wasn't
1289 // an "override encode" (!par_has_override_encode),
1290 // remove the outer encode and all the variants that belong to it.
1291 for(size_t i
= p_result
.size(); i
> 0; i
--)
1293 switch(p_result
[i
-1]->get_attribKeyword())
1295 case SingleWithAttrib::AT_ENCODE
:
1296 case SingleWithAttrib::AT_VARIANT
:
1297 if (self_encode_index
!= -1 && new_local_encode_context
1298 && !par_has_override_encode
)
1299 p_result
.replace(i
-1,1,NULL
);
1301 case SingleWithAttrib::AT_DISPLAY
:
1302 case SingleWithAttrib::AT_EXTENSION
:
1303 case SingleWithAttrib::AT_OPTIONAL
:
1304 case SingleWithAttrib::AT_ERRONEOUS
:
1305 case SingleWithAttrib::AT_INVALID
:
1310 //adding the right ones from the local attributes
1311 for(size_t i
= 0; i
< m_w_attrib_nof_elements
; i
++)
1313 act_single
= m_w_attrib
->get_element(i
);
1314 act_qualifiers
= act_single
->get_attribQualifiers();
1315 if(!act_qualifiers
|| act_qualifiers
->get_nof_qualifiers() == 0)
1317 switch(act_single
->get_attribKeyword())
1319 case SingleWithAttrib::AT_ENCODE
:
1320 if((par_has_encode
&& !par_has_override_encode
1321 && new_local_encode_context
)
1322 || (!par_has_encode
))
1324 p_result
.add_front(m_w_attrib
->get_element_for_modification(i
));
1325 stepped_over_encode
= false;
1326 }else if(new_local_encode_context
){
1327 //par_has_encode && par_has_override_encode
1328 stepped_over_encode
= true;
1330 stepped_over_encode
= false;
1333 case SingleWithAttrib::AT_VARIANT
:
1334 if((!par_has_encode
&& !par_has_override_variant
)
1335 || (par_has_encode
&& self_encode_index
== -1
1336 && !stepped_over_encode
&& !par_has_override_variant
)
1337 || (par_has_encode
&& self_encode_index
!= -1
1338 && !par_has_override_encode
&& !par_has_override_variant
)
1339 || (!par_has_encode
&& self_encode_index
!= -1))
1340 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1342 case SingleWithAttrib::AT_DISPLAY
:
1343 if(!par_has_override_display
)
1344 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1346 case SingleWithAttrib::AT_EXTENSION
:
1347 if(!par_has_override_extension
)
1348 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1350 case SingleWithAttrib::AT_OPTIONAL
:
1351 if (!par_has_override_optional
)
1352 p_result
.add(m_w_attrib
->get_element_for_modification(i
));
1354 case SingleWithAttrib::AT_ERRONEOUS
:
1355 case SingleWithAttrib::AT_INVALID
:
1365 * Be very cautious this function gives back only the qualifierless attributes.
1366 * Because of types giving back all the attribs, so that they are already
1367 * in their final encode context would mean that attributes coming from
1368 * the parent path would need to be cloned and qualified as many times
1369 * as many components the type has.
1371 const vector
<SingleWithAttrib
>& WithAttribPath::get_real_attrib()
1374 qualifierless_attrib_finder(cache
,s_o_encode
);
1380 bool WithAttribPath::has_attribs()
1382 if (had_global_variants
) return true;
1383 else if (get_real_attrib().size() > 0) return true;
1384 else if (m_w_attrib
) {
1385 for (size_t i
= 0; i
< m_w_attrib
->get_nof_elements(); i
++) {
1386 const Qualifiers
*qualifiers
=
1387 m_w_attrib
->get_element(i
)->get_attribQualifiers();
1388 if (qualifiers
&& qualifiers
->get_nof_qualifiers() > 0) return true;