Sync with 5.4.0
[deliverable/titan.core.git] / compiler2 / asn1 / Object.cc
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
8 #include "Object.hh"
9 #include "../Identifier.hh"
10 #include "Block.hh"
11 #include "TokenBuf.hh"
12 #include "../Type.hh"
13 #include "../Value.hh"
14 #include "OCSV.hh"
15 #include "../main.hh"
16
17 namespace Asn {
18
19 // =================================
20 // ===== ObjectClass
21 // =================================
22
23 // =================================
24 // ===== OC_defn
25 // =================================
26
27 OC_defn::OC_defn()
28 : ObjectClass(), block_fs(0), block_ws(0), fss(0),
29 ocs(0), is_erroneous(true), is_generated(false)
30 {
31 }
32
33 OC_defn::OC_defn(Block *p_block_fs, Block *p_block_ws)
34 : ObjectClass(), block_fs(p_block_fs), block_ws(p_block_ws), fss(0),
35 ocs(0), is_erroneous(false), is_generated(false)
36 {
37 if(!p_block_fs)
38 FATAL_ERROR("NULL parameter: Asn::OC_defn::OC_defn()");
39 }
40
41 OC_defn::OC_defn(const OC_defn& p)
42 : ObjectClass(p), is_erroneous(p.is_erroneous),
43 is_generated(false)
44 {
45 block_fs=p.block_fs?p.block_fs->clone():0;
46 block_ws=p.block_ws?p.block_ws->clone():0;
47 fss=p.fss?p.fss->clone():0;
48 if(fss) fss->set_my_oc(this);
49 /* warning: this is not implemented */
50 // ocs=p.ocs?p.ocs->clone():0;
51 ocs=0;
52 }
53
54 OC_defn::~OC_defn()
55 {
56 delete block_fs;
57 delete block_ws;
58 delete fss;
59 delete ocs;
60 }
61
62 void OC_defn::set_fullname(const string& p_fullname)
63 {
64 ObjectClass::set_fullname(p_fullname);
65 if(fss) fss->set_fullname(p_fullname);
66 }
67
68 void OC_defn::chk()
69 {
70 if(checked) return;
71 checked=true;
72 parse_block_fs();
73 if(!ocs) {
74 TokenBuf *t_tb=block_ws?block_ws->get_TokenBuf():0;
75 ocs=new OCS_root();
76 Error_Context ec(this, "In syntax of ObjectClass `%s'",
77 this->get_fullname().c_str());
78 OCSV_Builder *ocsv_builder=new OCSV_Builder(t_tb, fss);
79 ocs->accept(*ocsv_builder);
80 delete ocsv_builder;
81 delete block_ws; block_ws=0;
82 }
83 fss->chk();
84 }
85
86 void OC_defn::chk_this_obj(Object *p_obj)
87 {
88 if(!p_obj)
89 FATAL_ERROR("NULL parameter: Asn::OC_defn::chk_this_obj()");
90 if(is_erroneous) return;
91 Obj_defn *obj=p_obj->get_refd_last();
92 Error_Context ec(obj, "In object definition `%s'",
93 obj->get_fullname().c_str());
94 for(size_t i=0; i<fss->get_nof_fss(); i++) {
95 FieldSpec *fspec=fss->get_fs_byIndex(i)->get_last();
96 if(!obj->has_fs_withName(fspec->get_id())) {
97 if(!(fspec->get_is_optional() || fspec->has_default())
98 && !(obj->get_is_erroneous())) {
99 obj->error("Missing setting for `%s'",
100 fspec->get_id().get_dispname().c_str());
101 obj->set_is_erroneous();
102 }
103 continue;
104 }
105 FieldSetting *fset=obj->get_fs_byName(fspec->get_id());
106 if(!fset) continue;
107 Error_Context ec2(fset, "In setting for `%s'",
108 fspec->get_id().get_dispname().c_str());
109 fset->set_genname(obj->get_genname_own(), fspec->get_id().get_name());
110 fset->chk(fspec);
111 } // for i
112 }
113
114 void OC_defn::parse_block_fs()
115 {
116 if(fss) return;
117 if(!block_fs) {
118 if(!is_erroneous) FATAL_ERROR("Asn::OC_defn::parse_block_fs()");
119 }
120 else {
121 Node *node=block_fs->parse(KW_Block_FieldSpecList);
122 delete block_fs; block_fs=0;
123 fss=dynamic_cast<FieldSpecs*>(node);
124 }
125 if(!fss) fss=new FieldSpecs();
126 fss->set_fullname(get_fullname());
127 fss->set_my_oc(this);
128 }
129
130 OCS_root* OC_defn::get_ocs()
131 {
132 chk();
133 return ocs;
134 }
135
136 FieldSpecs* OC_defn::get_fss()
137 {
138 chk();
139 return fss;
140 }
141
142 void OC_defn::generate_code(output_struct *target)
143 {
144 if(is_generated) return;
145 is_generated=true;
146 fss->generate_code(target);
147 }
148
149 void OC_defn::dump(unsigned level) const
150 {
151 DEBUG(level, "ObjectClass definition");
152 if(block_fs)
153 DEBUG(level, "with unparsed block");
154 if(fss) {
155 DEBUG(level, "with fieldspecs (%lu pcs):",
156 (unsigned long) fss->get_nof_fss());
157 fss->dump(level+1);
158 }
159 if(ocs) {
160 DEBUG(level, "with syntax definition:");
161 ocs->dump(level+1);
162 }
163 }
164
165 // =================================
166 // ===== OC_refd
167 // =================================
168
169 OC_refd::OC_refd(Reference *p_ref)
170 : ObjectClass(), ref(p_ref), oc_error(0), oc_refd(0), oc_defn(0)
171 {
172 if(!p_ref)
173 FATAL_ERROR("NULL parameter: Asn::OC_refd::OC_refd()");
174 }
175
176 OC_refd::OC_refd(const OC_refd& p)
177 : ObjectClass(p), oc_error(0), oc_refd(0), oc_defn(0)
178 {
179 ref=p.ref->clone();
180 }
181
182 OC_refd::~OC_refd()
183 {
184 delete ref;
185 delete oc_error;
186 }
187
188 void OC_refd::set_my_scope(Scope *p_scope)
189 {
190 ObjectClass::set_my_scope(p_scope);
191 ref->set_my_scope(p_scope);
192 }
193
194 void OC_refd::set_fullname(const string& p_fullname)
195 {
196 ObjectClass::set_fullname(p_fullname);
197 ref->set_fullname(p_fullname);
198 }
199
200 OC_defn* OC_refd::get_refd_last(ReferenceChain *refch)
201 {
202 if(!oc_defn) {
203 bool destroy_refch=false;
204 if(!refch) {
205 refch=new ReferenceChain
206 (ref, "While searching referenced ObjectClass");
207 destroy_refch=true;
208 }
209 oc_defn=get_refd(refch)->get_refd_last(refch);
210 if(destroy_refch) delete refch;
211 }
212 return oc_defn;
213 }
214
215 ObjectClass* OC_refd::get_refd(ReferenceChain *refch)
216 {
217 if(refch) refch->add(get_fullname());
218 if(!oc_refd) {
219 Setting *setting=0;
220 Common::Assignment *ass=ref->get_refd_assignment();
221 if(!ass) goto error;
222 setting=ref->get_refd_setting();
223 if(setting->get_st()==Setting::S_ERROR) goto error;
224 oc_refd=dynamic_cast<ObjectClass*>(setting);
225 if(!oc_refd) {
226 error("This is not an objectclassreference: `%s'",
227 ref->get_dispname().c_str());
228 goto error;
229 }
230 goto end;
231 error:
232 oc_error=new OC_defn();
233 oc_refd=oc_error;
234 }
235 end:
236 return oc_refd;
237 }
238
239 void OC_refd::chk()
240 {
241 if(checked) return;
242 checked=true;
243 get_refd_last();
244 }
245
246 void OC_refd::chk_this_obj(Object *obj)
247 {
248 get_refd_last()->chk_this_obj(obj);
249 }
250
251 OCS_root* OC_refd::get_ocs()
252 {
253 chk();
254 return get_refd_last()->get_ocs();
255 }
256
257 FieldSpecs* OC_refd::get_fss()
258 {
259 chk();
260 return get_refd_last()->get_fss();
261 }
262
263 void OC_refd::generate_code(output_struct *target)
264 {
265 OC_defn *refd_last = get_refd_last();
266 if(my_scope->get_scope_mod_gen() ==
267 refd_last->get_my_scope()->get_scope_mod_gen())
268 refd_last->generate_code(target);
269 }
270
271 void OC_refd::dump(unsigned level) const
272 {
273 DEBUG(level, "objectclassreference");
274 level++;
275 ref->dump(level);
276 }
277
278 // =================================
279 // ===== FieldSpec
280 // =================================
281
282 FieldSpec::FieldSpec(fstype_t p_fstype, Identifier *p_id,
283 bool p_is_optional=false)
284 : Node(), Location(), fstype(p_fstype), id(p_id),
285 is_optional(p_is_optional), my_oc(0), checked(false)
286 {
287 if(!p_id)
288 FATAL_ERROR("NULL parameter: Asn::FieldSpec::FieldSpec()");
289 }
290
291 FieldSpec::FieldSpec(const FieldSpec& p)
292 : Node(p), Location(p), fstype(p.fstype), is_optional(p.is_optional),
293 my_oc(0), checked(false)
294 {
295 id=p.id->clone();
296 }
297
298 FieldSpec::~FieldSpec()
299 {
300 delete id;
301 }
302
303 void FieldSpec::set_fullname(const string& p_fullname)
304 {
305 Node::set_fullname(p_fullname);
306 }
307
308 void FieldSpec::set_my_oc(OC_defn *p_oc)
309 {
310 my_oc=p_oc;
311 }
312
313 // =================================
314 // ===== FieldSpec_Undef
315 // =================================
316
317 FieldSpec_Undef::FieldSpec_Undef(Identifier *p_id, Ref_defd *p_govref,
318 bool p_is_optional, Node *p_defsetting)
319 : FieldSpec(FS_UNDEF, p_id, p_is_optional),
320 govref(p_govref), defsetting(p_defsetting), fs(0)
321 {
322 if(!p_govref)
323 FATAL_ERROR("FieldSpec_Undef::FieldSpec_Undef()");
324 }
325
326 FieldSpec_Undef::FieldSpec_Undef(const FieldSpec_Undef& p)
327 : FieldSpec(p)
328 {
329 govref=p.govref?p.govref->clone():0;
330 defsetting=p.defsetting?p.defsetting->clone():0;
331 fs=p.fs?p.fs->clone():0;
332 }
333
334 FieldSpec_Undef::~FieldSpec_Undef()
335 {
336 delete govref;
337 delete defsetting;
338 if(fs) delete fs;
339 }
340
341 /** \todo Enhance! (FS_VS_FT + refch!) */
342 void FieldSpec_Undef::classify_fs(ReferenceChain *refch)
343 {
344 if(fstype!=FS_UNDEF) return;
345 bool destroy_refch=false;
346 if(!refch) {
347 refch=new ReferenceChain(this, "While examining kind of fieldspec");
348 destroy_refch=true;
349 }
350 else refch->mark_state();
351
352 if(is_optional && defsetting) {
353 error("OPTIONAL and DEFAULT are mutual exclusive");
354 is_optional=false;
355 }
356
357 /* temporary pointers */
358 Reference *t_ref=0;
359 Reference *t_ref2=0;
360 Block *t_block=0;
361
362 if(!refch->add(get_fullname()))
363 goto error;
364
365 if((t_ref=dynamic_cast<Reference*>(govref))) {
366 t_ref->set_my_scope(my_oc->get_my_scope());
367 if(t_ref->refers_to_st(Setting::S_ERROR, refch))
368 goto error;
369 }
370 if((t_ref=dynamic_cast<Reference*>(defsetting)))
371 t_ref->set_my_scope(my_oc->get_my_scope());
372 govref->set_fullname(get_fullname());
373 if(defsetting) defsetting->set_fullname(get_fullname());
374
375 if(id->isvalid_asn_objsetfieldref()
376 && (t_ref=dynamic_cast<Ref_defd_simple*>(govref))
377 && t_ref->refers_to_st(Setting::S_OC, refch)
378 ) {
379 ObjectSet *defobjset=0;
380 if(defsetting) {
381 if((t_block=dynamic_cast<Block*>(defsetting))) {
382 defobjset=new OS_defn(t_block);
383 }
384 else {
385 error("Cannot recognize DEFAULT setting"
386 " for this ObjectSetFieldSpec");
387 delete defobjset;
388 defobjset=new OS_defn();
389 }
390 } // if defsetting
391 fs=new FieldSpec_OS(id->clone(), new OC_refd(t_ref),
392 is_optional, defobjset);
393 govref=0;
394 defsetting=0;
395 fstype=FS_OS;
396 }
397 else if(id->isvalid_asn_objfieldref()
398 && (t_ref=dynamic_cast<Ref_defd_simple*>(govref))
399 && t_ref->refers_to_st(Setting::S_OC, refch)
400 ) {
401 Object *defobj=0;
402 if(defsetting) {
403 if((t_block=dynamic_cast<Block*>(defsetting))) {
404 defobj=new Obj_defn(t_block);
405 }
406 else if((t_ref2=dynamic_cast<Reference*>(defsetting))) {
407 defobj=new Obj_refd(t_ref2);
408 }
409 else {
410 error("Cannot recognize DEFAULT setting"
411 " for this ObjectFieldSpec");
412 delete defobj;
413 defobj=new Obj_defn();
414 }
415 } // if defsetting
416 fs=new FieldSpec_O(id->clone(), new OC_refd(t_ref),
417 is_optional, defobj);
418 govref=0;
419 defsetting=0;
420 fstype=FS_O;
421 }
422 else if(id->isvalid_asn_valfieldref()
423 && (t_ref=dynamic_cast<Ref_defd*>(govref))
424 && (t_ref->refers_to_st(Setting::S_T, refch)
425 || t_ref->refers_to_st(Setting::S_VS, refch))
426 ) {
427 Value *defval=0;
428 if(defsetting) {
429 if((t_block=dynamic_cast<Block*>(defsetting))) {
430 defval=new Value(Value::V_UNDEF_BLOCK, t_block);
431 }
432 else if((t_ref2=dynamic_cast<Reference*>(defsetting))) {
433 Ref_defd_simple *t_ref3=dynamic_cast<Ref_defd_simple*>(t_ref2);
434 if(t_ref3 && !t_ref3->get_modid()) {
435 defval=new Value(Value::V_UNDEF_LOWERID,
436 t_ref3->get_id()->clone());
437 delete t_ref3;
438 }
439 else {
440 defval=new Value(Value::V_REFD, t_ref2);
441 }
442 }
443 else {
444 error("Cannot recognize DEFAULT setting"
445 " for this FixedTypeValueFieldSpec");
446 delete defsetting;
447 defval=new Value(Value::V_ERROR);
448 }
449 } // if defsetting
450 fs=new FieldSpec_V_FT(id->clone(), new Type(Type::T_REFD, t_ref),
451 false, is_optional, defval);
452 govref=0;
453 defsetting=0;
454 fstype=FS_V_FT;
455 }
456 else {
457 goto error;
458 }
459 goto end;
460 error:
461 fstype=FS_ERROR;
462 error("Cannot recognize fieldspec");
463 fs=new FieldSpec_Error(id->clone(), is_optional, defsetting!=0);
464 delete defsetting; defsetting=0;
465 end:
466 fs->set_fullname(get_fullname());
467 if(my_oc) fs->set_my_oc(my_oc);
468 fs->set_location(*this);
469 if(destroy_refch) delete refch;
470 else refch->prev_state();
471 }
472
473 FieldSpec* FieldSpec_Undef::clone() const
474 {
475 if(fs) return fs->clone();
476 else return new FieldSpec_Undef(*this);
477 }
478
479 void FieldSpec_Undef::set_fullname(const string& p_fullname)
480 {
481 FieldSpec::set_fullname(p_fullname);
482 if(fs) fs->set_fullname(p_fullname);
483 }
484
485 FieldSpec::fstype_t FieldSpec_Undef::get_fstype()
486 {
487 classify_fs();
488 return fstype;
489 }
490
491 void FieldSpec_Undef::set_my_oc(OC_defn *p_oc)
492 {
493 FieldSpec::set_my_oc(p_oc);
494 if(fs) fs->set_my_oc(p_oc);
495 }
496
497 bool FieldSpec_Undef::has_default()
498 {
499 if(fs) return fs->has_default();
500 else return defsetting;
501 }
502
503 void FieldSpec_Undef::chk()
504 {
505 if(checked) return;
506 classify_fs();
507 fs->chk();
508 checked=true;
509 }
510
511 Setting* FieldSpec_Undef::get_default()
512 {
513 classify_fs();
514 return fs->get_default();
515 }
516
517 FieldSpec* FieldSpec_Undef::get_last()
518 {
519 classify_fs();
520 return fs->get_last();
521 }
522
523 void FieldSpec_Undef::generate_code(output_struct *target)
524 {
525 classify_fs();
526 fs->generate_code(target);
527 }
528
529 // =================================
530 // ===== FieldSpec_Error
531 // =================================
532
533 FieldSpec_Error::FieldSpec_Error(Identifier *p_id, bool p_is_optional,
534 bool p_has_default)
535 : FieldSpec(FS_ERROR, p_id, p_is_optional), setting_error(0),
536 has_default_flag(p_has_default)
537 {
538 }
539
540 FieldSpec_Error::FieldSpec_Error(const FieldSpec_Error& p)
541 : FieldSpec(p), setting_error(0), has_default_flag(p.has_default_flag)
542 {
543 }
544
545 FieldSpec_Error::~FieldSpec_Error()
546 {
547 delete setting_error;
548 }
549
550 FieldSpec_Error *FieldSpec_Error::clone() const
551 {
552 return new FieldSpec_Error(*this);
553 }
554
555 bool FieldSpec_Error::has_default()
556 {
557 return has_default_flag;
558 }
559
560 Setting* FieldSpec_Error::get_default()
561 {
562 if(!setting_error && has_default_flag)
563 setting_error=new Common::Setting_Error();
564 return setting_error;
565 }
566
567 void FieldSpec_Error::chk()
568 {
569
570 }
571
572 void FieldSpec_Error::generate_code(output_struct *)
573 {
574
575 }
576
577 // =================================
578 // ===== FieldSpec_T
579 // =================================
580
581 FieldSpec_T::FieldSpec_T(Identifier *p_id, bool p_is_optional,
582 Type *p_deftype)
583 : FieldSpec(FS_T, p_id, p_is_optional), deftype(p_deftype)
584 {
585 if(is_optional && deftype) {
586 error("OPTIONAL and DEFAULT are mutual exclusive");
587 is_optional=false;
588 }
589 if (deftype) deftype->set_ownertype(Type::OT_TYPE_FLD, this);
590 }
591
592 FieldSpec_T::FieldSpec_T(const FieldSpec_T& p)
593 : FieldSpec(p)
594 {
595 deftype=p.deftype?p.deftype->clone():0;
596 }
597
598 FieldSpec_T::~FieldSpec_T()
599 {
600 delete deftype;
601 }
602
603 void FieldSpec_T::set_fullname(const string& p_fullname)
604 {
605 FieldSpec::set_fullname(p_fullname);
606 if(deftype) deftype->set_fullname(p_fullname);
607 }
608
609 void FieldSpec_T::set_my_oc(OC_defn *p_oc)
610 {
611 FieldSpec::set_my_oc(p_oc);
612 if(deftype) deftype->set_my_scope(my_oc->get_my_scope());
613 }
614
615 Type* FieldSpec_T::get_default()
616 {
617 return deftype;
618 }
619
620 void FieldSpec_T::chk()
621 {
622 if(checked) return;
623 if(deftype) {
624 deftype->set_genname(my_oc->get_genname_own(), id->get_name());
625 deftype->chk();
626 }
627 checked=true;
628 }
629
630 void FieldSpec_T::generate_code(output_struct *target)
631 {
632 if (deftype) deftype->generate_code(target);
633 }
634
635 // =================================
636 // ===== FieldSpec_V_FT
637 // =================================
638
639 FieldSpec_V_FT::FieldSpec_V_FT(Identifier *p_id, Type *p_fixtype,
640 bool p_is_unique, bool p_is_optional,
641 Value *p_defval)
642 : FieldSpec(FS_V_FT, p_id, p_is_optional),
643 fixtype(p_fixtype), is_unique(p_is_unique), defval(p_defval)
644 {
645 if(!p_fixtype)
646 FATAL_ERROR("NULL parameter: FieldSpec_V_FT::FieldSpec_V_FT()");
647 fixtype->set_ownertype(Type::OT_FT_V_FLD, this);
648 if(is_optional && defval) {
649 error("OPTIONAL and DEFAULT are mutual exclusive");
650 is_optional=false;
651 }
652 if(is_unique && defval)
653 error("UNIQUE and DEFAULT are mutual exclusive");
654 }
655
656 FieldSpec_V_FT::FieldSpec_V_FT(const FieldSpec_V_FT& p)
657 : FieldSpec(p), is_unique(p.is_unique)
658 {
659 fixtype=p.fixtype->clone();
660 defval=p.defval?p.defval->clone():0;
661 }
662
663 FieldSpec_V_FT::~FieldSpec_V_FT()
664 {
665 delete fixtype;
666 delete defval;
667 }
668
669 void FieldSpec_V_FT::set_fullname(const string& p_fullname)
670 {
671 FieldSpec::set_fullname(p_fullname);
672 fixtype->set_fullname(p_fullname);
673 if(defval) defval->set_fullname(p_fullname);
674 }
675
676 void FieldSpec_V_FT::set_my_oc(OC_defn *p_oc)
677 {
678 FieldSpec::set_my_oc(p_oc);
679 Scope *scope=my_oc->get_my_scope();
680 fixtype->set_my_scope(scope);
681 if(defval) defval->set_my_scope(scope);
682 }
683
684 Value* FieldSpec_V_FT::get_default()
685 {
686 return defval;
687 }
688
689 void FieldSpec_V_FT::chk()
690 {
691 if(checked) return;
692 fixtype->set_genname(my_oc->get_genname_own(), id->get_name());
693 fixtype->chk();
694 if(defval) {
695 defval->set_my_governor(fixtype);
696 fixtype->chk_this_value_ref(defval);
697 fixtype->chk_this_value(defval, 0, Type::EXPECTED_CONSTANT,
698 INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, SUB_CHK);
699 if (!semantic_check_only) {
700 defval->set_genname_prefix("const_");
701 defval->set_genname_recursive(string(fixtype->get_genname_own()) +
702 "_defval_");
703 defval->set_code_section(GovernedSimple::CS_PRE_INIT);
704 }
705 }
706 checked=true;
707 }
708
709 void FieldSpec_V_FT::generate_code(output_struct *target)
710 {
711 fixtype->generate_code(target);
712 if(defval) {
713 const_def cdef;
714 Code::init_cdef(&cdef);
715 fixtype->generate_code_object(&cdef, defval);
716 cdef.init = defval->generate_code_init(cdef.init,
717 defval->get_lhs_name().c_str());
718 Code::merge_cdef(target, &cdef);
719 Code::free_cdef(&cdef);
720 }
721 }
722
723 // =================================
724 // ===== FieldSpec_V_VT
725 // =================================
726
727 // =================================
728 // ===== FieldSpec_VS_FT
729 // =================================
730
731 // =================================
732 // ===== FieldSpec_VS_VT
733 // =================================
734
735 // =================================
736 // ===== FieldSpec_O
737 // =================================
738
739 FieldSpec_O::FieldSpec_O(Identifier *p_id, ObjectClass *p_oc,
740 bool p_is_optional, Object *p_defobj)
741 : FieldSpec(FS_O, p_id, p_is_optional),
742 oc(p_oc), defobj(p_defobj)
743 {
744 if(!p_oc)
745 FATAL_ERROR("NULL parameter: FieldSpec_O::FieldSpec_O()");
746 if(is_optional && defobj) {
747 error("OPTIONAL and DEFAULT are mutual exclusive");
748 is_optional=false;
749 }
750 }
751
752 FieldSpec_O::FieldSpec_O(const FieldSpec_O& p)
753 : FieldSpec(p)
754 {
755 oc=p.oc->clone();
756 defobj=p.defobj?p.defobj->clone():0;
757 }
758
759 FieldSpec_O::~FieldSpec_O()
760 {
761 delete oc;
762 delete defobj;
763 }
764
765 void FieldSpec_O::set_fullname(const string& p_fullname)
766 {
767 FieldSpec::set_fullname(p_fullname);
768 oc->set_fullname(p_fullname);
769 if(defobj) defobj->set_fullname(p_fullname);
770 }
771
772 void FieldSpec_O::set_my_oc(OC_defn *p_oc)
773 {
774 FieldSpec::set_my_oc(p_oc);
775 Scope *scope=my_oc->get_my_scope();
776 oc->set_my_scope(scope);
777 if(defobj) defobj->set_my_scope(scope);
778 }
779
780 void FieldSpec_O::chk()
781 {
782 if(checked) return;
783 oc->set_genname(my_oc->get_genname_own(), id->get_name());
784 oc->chk();
785 if(defobj) {
786 defobj->set_my_governor(oc);
787 defobj->set_genname(oc->get_genname_own(), string("_defobj_"));
788 defobj->chk();
789 }
790 checked=true;
791 }
792
793 void FieldSpec_O::generate_code(output_struct *target)
794 {
795 oc->generate_code(target);
796 if (defobj) defobj->generate_code(target);
797 }
798
799 // =================================
800 // ===== FieldSpec_OS
801 // =================================
802
803 FieldSpec_OS::FieldSpec_OS(Identifier *p_id, ObjectClass *p_oc,
804 bool p_is_optional, ObjectSet *p_defobjset)
805 : FieldSpec(FS_OS, p_id, p_is_optional),
806 oc(p_oc), defobjset(p_defobjset)
807 {
808 if(!p_oc)
809 FATAL_ERROR("NULL parameter: FieldSpec_OS::FieldSpec_OS()");
810 if(is_optional && defobjset) {
811 error("OPTIONAL and DEFAULT are mutual exclusive");
812 is_optional=false;
813 }
814 }
815
816 FieldSpec_OS::FieldSpec_OS(const FieldSpec_OS& p)
817 : FieldSpec(p)
818 {
819 oc=p.oc->clone();
820 defobjset=p.defobjset?p.defobjset->clone():0;
821 }
822
823 FieldSpec_OS::~FieldSpec_OS()
824 {
825 delete oc;
826 delete defobjset;
827 }
828
829 void FieldSpec_OS::set_fullname(const string& p_fullname)
830 {
831 FieldSpec::set_fullname(p_fullname);
832 oc->set_fullname(p_fullname);
833 if(defobjset) defobjset->set_fullname(p_fullname);
834 }
835
836 void FieldSpec_OS::set_my_oc(OC_defn *p_oc)
837 {
838 FieldSpec::set_my_oc(p_oc);
839 Scope *scope=my_oc->get_my_scope();
840 oc->set_my_scope(scope);
841 if(defobjset) defobjset->set_my_scope(scope);
842 }
843
844 void FieldSpec_OS::chk()
845 {
846 if(checked) return;
847 oc->set_genname(my_oc->get_genname_own(), id->get_name());
848 oc->chk();
849 if(defobjset) {
850 defobjset->set_my_governor(oc);
851 defobjset->set_genname(oc->get_genname_own(), string("_defobj_"));
852 defobjset->chk();
853 }
854 checked=true;
855 }
856
857 void FieldSpec_OS::generate_code(output_struct *target)
858 {
859 oc->generate_code(target);
860 if (defobjset) defobjset->generate_code(target);
861 }
862
863 // =================================
864 // ===== FieldSpecs
865 // =================================
866
867 FieldSpecs::FieldSpecs()
868 : Node(), fs_error(0), my_oc(0)
869 {
870 }
871
872 FieldSpecs::FieldSpecs(const FieldSpecs& p)
873 : Node(p), fs_error(0), my_oc(0)
874 {
875 for(size_t i=0; i<p.fss_v.size(); i++)
876 add_fs(p.fss_v[i]->clone());
877 }
878
879 FieldSpecs::~FieldSpecs()
880 {
881 for(size_t i=0; i<fss_v.size(); i++)
882 delete fss_v[i];
883 fss.clear();
884 fss_v.clear();
885 delete fs_error;
886 }
887
888 void FieldSpecs::set_fullname(const string& p_fullname)
889 {
890 Node::set_fullname(p_fullname);
891 for(size_t i=0; i<fss_v.size(); i++) {
892 FieldSpec *fs=fss_v[i];
893 fs->set_fullname(p_fullname+"."+fs->get_id().get_dispname());
894 }
895 }
896
897 void FieldSpecs::set_my_oc(OC_defn *p_oc)
898 {
899 my_oc=p_oc;
900 for(size_t i=0; i<fss_v.size(); i++)
901 fss_v[i]->set_my_oc(p_oc);
902 }
903
904 void FieldSpecs::add_fs(FieldSpec *p_fs)
905 {
906 if(!p_fs)
907 FATAL_ERROR("NULL parameter: Asn::FieldSpecs::add_fs()");
908 if(fss.has_key(p_fs->get_id().get_name())) {
909 p_fs->error("A fieldspec with identifier `%s' already exists",
910 p_fs->get_id().get_dispname().c_str());
911 delete p_fs;
912 return;
913 }
914 fss.add(p_fs->get_id().get_name(), p_fs);
915 fss_v.add(p_fs);
916 p_fs->set_fullname(get_fullname()+"."+p_fs->get_id().get_dispname());
917 if(my_oc) p_fs->set_my_oc(my_oc);
918 }
919
920 bool FieldSpecs::has_fs_withId(const Identifier& p_id)
921 {
922 return fss.has_key(p_id.get_name());
923 }
924
925 FieldSpec* FieldSpecs::get_fs_byId(const Identifier& p_id)
926 {
927 if(fss.has_key(p_id.get_name()))
928 return fss[p_id.get_name()];
929 my_oc->error("No fieldspec with name `%s'", p_id.get_dispname().c_str());
930 return get_fs_error();
931 }
932
933 FieldSpec* FieldSpecs::get_fs_byIndex(size_t p_i)
934 {
935 if(p_i <fss_v.size())
936 return fss_v[p_i];
937 my_oc->error("No fieldspec at index %s", Int2string(p_i).c_str());
938 return get_fs_error();
939 }
940
941 FieldSpec* FieldSpecs::get_fs_error()
942 {
943 if(!fs_error)
944 fs_error=new FieldSpec_Error
945 (new Identifier(Identifier::ID_NAME, string("<error>")), true, false);
946 return fs_error;
947 }
948
949 void FieldSpecs::chk()
950 {
951 for(size_t i=0; i<fss_v.size(); i++) {
952 FieldSpec *fs=fss_v[i];
953 Error_Context ec(fs, "In fieldspec `%s'",
954 fs->get_id().get_dispname().c_str());
955 fs->chk();
956 }
957 }
958
959 void FieldSpecs::generate_code(output_struct *target)
960 {
961 for(size_t i=0; i<fss_v.size(); i++)
962 fss_v[i]->generate_code(target);
963 }
964
965 void FieldSpecs::dump(unsigned level) const
966 {
967 DEBUG(level, "Fieldspecs (%lu pcs.)", (unsigned long) fss.size());
968 level++;
969 for(size_t i=0; i<fss_v.size(); i++)
970 fss_v[i]->dump(level);
971 }
972
973 // =================================
974 // ===== OCS_Visitor
975 // =================================
976
977 OCS_Visitor* OCS_Visitor::clone() const
978 {
979 FATAL_ERROR("Asn::OCS_Visitor::clone()");
980 return 0;
981 }
982
983 // =================================
984 // ===== OCS_Node
985 // =================================
986
987 OCS_Node* OCS_Node::clone() const
988 {
989 FATAL_ERROR("Asn::OCS_Node::clone()");
990 return 0;
991 }
992
993 void OCS_Node::dump(unsigned level) const
994 {
995 DEBUG(level, "OCS_Node");
996 }
997
998 // =================================
999 // ===== OCS_seq
1000 // =================================
1001
1002 OCS_seq::~OCS_seq()
1003 {
1004 for(size_t i=0; i<ocss.size(); i++)
1005 delete ocss[i];
1006 ocss.clear();
1007 }
1008
1009 void OCS_seq::add_node(OCS_Node *p_node)
1010 {
1011 if(!p_node)
1012 FATAL_ERROR("NULL parameter: Asn::OCS_seq::add_node()");
1013 ocss.add(p_node);
1014 }
1015
1016 OCS_Node* OCS_seq::get_nth_node(size_t p_i)
1017 {
1018 if(ocss.size()<=p_i)
1019 FATAL_ERROR("Asn::OCS_seq::get_nth_node()");
1020 return ocss[p_i];
1021 }
1022
1023 string OCS_seq::get_dispname() const
1024 {
1025 string s;
1026 if(is_opt) s="[";
1027 for(size_t i=0; i<ocss.size(); i++) {
1028 if(i) s+=" ";
1029 s+=ocss[i]->get_dispname();
1030 }
1031 if(is_opt) s+="]";
1032 return s;
1033 }
1034
1035 void OCS_seq::dump(unsigned level) const
1036 {
1037 DEBUG(level, "%soptional sequence of tokens:", is_opt?"":"not ");
1038 level++;
1039 if(opt_first_comma)
1040 DEBUG(level, "[,]");
1041 for(size_t i=0; i<ocss.size(); i++)
1042 ocss[i]->dump(level);
1043 }
1044
1045 // =================================
1046 // ===== OCS_root
1047 // =================================
1048
1049 string OCS_root::get_dispname() const
1050 {
1051 return seq.get_dispname();
1052 }
1053
1054 void OCS_root::dump(unsigned level) const
1055 {
1056 seq.dump(level);
1057 }
1058
1059 // =================================
1060 // ===== OCS_literal
1061 // =================================
1062
1063 OCS_literal::OCS_literal(Identifier *p_word)
1064 : OCS_Node(), word(p_word)
1065 {
1066 if(!p_word)
1067 FATAL_ERROR("NULL parameter: Asn::OCS_literal::OCS_literal()");
1068 }
1069
1070 OCS_literal::~OCS_literal()
1071 {
1072 delete word;
1073 }
1074
1075 string OCS_literal::get_dispname() const
1076 {
1077 string s('`');
1078 if(word) s+=word->get_dispname().c_str();
1079 else s+=Token::get_token_name(keyword);
1080 s+='\'';
1081 return s;
1082 }
1083
1084 void OCS_literal::dump(unsigned level) const
1085 {
1086 DEBUG(level, "literal: %s", get_dispname().c_str());
1087 }
1088
1089 // =================================
1090 // ===== OCS_setting
1091 // =================================
1092
1093 OCS_setting::OCS_setting(settingtype_t p_st, Identifier *p_id)
1094 : OCS_Node(), st(p_st), id(p_id)
1095 {
1096 if(!p_id)
1097 FATAL_ERROR("NULL parameter: Asn::OCS_setting::OCS_setting()");
1098 }
1099
1100 OCS_setting::~OCS_setting()
1101 {
1102 delete id;
1103 }
1104
1105 string OCS_setting::get_dispname() const
1106 {
1107 string s('<');
1108 switch(st) {
1109 case S_T: s+="Type"; break;
1110 case S_V: s+="Value"; break;
1111 case S_VS: s+="ValueSet"; break;
1112 case S_O: s+="Object"; break;
1113 case S_OS: s+="ObjectSet"; break;
1114 default: s+="???";
1115 }
1116 s+='>';
1117 return s;
1118 }
1119
1120 void OCS_setting::dump(unsigned level) const
1121 {
1122 DEBUG(level, "%s (setting for `%s')",
1123 get_dispname().c_str(),
1124 id->get_dispname().c_str());
1125 }
1126
1127 // =================================
1128 // ===== OS_Element
1129 // =================================
1130
1131 OS_Element::~OS_Element()
1132 {
1133
1134 }
1135
1136 // =================================
1137 // ===== Object
1138 // =================================
1139
1140 void Object::set_my_governor(ObjectClass *p_gov)
1141 {
1142 if(!p_gov)
1143 FATAL_ERROR("NULL parameter: Asn::Object::set_my_governor()");
1144 my_governor=p_gov;
1145 }
1146
1147 OS_Element *Object::clone_ose() const
1148 {
1149 return clone();
1150 }
1151
1152 void Object::set_fullname_ose(const string& p_fullname)
1153 {
1154 set_fullname(p_fullname);
1155 }
1156
1157 void Object::set_genname_ose(const string& p_prefix, const string& p_suffix)
1158 {
1159 set_genname(p_prefix, p_suffix);
1160 }
1161
1162 void Object::set_my_scope_ose(Scope *p_scope)
1163 {
1164 set_my_scope(p_scope);
1165 }
1166
1167 // =================================
1168 // ===== FieldSetting
1169 // =================================
1170
1171 FieldSetting::FieldSetting(Identifier *p_name)
1172 : Node(), Location(), name(p_name), checked(false)
1173 {
1174 if(!p_name)
1175 FATAL_ERROR("NULL parameter: Asn::FieldSetting::FieldSetting()");
1176 }
1177
1178 FieldSetting::FieldSetting(const FieldSetting& p)
1179 : Node(p), Location(p), checked(false)
1180 {
1181 name=p.name->clone();
1182 }
1183
1184 FieldSetting::~FieldSetting()
1185 {
1186 delete name;
1187 }
1188
1189 void FieldSetting::set_fullname(const string& p_fullname)
1190 {
1191 Node::set_fullname(p_fullname);
1192 get_setting()->set_fullname(p_fullname);
1193 }
1194
1195 void FieldSetting::set_my_scope(Scope *p_scope)
1196 {
1197 get_setting()->set_my_scope(p_scope);
1198 }
1199
1200 void FieldSetting::set_genname(const string& p_prefix,
1201 const string& p_suffix)
1202 {
1203 get_setting()->set_genname(p_prefix, p_suffix);
1204 }
1205
1206 // =================================
1207 // ===== FieldSetting_Type
1208 // =================================
1209
1210 FieldSetting_Type::FieldSetting_Type(Identifier *p_name, Type *p_setting)
1211 : FieldSetting(p_name), setting(p_setting)
1212 {
1213 if(!p_setting)
1214 FATAL_ERROR
1215 ("NULL parameter: Asn::FieldSetting_Type::FieldSetting_Type()");
1216 setting->set_ownertype(Type::OT_FIELDSETTING, this);
1217 }
1218
1219 FieldSetting_Type::FieldSetting_Type(const FieldSetting_Type& p)
1220 : FieldSetting(p)
1221 {
1222 setting=p.setting->clone();
1223 }
1224
1225 FieldSetting_Type::~FieldSetting_Type()
1226 {
1227 delete setting;
1228 }
1229
1230 void FieldSetting_Type::chk(FieldSpec *p_fspec)
1231 {
1232 if(checked) return;
1233 if(p_fspec->get_fstype()!=FieldSpec::FS_T) {
1234 error("Type setting was expected");
1235 delete setting;
1236 setting=new Type(Type::T_ERROR);
1237 }
1238 checked=true;
1239 setting->chk();
1240 ReferenceChain refch(setting, "While checking embedded recursions");
1241 setting->chk_recursions(refch);
1242 }
1243
1244 void FieldSetting_Type::generate_code(output_struct *target)
1245 {
1246 setting->generate_code(target);
1247 }
1248
1249 void FieldSetting_Type::dump(unsigned level) const
1250 {
1251 DEBUG(level, "field setting (type): %s", name->get_dispname().c_str());
1252 level++;
1253 setting->dump(level);
1254 }
1255
1256 // =================================
1257 // ===== FieldSetting_Value
1258 // =================================
1259
1260 FieldSetting_Value::FieldSetting_Value(Identifier *p_name, Value *p_setting)
1261 : FieldSetting(p_name), setting(p_setting)
1262 {
1263 if(!p_setting)
1264 FATAL_ERROR
1265 ("NULL parameter: Asn::FieldSetting_Value::FieldSetting_Value()");
1266 }
1267
1268 FieldSetting_Value::FieldSetting_Value(const FieldSetting_Value& p)
1269 : FieldSetting(p)
1270 {
1271 setting=p.setting->clone();
1272 }
1273
1274 FieldSetting_Value::~FieldSetting_Value()
1275 {
1276 delete setting;
1277 }
1278
1279 void FieldSetting_Value::chk(FieldSpec *p_fspec)
1280 {
1281 if(checked) return;
1282 if(p_fspec->get_fstype()!=FieldSpec::FS_V_FT) {
1283 error("Value setting was expected");
1284 setting->set_valuetype(Value::V_ERROR);
1285 }
1286 FieldSpec_V_FT *fs=dynamic_cast<FieldSpec_V_FT*>(p_fspec);
1287 Type *type=fs->get_type();
1288 setting->set_my_governor(type);
1289 type->chk_this_value_ref(setting);
1290 checked=true;
1291 type->chk_this_value(setting, 0, Type::EXPECTED_CONSTANT,
1292 INCOMPLETE_NOT_ALLOWED, OMIT_NOT_ALLOWED, SUB_CHK);
1293 {
1294 ReferenceChain refch(setting, "While checking embedded recursions");
1295 setting->chk_recursions(refch);
1296 }
1297 if (!semantic_check_only) {
1298 setting->set_genname_prefix("const_");
1299 setting->set_genname_recursive(setting->get_genname_own());
1300 setting->set_code_section(GovernedSimple::CS_PRE_INIT);
1301 }
1302 }
1303
1304 void FieldSetting_Value::generate_code(output_struct *target)
1305 {
1306 const_def cdef;
1307 Code::init_cdef(&cdef);
1308 Type *type = setting->get_my_governor();
1309 type->generate_code_object(&cdef, setting);
1310 cdef.init = setting->generate_code_init(cdef.init,
1311 setting->get_lhs_name().c_str());
1312 Code::merge_cdef(target, &cdef);
1313 Code::free_cdef(&cdef);
1314 }
1315
1316 void FieldSetting_Value::dump(unsigned level) const
1317 {
1318 DEBUG(level, "field setting (value): %s", name->get_dispname().c_str());
1319 level++;
1320 setting->dump(level);
1321 }
1322
1323 // =================================
1324 // ===== FieldSetting_O
1325 // =================================
1326
1327 FieldSetting_O::FieldSetting_O(Identifier *p_name, Object *p_setting)
1328 : FieldSetting(p_name), setting(p_setting)
1329 {
1330 if(!p_setting)
1331 FATAL_ERROR
1332 ("NULL parameter: Asn::FieldSetting_O::FieldSetting_O()");
1333 }
1334
1335 FieldSetting_O::FieldSetting_O(const FieldSetting_O& p)
1336 : FieldSetting(p)
1337 {
1338 setting=p.setting->clone();
1339 }
1340
1341 FieldSetting_O::~FieldSetting_O()
1342 {
1343 delete setting;
1344 }
1345
1346 void FieldSetting_O::chk(FieldSpec *p_fspec)
1347 {
1348 if(checked) return;
1349 if(p_fspec->get_fstype()!=FieldSpec::FS_O) {
1350 error("Object setting was expected");
1351 delete setting;
1352 setting=new Obj_defn();
1353 }
1354 FieldSpec_O *fs=dynamic_cast<FieldSpec_O*>(p_fspec);
1355 ObjectClass *oc=fs->get_oc();
1356 setting->set_my_governor(oc);
1357 checked=true;
1358 setting->chk();
1359 }
1360
1361 void FieldSetting_O::generate_code(output_struct *target)
1362 {
1363 setting->generate_code(target);
1364 }
1365
1366 void FieldSetting_O::dump(unsigned level) const
1367 {
1368 DEBUG(level, "field setting (object): %s", name->get_dispname().c_str());
1369 level++;
1370 setting->dump(level);
1371 }
1372
1373 // =================================
1374 // ===== FieldSetting_OS
1375 // =================================
1376
1377 FieldSetting_OS::FieldSetting_OS(Identifier *p_name, ObjectSet *p_setting)
1378 : FieldSetting(p_name), setting(p_setting)
1379 {
1380 if(!p_setting)
1381 FATAL_ERROR
1382 ("NULL parameter: Asn::FieldSetting_OS::FieldSetting_OS()");
1383 }
1384
1385 FieldSetting_OS::FieldSetting_OS(const FieldSetting_OS& p)
1386 : FieldSetting(p)
1387 {
1388 setting=p.setting->clone();
1389 }
1390
1391 FieldSetting_OS::~FieldSetting_OS()
1392 {
1393 delete setting;
1394 }
1395
1396 void FieldSetting_OS::chk(FieldSpec *p_fspec)
1397 {
1398 if(checked) return;
1399 if(p_fspec->get_fstype()!=FieldSpec::FS_OS) {
1400 error("ObjectSet setting was expected");
1401 delete setting;
1402 setting=new OS_defn();
1403 }
1404 FieldSpec_OS *fs=dynamic_cast<FieldSpec_OS*>(p_fspec);
1405 ObjectClass *oc=fs->get_oc();
1406 setting->set_my_governor(oc);
1407 checked=true;
1408 setting->chk();
1409 }
1410
1411 void FieldSetting_OS::generate_code(output_struct *target)
1412 {
1413 setting->generate_code(target);
1414 }
1415
1416 void FieldSetting_OS::dump(unsigned level) const
1417 {
1418 DEBUG(level, "field setting (objectset): %s",
1419 name->get_dispname().c_str());
1420 level++;
1421 setting->dump(level);
1422 }
1423
1424 // =================================
1425 // ===== Obj_defn
1426 // =================================
1427
1428 Obj_defn::Obj_defn(Block *p_block)
1429 : Object(), block(p_block), is_generated(false)
1430 {
1431 if(!p_block)
1432 FATAL_ERROR("NULL parameter: Asn::Obj_defn::Obj_defn()");
1433 }
1434
1435 Obj_defn::Obj_defn()
1436 : Object(), block(0), is_generated(false)
1437 {
1438 set_is_erroneous();
1439 }
1440
1441 Obj_defn::Obj_defn(const Obj_defn& p)
1442 : Object(p), is_generated(false)
1443 {
1444 block=p.block?p.block->clone():0;
1445 for(size_t i=0; i<p.fss.size(); i++)
1446 add_fs(p.fss.get_nth_elem(i)->clone());
1447 }
1448
1449 Obj_defn::~Obj_defn()
1450 {
1451 delete block;
1452 for(size_t i=0; i<fss.size(); i++)
1453 delete fss.get_nth_elem(i);
1454 fss.clear();
1455 }
1456
1457 void Obj_defn::set_fullname(const string& p_fullname)
1458 {
1459 Object::set_fullname(p_fullname);
1460 for(size_t i=0; i<fss.size(); i++) {
1461 FieldSetting *fs=fss.get_nth_elem(i);
1462 fs->set_fullname(p_fullname+"."+fs->get_name().get_dispname());
1463 }
1464 }
1465
1466 void Obj_defn::set_my_scope(Scope *p_scope)
1467 {
1468 Object::set_my_scope(p_scope);
1469 for(size_t i=0; i<fss.size(); i++)
1470 fss.get_nth_elem(i)->set_my_scope(p_scope);
1471 }
1472
1473 void Obj_defn::chk()
1474 {
1475 if(checked) return;
1476 if(block) parse_block();
1477 if(!my_governor)
1478 FATAL_ERROR("Asn::Obj_defn::chk()");
1479 my_governor->chk_this_obj(this);
1480 checked=true;
1481 }
1482
1483 void Obj_defn::add_fs(FieldSetting *p_fs)
1484 {
1485 if(!p_fs)
1486 FATAL_ERROR("NULL parameter: Asn::Obj_defn::add_fs()");
1487 if(fss.has_key(p_fs->get_name().get_name())) {
1488 error("This object already has a field setting with name `%s'",
1489 p_fs->get_name().get_dispname().c_str());
1490 delete p_fs;
1491 return;
1492 }
1493 fss.add(p_fs->get_name().get_name(), p_fs);
1494 p_fs->set_my_scope(my_scope);
1495 p_fs->set_fullname(get_fullname()+"."+p_fs->get_name().get_dispname());
1496 }
1497
1498 bool Obj_defn::has_fs_withName(const Identifier& p_name)
1499 {
1500 if(block) parse_block();
1501 return fss.has_key(p_name.get_name());
1502 }
1503
1504 FieldSetting* Obj_defn::get_fs_byName(const Identifier& p_name)
1505 {
1506 if(block) parse_block();
1507 const string& s=p_name.get_name();
1508 if(fss.has_key(s))
1509 return fss[s];
1510 if(!is_erroneous)
1511 error("No field setting with identifier `%s' in object `%s'",
1512 p_name.get_dispname().c_str(), get_fullname().c_str());
1513 return 0;
1514 }
1515
1516 bool Obj_defn::has_fs_withName_dflt(const Identifier& p_name)
1517 {
1518 chk();
1519 if(is_erroneous) return true;
1520 if(fss.has_key(p_name.get_name())) return true;
1521 if(my_governor->get_fss()->has_fs_withId(p_name)
1522 && my_governor->get_fss()->get_fs_byId(p_name)->has_default())
1523 return true;
1524 return false;
1525 }
1526
1527 Setting* Obj_defn::get_setting_byName_dflt(const Identifier& p_name)
1528 {
1529 chk();
1530 const string& s=p_name.get_name();
1531 if(fss.has_key(s))
1532 return fss[s]->get_setting();
1533 if(my_governor->get_fss()->has_fs_withId(p_name)) {
1534 FieldSpec *fs=my_governor->get_fss()->get_fs_byId(p_name);
1535 if(fs->has_default())
1536 return fs->get_default();
1537 }
1538 if(!is_erroneous)
1539 error("No field setting or default with identifier `%s' in object `%s'",
1540 p_name.get_dispname().c_str(), get_fullname().c_str());
1541 return 0;
1542 }
1543
1544 void Obj_defn::parse_block()
1545 {
1546 if(!block) return;
1547 if(!my_governor)
1548 FATAL_ERROR("Asn::Obj_defn::parse_block():"
1549 " my_governor not set");
1550 OCSV_Parser *ocsv_parser=new OCSV_Parser(block->get_TokenBuf(), this);
1551 my_governor->get_ocs()->accept(*ocsv_parser);
1552 delete ocsv_parser;
1553 delete block; block=0;
1554 }
1555
1556 void Obj_defn::generate_code(output_struct *target)
1557 {
1558 if(is_generated) return;
1559 is_generated=true;
1560 for(size_t i=0; i<fss.size(); i++)
1561 fss.get_nth_elem(i)->generate_code(target);
1562 }
1563
1564 void Obj_defn::dump(unsigned level) const
1565 {
1566 DEBUG(level, "Object definition");
1567 if(block)
1568 DEBUG(level, "with unparsed block");
1569 else {
1570 DEBUG(level, "with field settings:");
1571 level++;
1572 for(size_t i=0; i<fss.size(); i++)
1573 fss.get_nth_elem(i)->dump(level);
1574 }
1575 }
1576
1577 // =================================
1578 // ===== Obj_refd
1579 // =================================
1580
1581 Obj_refd::Obj_refd(Reference *p_ref)
1582 : Object(), ref(p_ref), o_error(0), o_refd(0), o_defn(0)
1583 {
1584 if(!p_ref)
1585 FATAL_ERROR("NULL parameter: Asn::Obj_refd::Obj_refd()");
1586 }
1587
1588 Obj_refd::Obj_refd(const Obj_refd& p)
1589 : Object(p), o_error(0), o_refd(0), o_defn(0)
1590 {
1591 ref=p.ref->clone();
1592 }
1593
1594 Obj_refd::~Obj_refd()
1595 {
1596 delete ref;
1597 delete o_error;
1598 }
1599
1600 void Obj_refd::set_my_scope(Scope *p_scope)
1601 {
1602 Object::set_my_scope(p_scope);
1603 ref->set_my_scope(p_scope);
1604 }
1605
1606 void Obj_refd::set_fullname(const string& p_fullname)
1607 {
1608 Object::set_fullname(p_fullname);
1609 ref->set_fullname(p_fullname);
1610 }
1611
1612 Obj_defn* Obj_refd::get_refd_last(ReferenceChain *refch)
1613 {
1614 if(!o_defn) {
1615 bool destroy_refch=false;
1616 if(!refch) {
1617 refch=new ReferenceChain(ref, "While searching referenced Object");
1618 destroy_refch=true;
1619 }
1620 o_defn=get_refd(refch)->get_refd_last(refch);
1621 if(destroy_refch) delete refch;
1622 }
1623 return o_defn;
1624 }
1625
1626 Object* Obj_refd::get_refd(ReferenceChain *refch)
1627 {
1628 if(refch && !refch->add(get_fullname())) goto error;
1629 if(!o_refd) {
1630 Common::Assignment *ass=ref->get_refd_assignment();
1631 if(!ass) goto error;
1632 Setting *setting=ref->get_refd_setting();
1633 if (!setting || setting->get_st()==Setting::S_ERROR) goto error;
1634 o_refd=dynamic_cast<Object*>(setting);
1635 if(!o_refd) {
1636 error("This is not an objectreference: `%s'",
1637 ref->get_dispname().c_str());
1638 goto error;
1639 }
1640 }
1641 goto end;
1642 error:
1643 o_error=new Obj_defn();
1644 o_error->set_my_governor(my_governor);
1645 o_refd=o_error;
1646 end:
1647 return o_refd;
1648 }
1649
1650 void Obj_refd::chk()
1651 {
1652 if(checked) return;
1653 checked=true;
1654 if(!my_governor)
1655 FATAL_ERROR("Asn::Obj_refd::chk()");
1656 if(get_refd_last()->get_my_governor()->get_refd_last()
1657 !=my_governor->get_refd_last()) {
1658 error("ObjectClass mismatch: Object of class `%s' was expected"
1659 " instead of `%s'",
1660 my_governor->get_refd_last()->get_fullname().c_str(),
1661 get_refd_last()->get_my_governor()->get_refd_last()
1662 ->get_fullname().c_str());
1663 o_error=new Obj_defn();
1664 o_error->set_is_erroneous();
1665 o_error->set_my_governor(my_governor);
1666 o_refd=o_error;
1667 }
1668 }
1669
1670 void Obj_refd::generate_code(output_struct *target)
1671 {
1672 Obj_defn *refd_last = get_refd_last();
1673 if (my_scope->get_scope_mod_gen() ==
1674 refd_last->get_my_scope()->get_scope_mod_gen())
1675 refd_last->generate_code(target);
1676 }
1677
1678 void Obj_refd::dump(unsigned level) const
1679 {
1680 DEBUG(level, "objectreference");
1681 level++;
1682 ref->dump(level);
1683 }
1684
1685 // =================================
1686 // ===== OSE_Visitor
1687 // =================================
1688
1689 OSE_Visitor *OSE_Visitor::clone() const
1690 {
1691 FATAL_ERROR("Asn::OSE_Visitor::clone()");
1692 return 0;
1693 }
1694
1695 // =================================
1696 // ===== ObjectSet
1697 // =================================
1698
1699 void ObjectSet::set_my_governor(ObjectClass *p_gov)
1700 {
1701 if(!p_gov)
1702 FATAL_ERROR("NULL parameter: Asn::ObjectSet::set_my_governor()");
1703 my_governor=p_gov;
1704 }
1705
1706 // =================================
1707 // ===== Objects
1708 // =================================
1709
1710 Objects::Objects(const Objects& p)
1711 : Node(p)
1712 {
1713 objs.replace(0, 0, &p.objs);
1714 }
1715
1716 Objects::~Objects()
1717 {
1718 objs.clear();
1719 }
1720
1721 void Objects::add_objs(Objects *p_objs)
1722 {
1723 if(!p_objs)
1724 FATAL_ERROR("NULL parameter: Asn::Objects::add_objs()");
1725 objs.replace(objs.size(), 0, &p_objs->objs);
1726 }
1727
1728 void Objects::dump(unsigned level) const
1729 {
1730 for(size_t i=0; i<objs.size(); i++)
1731 objs[i]->dump(level);
1732 }
1733
1734 // =================================
1735 // ===== OSEV_objcollctr
1736 // =================================
1737
1738 OSEV_objcollctr::OSEV_objcollctr(ObjectSet& parent)
1739 : OSE_Visitor(0)
1740 {
1741 governor=parent.get_my_governor()->get_refd_last();
1742 loc=&parent;
1743 objs=new Objects();
1744 visdes.add(parent.get_refd_last(), 0);
1745 }
1746
1747 OSEV_objcollctr::OSEV_objcollctr(const Location *p_loc,
1748 ObjectClass *p_governor)
1749 : OSE_Visitor(p_loc)
1750 {
1751 if(!p_governor)
1752 FATAL_ERROR("Asn::OSEV_objcollctr::OSEV_objcollctr()");
1753 governor=p_governor->get_refd_last();
1754 objs=new Objects();
1755 }
1756
1757 OSEV_objcollctr::~OSEV_objcollctr()
1758 {
1759 delete objs;
1760 visdes.clear();
1761 }
1762
1763 void OSEV_objcollctr::visit_Object(Object& p)
1764 {
1765 Obj_defn *o = p.get_refd_last();
1766 if(o->get_is_erroneous()) return;
1767 if(visdes.has_key(o)) return;
1768 if(o->get_my_governor()->get_refd_last()!=governor) {
1769 loc->error("Objects of objectclass `%s' are expected; "
1770 "`%s' is object of class `%s'",
1771 governor->get_fullname().c_str(),
1772 p.get_fullname().c_str(),
1773 p.get_my_governor()->get_refd_last()->get_fullname().c_str());
1774 return;
1775 }
1776 visdes.add(o, 0);
1777 objs->add_o(o);
1778 }
1779
1780 void OSEV_objcollctr::visit_OS_refd(OS_refd& p)
1781 {
1782 visit_ObjectSet(p);
1783 }
1784
1785 void OSEV_objcollctr::visit_ObjectSet(ObjectSet& p, bool force)
1786 {
1787 OS_defn& os=*p.get_refd_last();
1788 if(os.get_my_governor()->get_refd_last()!=governor) {
1789 loc->error("Objects of objectclass `%s' are expected; "
1790 "`%s' is objectset of class `%s'",
1791 governor->get_fullname().c_str(),
1792 p.get_fullname().c_str(),
1793 os.get_my_governor()->get_refd_last()->get_fullname().c_str());
1794 return;
1795 }
1796 if(visdes.has_key(&os)) {
1797 if(!force) return;
1798 }
1799 else visdes.add(&os, 0);
1800 Objects *other_objs=os.get_objs();
1801 for(size_t i=0; i<other_objs->get_nof_objs(); i++)
1802 visit_Object(*other_objs->get_obj_byIndex(i));
1803 }
1804
1805 Objects* OSEV_objcollctr::give_objs()
1806 {
1807 Objects *tmp_objs=objs;
1808 objs=0;
1809 return tmp_objs;
1810 }
1811
1812 // =================================
1813 // ===== OSEV_checker
1814 // =================================
1815
1816 OSEV_checker::OSEV_checker(const Location *p_loc, ObjectClass *p_governor)
1817 : OSE_Visitor(p_loc), governor(p_governor)
1818 {
1819 if(!p_governor)
1820 FATAL_ERROR("NULL parameter: Asn::OSEV_checker::OSEV_checker()");
1821 gov_defn=p_governor->get_refd_last();
1822 }
1823
1824 void OSEV_checker::visit_Object(Object& p)
1825 {
1826 p.set_my_governor(governor);
1827 p.chk();
1828 }
1829
1830 void OSEV_checker::visit_OS_refd(OS_refd& p)
1831 {
1832 p.set_my_governor(governor);
1833 p.chk();
1834 }
1835
1836 // =================================
1837 // ===== OSEV_codegen
1838 // =================================
1839
1840 void OSEV_codegen::visit_Object(Object& p)
1841 {
1842 p.generate_code(target);
1843 }
1844
1845 void OSEV_codegen::visit_OS_refd(OS_refd& p)
1846 {
1847 p.generate_code(target);
1848 }
1849
1850 // =================================
1851 // ===== OS_defn
1852 // =================================
1853
1854 OS_defn::OS_defn()
1855 : ObjectSet(), block(0), objs(0), is_generated(false)
1856 {
1857 oses=new vector<OS_Element>();
1858 }
1859
1860 OS_defn::OS_defn(Block *p_block)
1861 : ObjectSet(), block(p_block), objs(0), is_generated(false)
1862 {
1863 if(!p_block)
1864 FATAL_ERROR("NULL parameter: Asn::OS_defn::OS_defn()");
1865 oses=new vector<OS_Element>();
1866 }
1867
1868 OS_defn::OS_defn(Objects *p_objs)
1869 : ObjectSet(), block(0), objs(p_objs), is_generated(false)
1870 {
1871 if(!p_objs)
1872 FATAL_ERROR("NULL parameter: Asn::OS_defn::OS_defn()");
1873 oses=new vector<OS_Element>();
1874 }
1875
1876 OS_defn::OS_defn(const OS_defn& p)
1877 : ObjectSet(p), objs(0), is_generated(false)
1878 {
1879 block=p.block?p.block->clone():0;
1880 oses=new vector<OS_Element>();
1881 for(size_t i=0; i<p.oses->size(); i++)
1882 oses->add((*p.oses)[i]->clone_ose());
1883 }
1884
1885 OS_defn::~OS_defn()
1886 {
1887 delete block;
1888 delete objs;
1889 for(size_t i=0; i<oses->size(); i++)
1890 delete (*oses)[i];
1891 oses->clear();
1892 delete oses;
1893 }
1894
1895 void OS_defn::steal_oses(OS_defn *other_os)
1896 {
1897 if(!oses || objs || !other_os || !other_os->oses || other_os->objs)
1898 FATAL_ERROR("Asn::OS_defn::steal_oses()");
1899 oses->replace(oses->size(), 0, other_os->oses);
1900 other_os->oses->clear();
1901 delete other_os;
1902 }
1903
1904 OS_defn* OS_defn::get_refd_last(ReferenceChain *refch)
1905 {
1906 if(oses->size()!=1) return this;
1907 OS_refd* ref=dynamic_cast<OS_refd*>((*oses)[0]);
1908 if(!ref) return this;
1909 OS_defn* reflast=0;
1910 bool destroy_refch=false;
1911 if(!refch) {
1912 refch=new ReferenceChain
1913 (this, "While searching quasi-referenced ObjectSet");
1914 destroy_refch=true;
1915 }
1916 refch->add(get_fullname());
1917 reflast=ref->get_refd_last(refch);
1918 if(destroy_refch) delete refch;
1919 return reflast;
1920 }
1921
1922 void OS_defn::set_fullname(const string& p_fullname)
1923 {
1924 ObjectSet::set_fullname(p_fullname);
1925 for(size_t i=0; i<oses->size(); i++)
1926 (*oses)[i]->set_fullname_ose(p_fullname+"."+Int2string(i+1));
1927 }
1928
1929 void OS_defn::set_my_scope(Scope *p_scope)
1930 {
1931 ObjectSet::set_my_scope(p_scope);
1932 for(size_t i=0; i<oses->size(); i++)
1933 (*oses)[i]->set_my_scope_ose(p_scope);
1934 }
1935
1936 void OS_defn::add_ose(OS_Element *p_ose)
1937 {
1938 if(!p_ose)
1939 FATAL_ERROR("NULL parameter: Asn::OS_defn::add_ose");
1940 oses->add(p_ose);
1941 }
1942
1943 size_t OS_defn::get_nof_objs()
1944 {
1945 if(!objs) create_objs();
1946 return objs->get_nof_objs();
1947 }
1948
1949 Object* OS_defn::get_obj_byIndex(size_t p_i)
1950 {
1951 if(!objs) create_objs();
1952 return objs->get_obj_byIndex(p_i);
1953 }
1954
1955 Objects* OS_defn::get_objs()
1956 {
1957 if(!objs) create_objs();
1958 return objs;
1959 }
1960
1961 void OS_defn::parse_block()
1962 {
1963 if(!block) return;
1964 Node *node=block->parse(KW_Block_ObjectSetSpec);
1965 OS_defn *tmp=dynamic_cast<OS_defn*>(node);
1966 if(tmp){
1967 set_location(*tmp);
1968 delete block; block=0;
1969 delete oses;
1970 oses=tmp->oses;
1971 tmp->oses=new vector<OS_Element>();
1972 delete tmp;
1973 }else{
1974 delete block; block=0;
1975 delete oses;
1976 oses = new vector<OS_Element>();
1977 }
1978 set_fullname(get_fullname());
1979 set_my_scope(get_my_scope());
1980 }
1981
1982 void OS_defn::chk()
1983 {
1984 if(checked) return;
1985 if(block) parse_block();
1986 if(!my_governor)
1987 FATAL_ERROR("Asn::OS_defn::chk()");
1988 Error_Context cntxt(this, "In objectset definition `%s'",
1989 get_fullname().c_str());
1990 OSEV_checker osev(this, my_governor);
1991 for(size_t i=0; i<oses->size(); i++) {
1992 OS_Element *ose=(*oses)[i];
1993 ose->set_genname_ose(get_genname_own(), Int2string(i + 1));
1994 ose->accept(osev);
1995 }
1996 checked=true;
1997 //create_objs();
1998 }
1999
2000 void OS_defn::create_objs()
2001 {
2002 if(objs) return;
2003 if(!my_governor)
2004 FATAL_ERROR("Asn::OS_defn::create_objs()");
2005 if(!checked) chk();
2006 Error_Context cntxt(this, "While exploring objectset definition `%s'",
2007 get_fullname().c_str());
2008 OSEV_objcollctr osev(*this);
2009 for(size_t i=0; i<oses->size(); i++)
2010 (*oses)[i]->accept(osev);
2011 objs=osev.give_objs();
2012 }
2013
2014 void OS_defn::generate_code(output_struct *target)
2015 {
2016 if (is_generated) return;
2017 is_generated = true;
2018 OSEV_codegen osev(this, target);
2019 for(size_t i=0; i<oses->size(); i++)
2020 (*oses)[i]->accept(osev);
2021 }
2022
2023 void OS_defn::dump(unsigned level) const
2024 {
2025 DEBUG(level, "ObjectSet definition");
2026 if(block) DEBUG(level, "with unparsed block");
2027 if(objs) {
2028 DEBUG(level, "with collected objects (%lu pcs)",
2029 (unsigned long) objs->get_nof_objs());
2030 objs->dump(level+1);
2031 }
2032 else DEBUG(level, "with uncollected objects");
2033 }
2034
2035 // =================================
2036 // ===== OS_refd
2037 // =================================
2038
2039 OS_refd::OS_refd(Reference *p_ref)
2040 : ObjectSet(), OS_Element(), ref(p_ref), os_error(0),
2041 os_refd(0), os_defn(0)
2042 {
2043 if(!p_ref)
2044 FATAL_ERROR("NULL parameter: Asn::OS_refd::OS_refd()");
2045 }
2046
2047 OS_refd::OS_refd(const OS_refd& p)
2048 : ObjectSet(p), OS_Element(p), os_error(0), os_refd(0), os_defn(0)
2049 {
2050 ref=p.ref->clone();
2051 }
2052
2053 string OS_refd::create_stringRepr()
2054 {
2055 return get_refd_last()->get_stringRepr();
2056 }
2057
2058 OS_refd::~OS_refd()
2059 {
2060 delete ref;
2061 delete os_error;
2062 }
2063
2064 void OS_refd::set_my_scope(Scope *p_scope)
2065 {
2066 ObjectSet::set_my_scope(p_scope);
2067 ref->set_my_scope(p_scope);
2068 }
2069
2070 ObjectSet* OS_refd::get_refd(ReferenceChain *refch)
2071 {
2072 if(refch) refch->add(get_fullname());
2073 if(!os_refd) {
2074 Setting *setting=0;
2075 Common::Assignment *ass=ref->get_refd_assignment();
2076 if(!ass) goto error;
2077 setting=ref->get_refd_setting();
2078 if(setting->get_st()==Setting::S_ERROR) goto error;
2079 os_refd=dynamic_cast<ObjectSet*>(setting);
2080 if(!os_refd) {
2081 error("This is not an objectsetreference: `%s'",
2082 ref->get_dispname().c_str());
2083 goto error;
2084 }
2085 goto end;
2086 error:
2087 /* create an empty ObjectSet */
2088 os_error=new OS_defn();
2089 os_error->set_my_governor(my_governor);
2090 os_refd=os_error;
2091 }
2092 end:
2093 return os_refd;
2094 }
2095
2096 OS_defn* OS_refd::get_refd_last(ReferenceChain *refch)
2097 {
2098 if(!os_defn) {
2099 bool destroy_refch=false;
2100 if(!refch) {
2101 refch=new ReferenceChain(ref, "While searching referenced ObjectSet");
2102 destroy_refch=true;
2103 }
2104 ObjectSet *t_os = get_refd(refch);
2105 os_defn= t_os->get_refd_last(refch);
2106 if(destroy_refch) delete refch;
2107 }
2108 return os_defn;
2109 }
2110
2111 size_t OS_refd::get_nof_objs()
2112 {
2113 return get_refd_last()->get_nof_objs();
2114 }
2115
2116 Object* OS_refd::get_obj_byIndex(size_t p_i)
2117 {
2118 return get_refd_last()->get_obj_byIndex(p_i);
2119 }
2120
2121 void OS_refd::chk()
2122 {
2123 if(checked) return;
2124 checked=true;
2125 if(!my_governor)
2126 FATAL_ERROR("Asn::OS_refd::chk()");
2127 ObjectClass *my_class = my_governor->get_refd_last();
2128 ObjectClass *refd_class =
2129 get_refd_last()->get_my_governor()->get_refd_last();
2130 if (my_class != refd_class) {
2131 error("ObjectClass mismatch: ObjectSet of class `%s' was expected "
2132 "instead of `%s'", my_class->get_fullname().c_str(),
2133 refd_class->get_fullname().c_str());
2134 os_error=new OS_defn();
2135 os_error->set_my_governor(my_governor);
2136 os_refd=os_error;
2137 }
2138 }
2139
2140 void OS_refd::generate_code(output_struct *target)
2141 {
2142 OS_defn *refd_last = get_refd_last();
2143 if(my_scope->get_scope_mod_gen() ==
2144 refd_last->get_my_scope()->get_scope_mod_gen())
2145 refd_last->generate_code(target);
2146 }
2147
2148 void OS_refd::dump(unsigned level) const
2149 {
2150 DEBUG(level, "objectsetreference");
2151 level++;
2152 ref->dump(level);
2153 }
2154
2155 OS_Element *OS_refd::clone_ose() const
2156 {
2157 return clone();
2158 }
2159
2160 void OS_refd::set_fullname_ose(const string& p_fullname)
2161 {
2162 set_fullname(p_fullname);
2163 }
2164
2165 void OS_refd::set_genname_ose(const string& p_prefix, const string& p_suffix)
2166 {
2167 set_genname(p_prefix, p_suffix);
2168 }
2169
2170 void OS_refd::set_my_scope_ose(Scope *p_scope)
2171 {
2172 set_my_scope(p_scope);
2173 }
2174
2175 } // namespace Asn
This page took 0.13319 seconds and 5 git commands to generate.