Commit | Line | Data |
---|---|---|
970ed795 EL |
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" | |
9 | #include "../map.hh" | |
10 | #include "../CompilerError.hh" | |
11 | #include "../Type.hh" | |
12 | #include "TtcnTemplate.hh" | |
13 | ||
14 | namespace Ttcn { | |
15 | ||
16 | // ==== Qualifier ==== | |
17 | ||
18 | Qualifier* Qualifier::clone() const | |
19 | { | |
20 | return new Qualifier(*this); | |
21 | } | |
22 | ||
23 | const Identifier* Qualifier::get_identifier(size_t p_index) const | |
24 | { | |
25 | FieldOrArrayRef* ref = get_ref(p_index); | |
26 | return (ref->get_type()==FieldOrArrayRef::FIELD_REF) ? | |
27 | ref->get_id() : | |
28 | &underscore_zero ; | |
29 | } | |
30 | ||
31 | Qualifier* Qualifier::get_qualifier_without_first_id() const | |
32 | { | |
33 | Qualifier* temp = this->clone(); | |
34 | temp->remove_refs(1); | |
35 | return temp; | |
36 | } | |
37 | ||
38 | string Qualifier::get_stringRepr() const | |
39 | { | |
40 | string str_repr; | |
41 | append_stringRepr(str_repr); | |
42 | if (str_repr[0]=='.') str_repr.replace(0, 1, ""); // remove leading dot if present | |
43 | return str_repr; | |
44 | } | |
45 | ||
46 | void Qualifier::dump(unsigned level) const | |
47 | { | |
48 | const string& rep = get_stringRepr(); | |
49 | DEBUG(level, "(%s)", rep.c_str()); | |
50 | } | |
51 | ||
52 | // ==== Qualifiers ==== | |
53 | ||
54 | Qualifiers::~Qualifiers() | |
55 | { | |
56 | for(size_t i = 0; i < qualifiers.size(); i++) | |
57 | { | |
58 | delete qualifiers[i]; | |
59 | } | |
60 | qualifiers.clear(); | |
61 | } | |
62 | ||
63 | Qualifiers::Qualifiers(const Qualifiers& p) | |
64 | : Node(p) | |
65 | { | |
66 | for(size_t i = 0; i < p.get_nof_qualifiers(); i++) | |
67 | { | |
68 | qualifiers.add(p.get_qualifier(i)->clone()); | |
69 | } | |
70 | } | |
71 | ||
72 | void Qualifiers::add_qualifier(Qualifier* p_qualifier) | |
73 | { | |
74 | if(p_qualifier->get_nof_identifiers() > 0) | |
75 | qualifiers.add(p_qualifier); | |
76 | else | |
77 | delete p_qualifier; | |
78 | } | |
79 | ||
80 | void Qualifiers::delete_qualifier(size_t p_index) | |
81 | { | |
82 | delete qualifiers[p_index]; | |
83 | qualifiers.replace(p_index,1,NULL); | |
84 | } | |
85 | ||
86 | const Qualifier* Qualifiers::get_qualifier(size_t p_index) const | |
87 | { | |
88 | return qualifiers[p_index]; | |
89 | } | |
90 | ||
91 | Qualifiers* Qualifiers::clone() const | |
92 | { | |
93 | return new Qualifiers(*this); | |
94 | } | |
95 | ||
96 | void Qualifiers::set_fullname(const string& p_fullname) | |
97 | { | |
98 | Node::set_fullname(p_fullname); | |
99 | for(size_t i = 0; i < qualifiers.size(); i++) | |
100 | { | |
101 | qualifiers[i]->set_fullname(p_fullname + ".<qualifier " | |
102 | + Int2string(i) + ">"); | |
103 | } | |
104 | } | |
105 | ||
106 | bool Qualifiers::has_qualifier(Qualifier* p_qualifier) const | |
107 | { | |
108 | bool result = false; | |
109 | size_t index; | |
110 | bool same; | |
111 | for(size_t i = 0; i < qualifiers.size() && !result; i++) | |
112 | { | |
113 | if(qualifiers[i]->get_nof_identifiers() | |
114 | ==p_qualifier->get_nof_identifiers()) | |
115 | { | |
116 | index = 0; | |
117 | same = true; | |
118 | while(index < p_qualifier->get_nof_identifiers() && same) | |
119 | { | |
120 | same = (*qualifiers[i]->get_identifier(index) | |
121 | == *p_qualifier->get_identifier(index)); | |
122 | index++; | |
123 | } | |
124 | ||
125 | result |= same; | |
126 | } | |
127 | } | |
128 | return result; | |
129 | } | |
130 | ||
131 | void Qualifiers::dump(unsigned level) const | |
132 | { | |
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); | |
137 | } | |
138 | ||
139 | // ==== ErroneousDescriptor ==== | |
140 | ||
141 | ErroneousDescriptor::~ErroneousDescriptor() | |
142 | { | |
143 | for (size_t i=0; i<descr_m.size(); i++) delete descr_m.get_nth_elem(i); | |
144 | descr_m.clear(); | |
145 | for (size_t i=0; i<values_m.size(); i++) delete values_m.get_nth_elem(i); | |
146 | values_m.clear(); | |
147 | } | |
148 | ||
149 | // ==== ErroneousAttributeSpec ==== | |
150 | ||
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) | |
154 | { | |
155 | if (!tmpl_inst) FATAL_ERROR("ErroneousAttributeSpec::ErroneousAttributeSpec()"); | |
156 | } | |
157 | ||
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) | |
161 | { | |
162 | tmpl_inst = p.tmpl_inst ? tmpl_inst->clone() : 0; | |
163 | } | |
164 | ||
165 | ErroneousAttributeSpec::~ErroneousAttributeSpec() | |
166 | { | |
167 | delete tmpl_inst; | |
168 | delete value; | |
169 | } | |
170 | ||
171 | ErroneousAttributeSpec* ErroneousAttributeSpec::clone() const | |
172 | { | |
173 | return new ErroneousAttributeSpec(*this); | |
174 | } | |
175 | ||
176 | void ErroneousAttributeSpec::set_fullname(const string& p_fullname) | |
177 | { | |
178 | Node::set_fullname(p_fullname); | |
179 | tmpl_inst->set_fullname(p_fullname+".<template_instance>"); | |
180 | } | |
181 | ||
182 | void ErroneousAttributeSpec::set_my_scope(Scope *p_scope) | |
183 | { | |
184 | tmpl_inst->set_my_scope(p_scope); | |
185 | } | |
186 | ||
187 | void ErroneousAttributeSpec::dump(unsigned level) const | |
188 | { | |
189 | DEBUG(level, "raw: %s", is_raw ? "yes" : "no"); | |
190 | DEBUG(level, "indicator:"); | |
191 | switch (indicator) { | |
192 | case I_BEFORE: | |
193 | DEBUG(level+1, "before"); | |
194 | break; | |
195 | case I_VALUE: | |
196 | DEBUG(level+1, "value"); | |
197 | break; | |
198 | case I_AFTER: | |
199 | DEBUG(level+1, "after"); | |
200 | break; | |
201 | case I_INVALID: | |
202 | DEBUG(level+1, "<invalid>"); | |
203 | break; | |
204 | default: | |
205 | FATAL_ERROR("ErroneousAttributeSpec::dump()"); | |
206 | } | |
207 | DEBUG(level, "template instance:"); | |
208 | tmpl_inst->dump(level+1); | |
209 | } | |
210 | ||
211 | bool ErroneousAttributeSpec::get_is_omit() const | |
212 | { | |
213 | return (tmpl_inst->get_Template()->get_templatetype()==Template::OMIT_VALUE); | |
214 | } | |
215 | ||
216 | void ErroneousAttributeSpec::chk() | |
217 | { | |
218 | if (get_is_omit()) { // special case, no type needed | |
219 | if ((indicator==I_BEFORE)||(indicator==I_AFTER)) { | |
220 | if (!has_all_keyword) { | |
221 | tmpl_inst->error( | |
222 | "Keyword `all' is expected after `omit' when omitting all fields %s the specified field", | |
223 | get_indicator_str(indicator)); | |
224 | } | |
225 | } else { | |
226 | if (has_all_keyword) { | |
227 | tmpl_inst->error( | |
228 | "Unexpected `all' keyword after `omit' when omitting one field"); | |
229 | } | |
230 | } | |
231 | type = 0; | |
232 | return; | |
233 | } | |
234 | if (has_all_keyword) { | |
235 | tmpl_inst->error( | |
236 | "Unexpected `all' keyword after the in-line template"); | |
237 | } | |
238 | ||
239 | // determine the type of the tmpl_inst | |
240 | type = tmpl_inst->get_expr_governor(Type::EXPECTED_TEMPLATE); | |
241 | if (!type) { | |
242 | tmpl_inst->get_Template()->set_lowerid_to_ref(); | |
243 | type = tmpl_inst->get_expr_governor(Type::EXPECTED_TEMPLATE); | |
244 | } | |
245 | if (!type) { | |
246 | tmpl_inst->error("Cannot determine the type of the in-line template"); | |
247 | return; | |
248 | } | |
249 | type->chk(); | |
250 | Type* type_last = type->get_type_refd_last(); | |
251 | if (!type_last || type_last->get_typetype()==Type::T_ERROR) { | |
252 | type = 0; | |
253 | return; | |
254 | } | |
255 | if (is_raw) { | |
256 | switch (type_last->get_typetype_ttcn3()) { | |
257 | case Type::T_BSTR: | |
258 | case Type::T_OSTR: | |
259 | case Type::T_CSTR: | |
260 | case Type::T_USTR: | |
261 | break; | |
262 | default: | |
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()); | |
265 | } | |
266 | } | |
267 | ||
268 | if (tmpl_inst->get_DerivedRef()) { | |
269 | tmpl_inst->error("Reference to a constant value was expected instead of an in-line modified template"); | |
270 | type = 0; | |
271 | return; | |
272 | } | |
273 | ||
274 | Template* templ = tmpl_inst->get_Template(); | |
275 | ||
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); | |
285 | //} | |
286 | value->set_code_section(GovernedSimple::CS_PRE_INIT); | |
287 | } else { | |
288 | tmpl_inst->error("A specific value without matching symbols was expected"); | |
289 | type = 0; | |
290 | return; | |
291 | } | |
292 | } | |
293 | ||
294 | const char* ErroneousAttributeSpec::get_indicator_str(indicator_t i) | |
295 | { | |
296 | switch (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()"); | |
301 | } | |
302 | return ""; | |
303 | } | |
304 | ||
305 | char* ErroneousAttributeSpec::generate_code_str(char *str, string genname) | |
306 | { | |
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; | |
314 | } else { | |
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()); | |
318 | } | |
319 | return str; | |
320 | } | |
321 | ||
322 | char* ErroneousAttributeSpec::generate_code_init_str(char *str, string genname) | |
323 | { | |
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()); | |
328 | return str; | |
329 | } | |
330 | ||
331 | string ErroneousAttributeSpec::get_typedescriptor_str() | |
332 | { | |
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_" ); | |
337 | } | |
338 | ||
339 | void ErroneousAttributeSpec::chk_recursions(ReferenceChain& refch) | |
340 | { | |
341 | if (value) { | |
342 | refch.mark_state(); | |
343 | value->chk_recursions(refch); | |
344 | refch.prev_state(); | |
345 | } | |
346 | } | |
347 | ||
348 | // ==== ErroneousValues ==== | |
349 | ||
350 | char* ErroneousValues::generate_code_embedded_str(char *str, string genname) | |
351 | { | |
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); | |
355 | return str; | |
356 | } | |
357 | ||
358 | char* ErroneousValues::generate_code_embedded_str(char *str, string genname, ErroneousAttributeSpec* attr_spec) | |
359 | { | |
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()); | |
365 | return str; | |
366 | } | |
367 | ||
368 | char* ErroneousValues::generate_code_init_str(char *str, string genname) | |
369 | { | |
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"); | |
373 | return str; | |
374 | } | |
375 | ||
376 | char* ErroneousValues::generate_code_struct_str(char *str, string genname, int field_index) | |
377 | { | |
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"); | |
383 | return str; | |
384 | } | |
385 | ||
386 | void ErroneousValues::chk_recursions(ReferenceChain& refch) | |
387 | { | |
388 | if (before) before->chk_recursions(refch); | |
389 | if (value) value->chk_recursions(refch); | |
390 | if (after) after->chk_recursions(refch); | |
391 | } | |
392 | ||
393 | // ==== ErroneousDescriptor ==== | |
394 | ||
395 | char* ErroneousDescriptor::generate_code_embedded_str(char *str, string genname) | |
396 | { | |
397 | // values | |
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))); | |
400 | } | |
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))); | |
404 | } | |
405 | // values vector | |
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); | |
412 | } | |
413 | str = mputstr(str, " };\n"); | |
414 | } | |
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); | |
422 | } | |
423 | str = mputstr(str, " };\n"); | |
424 | } | |
425 | return str; | |
426 | } | |
427 | ||
428 | char* ErroneousDescriptor::generate_code_init_str(char *str, string genname) | |
429 | { | |
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))); | |
432 | } | |
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))); | |
435 | } | |
436 | return str; | |
437 | } | |
438 | ||
439 | char* ErroneousDescriptor::generate_code_struct_str(char *str, string genname, int field_index) | |
440 | { | |
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 }", | |
444 | field_index, | |
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"); | |
449 | return str; | |
450 | } | |
451 | ||
452 | void ErroneousDescriptor::chk_recursions(ReferenceChain& refch) | |
453 | { | |
454 | for (size_t i=0; i<values_m.size(); i++) { | |
455 | values_m.get_nth_elem(i)->chk_recursions(refch); | |
456 | } | |
457 | for (size_t i=0; i<descr_m.size(); i++) { | |
458 | descr_m.get_nth_elem(i)->chk_recursions(refch); | |
459 | } | |
460 | } | |
461 | ||
462 | char* ErroneousDescriptor::generate_code_str(char *str, string genname) | |
463 | { | |
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"); | |
469 | return str; | |
470 | } | |
471 | ||
472 | // ==== ErroneousAttributes ==== | |
473 | ||
474 | ErroneousAttributes::ErroneousAttributes(Type* p_type): | |
475 | type(p_type), err_descr_tree(NULL) | |
476 | { | |
477 | if (!type) FATAL_ERROR("ErroneousAttributes::ErroneousAttributes()"); | |
478 | } | |
479 | ||
480 | ErroneousAttributes::~ErroneousAttributes() | |
481 | { | |
482 | for (size_t i=0; i<spec_vec.size(); i++) { | |
483 | delete spec_vec[i]; | |
484 | } | |
485 | spec_vec.clear(); | |
486 | if (err_descr_tree) delete err_descr_tree; | |
487 | } | |
488 | ||
489 | ErroneousAttributes::ErroneousAttributes(const ErroneousAttributes& p) | |
490 | : Node(p), type(p.type), err_descr_tree(NULL) | |
491 | { | |
492 | } | |
493 | ||
494 | ErroneousAttributes* ErroneousAttributes::clone() const | |
495 | { | |
496 | return new ErroneousAttributes(*this); | |
497 | } | |
498 | ||
499 | void ErroneousAttributes::set_fullname(const string& p_fullname) | |
500 | { | |
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)+">"); | |
504 | } | |
505 | } | |
506 | ||
507 | void ErroneousAttributes::dump(unsigned level) const | |
508 | { | |
509 | DEBUG(level, "erroneous attributes:"); | |
510 | for (size_t i=0; i<spec_vec.size(); i++) { | |
511 | spec_vec[i]->dump(level+1); | |
512 | } | |
513 | } | |
514 | ||
515 | void ErroneousAttributes::add_spec(ErroneousAttributeSpec* err_attr_spec) | |
516 | { | |
517 | if (!err_attr_spec) FATAL_ERROR("ErroneousAttributes::add_spec()"); | |
518 | spec_vec.add(err_attr_spec); | |
519 | } | |
520 | ||
521 | void ErroneousAttributes::add_pair(const Qualifier* qualifier, ErroneousAttributeSpec* err_attr_spec) | |
522 | { | |
523 | if (!qualifier || !err_attr_spec) FATAL_ERROR("ErroneousAttributes::add()"); | |
524 | field_err_t f; | |
525 | f.qualifier = qualifier; | |
526 | f.err_attr = err_attr_spec; | |
527 | field_array.add(f); | |
528 | } | |
529 | ||
530 | void ErroneousAttributes::chk() | |
531 | { | |
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()) { | |
539 | case Type::T_BSTR: | |
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()); | |
543 | } | |
544 | break; | |
545 | case Type::T_CSTR: | |
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()); | |
549 | } | |
550 | break; | |
551 | case Type::T_USTR: | |
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()); | |
555 | } | |
556 | break; | |
557 | default: | |
558 | break; | |
559 | } | |
560 | } else { | |
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()); | |
570 | } | |
571 | } | |
572 | } | |
573 | } | |
574 | ||
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()"); | |
581 | } | |
582 | // check the qualifiers and build the tree | |
583 | err_descr_tree = build_descr_tree(field_array); | |
584 | } | |
585 | ||
586 | ErroneousDescriptor* ErroneousAttributes::build_descr_tree(dynamic_array<field_err_t>& fld_array) | |
587 | { | |
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()); | |
601 | continue; | |
602 | } | |
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()); | |
607 | continue; | |
608 | } | |
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"); | |
619 | continue; | |
620 | } | |
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()); | |
627 | continue; | |
628 | } | |
629 | break; | |
630 | case Type::T_SEQ_T: | |
631 | case Type::T_SET_T: | |
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()); | |
637 | continue; | |
638 | } | |
639 | // no break | |
640 | case Type::T_SEQOF: | |
641 | case Type::T_SETOF: | |
642 | if (is_omit && (act_indicator==ErroneousAttributeSpec::I_BEFORE) && | |
643 | (fld_idx==0)) { | |
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()); | |
647 | continue; | |
648 | } | |
649 | break; | |
650 | default: | |
651 | break; | |
652 | } | |
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)); | |
662 | continue; | |
663 | } | |
664 | } | |
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()); | |
670 | continue; | |
671 | } | |
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()); | |
678 | continue; | |
679 | } | |
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) { | |
683 | is_invalid = true; | |
684 | break; | |
685 | } | |
686 | } | |
687 | if (!is_invalid) { | |
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) { | |
690 | is_invalid = true; | |
691 | break; | |
692 | } | |
693 | } | |
694 | } | |
695 | if (is_invalid) { | |
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()); | |
699 | continue; | |
700 | } | |
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(); | |
705 | continue; | |
706 | } | |
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()); | |
712 | continue; | |
713 | } | |
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) { | |
717 | is_invalid = true; | |
718 | break; | |
719 | } | |
720 | } | |
721 | if (!is_invalid) { | |
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) { | |
724 | is_invalid = true; | |
725 | break; | |
726 | } | |
727 | } | |
728 | } | |
729 | if (is_invalid) { | |
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()); | |
733 | continue; | |
734 | } | |
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(); | |
739 | continue; | |
740 | } | |
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()"); | |
749 | } | |
750 | if (!has_key) { | |
751 | err_descr->values_m.add(fld_idx, evs); | |
752 | } | |
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()); | |
758 | continue; | |
759 | } | |
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); | |
767 | if (!has_idx) { | |
768 | embedded_fld_array_m.add(fld_idx, emb_fld_array); | |
769 | } | |
770 | } | |
771 | } | |
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; | |
777 | } | |
778 | embedded_fld_array_m.clear(); | |
779 | return err_descr; | |
780 | } | |
781 | ||
782 | // ==== AttributeSpec ==== | |
783 | ||
784 | AttributeSpec* AttributeSpec::clone() const | |
785 | { | |
786 | return new AttributeSpec(*this); | |
787 | } | |
788 | ||
789 | void AttributeSpec::set_fullname(const string& p_fullname) | |
790 | { | |
791 | Node::set_fullname(p_fullname); | |
792 | } | |
793 | ||
794 | void AttributeSpec::dump(unsigned level) const | |
795 | { | |
796 | DEBUG(level,"spec: %s", spec.c_str()); | |
797 | } | |
798 | ||
799 | // ==== SingleWithAttrib ==== | |
800 | ||
801 | SingleWithAttrib::SingleWithAttrib(const SingleWithAttrib& p) | |
802 | : Node(p), Location(p), attribKeyword(p.attribKeyword), | |
803 | hasOverride(p.hasOverride) | |
804 | { | |
805 | attribQualifiers = p.attribQualifiers ? p.attribQualifiers->clone() : 0; | |
806 | attribSpec = p.attribSpec->clone(); | |
807 | } | |
808 | ||
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) | |
815 | { | |
816 | if(!p_attribSpec) | |
817 | FATAL_ERROR("SingleWithAttrib::SingleWithAttrib()"); | |
818 | } | |
819 | ||
820 | SingleWithAttrib::~SingleWithAttrib() | |
821 | { | |
822 | delete attribQualifiers; | |
823 | delete attribSpec; | |
824 | } | |
825 | ||
826 | SingleWithAttrib* SingleWithAttrib::clone() const | |
827 | { | |
828 | return new SingleWithAttrib(*this); | |
829 | } | |
830 | ||
831 | void SingleWithAttrib::set_fullname(const string& p_fullname) | |
832 | { | |
833 | Node::set_fullname(p_fullname); | |
834 | if(attribQualifiers) | |
835 | attribQualifiers->set_fullname(p_fullname + ".<attribute qualifiers>"); | |
836 | attribSpec->set_fullname(p_fullname + ".<attribute specification>"); | |
837 | } | |
838 | ||
839 | void SingleWithAttrib::dump(unsigned level) const | |
840 | { | |
841 | DEBUG(level,"attribute"); | |
842 | switch(attribKeyword) | |
843 | { | |
844 | case AT_ENCODE: | |
845 | DEBUG(level + 1,"keyword: encode"); | |
846 | break; | |
847 | case AT_VARIANT: | |
848 | DEBUG(level + 1,"keyword: variant"); | |
849 | break; | |
850 | case AT_DISPLAY: | |
851 | DEBUG(level + 1,"keyword: display"); | |
852 | break; | |
853 | case AT_EXTENSION: | |
854 | DEBUG(level + 1,"keyword: extension"); | |
855 | break; | |
856 | case AT_OPTIONAL: | |
857 | DEBUG(level + 1,"keyword: optional"); | |
858 | break; | |
859 | case AT_ERRONEOUS: | |
860 | DEBUG(level+1, "keyword: erroneous"); | |
861 | break; | |
862 | case AT_INVALID: | |
863 | DEBUG(level+1, "invalid keyword"); | |
864 | break; | |
865 | default: | |
866 | FATAL_ERROR("SingleWithAttrib::dump()"); | |
867 | } | |
868 | ||
869 | DEBUG(level + 1, hasOverride ? "has override" : "hasn't got override"); | |
870 | ||
871 | if(attribSpec) | |
872 | attribSpec->dump(level + 1); | |
873 | if(attribQualifiers) | |
874 | attribQualifiers->dump(level + 1); | |
875 | } | |
876 | ||
877 | // ==== MultiWithAttrib ==== | |
878 | ||
879 | MultiWithAttrib::MultiWithAttrib(const MultiWithAttrib& p) | |
880 | : Node(p), Location(p) | |
881 | { | |
882 | for(size_t i = 0; i < p.get_nof_elements(); i++) | |
883 | { | |
884 | elements.add(p.get_element(i)->clone()); | |
885 | } | |
886 | } | |
887 | ||
888 | MultiWithAttrib* MultiWithAttrib::clone() const | |
889 | { | |
890 | return new MultiWithAttrib(*this); | |
891 | } | |
892 | ||
893 | void MultiWithAttrib::set_fullname(const string& p_fullname) | |
894 | { | |
895 | Node::set_fullname(p_fullname); | |
896 | for(size_t i = 0; i < elements.size(); i++) | |
897 | { | |
898 | elements[i]->set_fullname(p_fullname + ".<singlewithattribute " | |
899 | + Int2string(i) + ">"); | |
900 | } | |
901 | } | |
902 | ||
903 | MultiWithAttrib::~MultiWithAttrib() | |
904 | { | |
905 | for(size_t i = 0; i < elements.size(); i++) | |
906 | { | |
907 | delete elements[i]; | |
908 | } | |
909 | elements.clear(); | |
910 | } | |
911 | ||
912 | const SingleWithAttrib* MultiWithAttrib::get_element(size_t p_index) const | |
913 | { | |
914 | return elements[p_index]; | |
915 | } | |
916 | ||
917 | SingleWithAttrib* MultiWithAttrib::get_element_for_modification( | |
918 | size_t p_index) | |
919 | { | |
920 | return elements[p_index]; | |
921 | } | |
922 | ||
923 | void MultiWithAttrib::delete_element(size_t p_index) | |
924 | { | |
925 | delete elements[p_index]; | |
926 | elements.replace(p_index,1,NULL); | |
927 | } | |
928 | ||
929 | void MultiWithAttrib::dump(unsigned level) const | |
930 | { | |
931 | DEBUG(level,"with attrib parameters (%lu pcs)", | |
932 | (unsigned long) elements.size()); | |
933 | for(size_t i = 0; i < elements.size(); i++) | |
934 | { | |
935 | elements[i]->dump(level + 1); | |
936 | } | |
937 | } | |
938 | ||
939 | // ==== WithAttribPath ==== | |
940 | ||
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) | |
944 | { | |
945 | m_w_attrib = p.m_w_attrib ? p.m_w_attrib->clone() : 0; | |
946 | } | |
947 | ||
948 | WithAttribPath::~WithAttribPath() | |
949 | { | |
950 | delete m_w_attrib; | |
951 | cache.clear(); | |
952 | } | |
953 | ||
954 | WithAttribPath* WithAttribPath::clone() const | |
955 | { | |
956 | return new WithAttribPath(*this); | |
957 | } | |
958 | ||
959 | void WithAttribPath::set_fullname(const string& p_fullname) | |
960 | { | |
961 | Node::set_fullname(p_fullname); | |
962 | if (m_w_attrib) m_w_attrib->set_fullname(p_fullname | |
963 | + ".<multiwithattribute>"); | |
964 | } | |
965 | ||
966 | void WithAttribPath::chk_no_qualif() | |
967 | { | |
968 | if(attributes_checked) | |
969 | return; | |
970 | ||
971 | if(!m_w_attrib) | |
972 | return; | |
973 | ||
974 | const SingleWithAttrib *swa; | |
975 | for(int i = m_w_attrib->get_nof_elements() - 1; i >= 0; i--) | |
976 | { | |
977 | swa = m_w_attrib->get_element(i); | |
978 | if( swa->get_attribQualifiers() | |
979 | && swa->get_attribQualifiers()->get_nof_qualifiers() != 0) | |
980 | { | |
981 | swa->error("field qualifiers are only allowed" | |
982 | " for record, set and union types"); | |
983 | m_w_attrib->delete_element(i); | |
984 | } | |
985 | } | |
986 | ||
987 | attributes_checked = true; | |
988 | } | |
989 | ||
990 | void WithAttribPath::dump(unsigned int level) const | |
991 | { | |
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); | |
998 | if (!a) continue; | |
999 | a->dump(level+1); | |
1000 | } | |
1001 | } | |
1002 | ||
1003 | /** | |
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. | |
1009 | */ | |
1010 | void WithAttribPath::chk_global_attrib(bool erroneous_allowed) | |
1011 | { | |
1012 | if(!m_w_attrib) | |
1013 | return; | |
1014 | ||
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"); | |
1022 | } | |
1023 | } | |
1024 | } | |
1025 | ||
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--) | |
1032 | { | |
1033 | const SingleWithAttrib* const temp_attrib = | |
1034 | m_w_attrib->get_element(i-1); | |
1035 | switch(temp_attrib->get_attribKeyword()) | |
1036 | { | |
1037 | case SingleWithAttrib::AT_ENCODE: | |
1038 | { | |
1039 | if(has_encode) | |
1040 | { | |
1041 | temp_attrib->warning("Only the last encode " | |
1042 | "of the with statement will have effect"); | |
1043 | m_w_attrib->delete_element(i-1); | |
1044 | }else{ | |
1045 | has_encode = true; | |
1046 | } | |
1047 | } | |
1048 | break; | |
1049 | case SingleWithAttrib::AT_ERRONEOUS: | |
1050 | { | |
1051 | if (temp_attrib->has_override()) { | |
1052 | temp_attrib->error("Override cannot be used with erroneous"); | |
1053 | } | |
1054 | } | |
1055 | break; | |
1056 | default: | |
1057 | break; | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | for(size_t i = 0; i < m_w_attrib->get_nof_elements();) | |
1062 | { | |
1063 | const SingleWithAttrib* const temp_attrib = m_w_attrib->get_element(i); | |
1064 | switch(temp_attrib->get_attribKeyword()) | |
1065 | { | |
1066 | case SingleWithAttrib::AT_VARIANT: | |
1067 | { | |
1068 | if(has_override_variant) | |
1069 | { | |
1070 | temp_attrib->warning("Only the first override" | |
1071 | " variant of the with statement will have effect"); | |
1072 | m_w_attrib->delete_element(i); | |
1073 | }else{ | |
1074 | if(temp_attrib->has_override()) | |
1075 | has_override_variant = true; | |
1076 | i++; | |
1077 | } | |
1078 | } | |
1079 | break; | |
1080 | case SingleWithAttrib::AT_DISPLAY: | |
1081 | { | |
1082 | if(has_override_display) | |
1083 | { | |
1084 | temp_attrib->warning("Only the first override" | |
1085 | " display of the with statement will have effect"); | |
1086 | m_w_attrib->delete_element(i); | |
1087 | }else{ | |
1088 | if(temp_attrib->has_override()) | |
1089 | has_override_display = true; | |
1090 | i++; | |
1091 | } | |
1092 | } | |
1093 | break; | |
1094 | case SingleWithAttrib::AT_EXTENSION: | |
1095 | { | |
1096 | if(has_override_extension) | |
1097 | { | |
1098 | temp_attrib->warning("Only the first override" | |
1099 | " extension of the with statement will have effect"); | |
1100 | m_w_attrib->delete_element(i); | |
1101 | }else{ | |
1102 | if(temp_attrib->has_override()) | |
1103 | has_override_extension = true; | |
1104 | i++; | |
1105 | } | |
1106 | } | |
1107 | break; | |
1108 | case SingleWithAttrib::AT_OPTIONAL: | |
1109 | { | |
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()); | |
1115 | } | |
1116 | if(has_override_optional) | |
1117 | { | |
1118 | temp_attrib->warning("Only the first override" | |
1119 | " optional of the with statement will have effect"); | |
1120 | m_w_attrib->delete_element(i); | |
1121 | }else{ | |
1122 | if(temp_attrib->has_override()) | |
1123 | has_override_optional = true; | |
1124 | i++; | |
1125 | } | |
1126 | } | |
1127 | break; | |
1128 | default: | |
1129 | i++; | |
1130 | break; | |
1131 | } // switch | |
1132 | } // next i | |
1133 | } | |
1134 | ||
1135 | void WithAttribPath::set_with_attr(MultiWithAttrib* p_m_w_attr) | |
1136 | { | |
1137 | if(m_w_attrib) FATAL_ERROR("WithAttribPath::set_with_attr()"); | |
1138 | m_w_attrib = p_m_w_attr; | |
1139 | attributes_checked = false; | |
1140 | } | |
1141 | ||
1142 | /** | |
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 | |
1148 | * our list. | |
1149 | */ | |
1150 | void WithAttribPath::qualifierless_attrib_finder( | |
1151 | vector<SingleWithAttrib>& p_result, | |
1152 | bool& stepped_over_encode) | |
1153 | { | |
1154 | if(cached) | |
1155 | { | |
1156 | for(size_t i = 0; i < cache.size(); i++) | |
1157 | { | |
1158 | p_result.add(cache[i]); | |
1159 | } | |
1160 | stepped_over_encode = s_o_encode; | |
1161 | return; | |
1162 | } | |
1163 | ||
1164 | if(parent) | |
1165 | parent->qualifierless_attrib_finder(p_result,stepped_over_encode); | |
1166 | else | |
1167 | stepped_over_encode = false; | |
1168 | ||
1169 | if(m_w_attrib) | |
1170 | { | |
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; | |
1186 | ||
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++) | |
1192 | { | |
1193 | act_single = m_w_attrib->get_element(i); | |
1194 | act_qualifiers = act_single->get_attribQualifiers(); | |
1195 | ||
1196 | //global attribute | |
1197 | if(!act_qualifiers || act_qualifiers->get_nof_qualifiers() == 0) | |
1198 | { | |
1199 | switch(act_single->get_attribKeyword()) | |
1200 | { | |
1201 | case SingleWithAttrib::AT_ENCODE: | |
1202 | self_encode_index = i; | |
1203 | break; | |
1204 | ||
1205 | case SingleWithAttrib::AT_VARIANT: { | |
1206 | // Ignore JSON variants, these should not produce warnings | |
1207 | const string& spec = act_single->get_attribSpec().get_spec(); | |
1208 | size_t i = 0; | |
1209 | while (i < spec.size()) { | |
1210 | if (spec[i] != ' ' && spec[i] != '\t') { | |
1211 | break; | |
1212 | } | |
1213 | ++i; | |
1214 | } | |
1215 | if (i == spec.size() || spec.find("JSON", i) != i) { | |
1216 | self_has_variant = true; | |
1217 | } | |
1218 | break; } | |
1219 | ||
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: | |
1225 | break; | |
1226 | ||
1227 | default: | |
1228 | FATAL_ERROR("WithAttribPath::attrib_finder()"); | |
1229 | } | |
1230 | } // if global | |
1231 | } // next i | |
1232 | ||
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++) | |
1237 | { | |
1238 | act_single = p_result[i]; | |
1239 | ||
1240 | switch(act_single->get_attribKeyword()) | |
1241 | { | |
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) | |
1246 | { | |
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()); | |
1251 | } | |
1252 | break; | |
1253 | ||
1254 | case SingleWithAttrib::AT_VARIANT: | |
1255 | par_has_override_variant |= act_single->has_override(); | |
1256 | break; | |
1257 | case SingleWithAttrib::AT_DISPLAY: | |
1258 | par_has_override_display |= act_single->has_override(); | |
1259 | break; | |
1260 | case SingleWithAttrib::AT_EXTENSION: | |
1261 | par_has_override_extension |= act_single->has_override(); | |
1262 | break; | |
1263 | case SingleWithAttrib::AT_OPTIONAL: | |
1264 | par_has_override_optional |= act_single->has_override(); | |
1265 | break; | |
1266 | case SingleWithAttrib::AT_ERRONEOUS: | |
1267 | case SingleWithAttrib::AT_INVALID: | |
1268 | // ignore | |
1269 | break; | |
1270 | default: | |
1271 | FATAL_ERROR("WithAttribPath::attrib_finder()"); | |
1272 | } | |
1273 | } | |
1274 | ||
1275 | if(!par_has_encode && self_encode_index == -1 && self_has_variant) | |
1276 | { | |
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++) | |
1280 | { | |
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"); | |
1284 | } | |
1285 | } | |
1286 | ||
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--) | |
1292 | { | |
1293 | switch(p_result[i-1]->get_attribKeyword()) | |
1294 | { | |
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); | |
1300 | break; | |
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: | |
1306 | break; | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | //adding the right ones from the local attributes | |
1311 | for(size_t i = 0; i < m_w_attrib_nof_elements; i++) | |
1312 | { | |
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) | |
1316 | { | |
1317 | switch(act_single->get_attribKeyword()) | |
1318 | { | |
1319 | case SingleWithAttrib::AT_ENCODE: | |
1320 | if((par_has_encode && !par_has_override_encode | |
1321 | && new_local_encode_context) | |
1322 | || (!par_has_encode)) | |
1323 | { | |
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; | |
1329 | }else{ | |
1330 | stepped_over_encode = false; | |
1331 | } | |
1332 | break; | |
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)); | |
1341 | break; | |
1342 | case SingleWithAttrib::AT_DISPLAY: | |
1343 | if(!par_has_override_display) | |
1344 | p_result.add(m_w_attrib->get_element_for_modification(i)); | |
1345 | break; | |
1346 | case SingleWithAttrib::AT_EXTENSION: | |
1347 | if(!par_has_override_extension) | |
1348 | p_result.add(m_w_attrib->get_element_for_modification(i)); | |
1349 | break; | |
1350 | case SingleWithAttrib::AT_OPTIONAL: | |
1351 | if (!par_has_override_optional) | |
1352 | p_result.add(m_w_attrib->get_element_for_modification(i)); | |
1353 | break; | |
1354 | case SingleWithAttrib::AT_ERRONEOUS: | |
1355 | case SingleWithAttrib::AT_INVALID: | |
1356 | // ignore | |
1357 | break; | |
1358 | } | |
1359 | } | |
1360 | } | |
1361 | } | |
1362 | } | |
1363 | ||
1364 | /* | |
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. | |
1370 | */ | |
1371 | const vector<SingleWithAttrib>& WithAttribPath::get_real_attrib() | |
1372 | { | |
1373 | if (!cached) { | |
1374 | qualifierless_attrib_finder(cache,s_o_encode); | |
1375 | cached = true; | |
1376 | } | |
1377 | return cache; | |
1378 | } | |
1379 | ||
1380 | bool WithAttribPath::has_attribs() | |
1381 | { | |
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; | |
1389 | } | |
1390 | } | |
1391 | return false; | |
1392 | } | |
1393 | ||
1394 | } |