180d0d117d85a64d5321c375617c65a0264665b4
[deliverable/titan.core.git] / compiler2 / asn1 / AST_asn1.hh
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 #ifndef _Asn_AST_HH
9 #define _Asn_AST_HH
10
11 #include "../AST.hh"
12 #include "../Int.hh"
13 #include "Object0.hh"
14
15 namespace Common {
16 class CodeGenHelper;
17 }
18
19 /**
20 * This namespace contains things related to ASN.1 compiler.
21 */
22 namespace Asn {
23
24 /**
25 * \addtogroup AST
26 *
27 * @{
28 */
29
30 class Assignments;
31 /**
32 * This is used when parsing internal stuff (e.g., EXTERNAL,
33 * EMBEDDED PDV, etc.).
34 */
35 extern Assignments *parsed_assignments;
36
37 using namespace Common;
38
39 struct TagDefault;
40
41 class Assignment;
42 class Module;
43 class Symbols;
44 class Exports;
45 class ImpMod;
46 class Imports;
47 class Ass_Undef;
48 class Ass_T;
49 class Ass_V;
50 class Ass_VS;
51 class Ass_OC;
52 class Ass_O;
53 class Ass_OS;
54
55 /* not defined, only declared in this file */
56
57 class Block;
58 class TokenBuf;
59 class ValueSet;
60 class ObjectClass;
61 class Object;
62 class ObjectSet;
63
64 /**
65 * Struct to represent default tagging.
66 */
67 struct TagDefault {
68 enum tagdef_t {
69 EXPLICIT, /**< EXPLICIT TAGS */
70 IMPLICIT, /**< IMPLICIT TAGS */
71 AUTOMATIC /**< AUTOMATIC TAGS */
72 };
73 };
74
75 /**
76 * Helper-class for parameterized assignments.
77 */
78 class Ass_pard : public Node {
79 private:
80 Assignment *my_ass; // link to...
81 Block *parlist_block; /**< parameter list */
82 /** if the block (parlist) is pre-parsed then it is deleted, and
83 * the content is put into these vectors. */
84 vector<Identifier> dummyrefs;
85 /** contains one tokenbuf for each parameter; e.g. for parameter
86 * "INTEGER : i" it contains "i INTEGER ::=" */
87 vector<TokenBuf> governors;
88 /** Instance counters: for each target module a separate counter is
89 * maintained to get deterministic instance numbers in case of incremental
90 * compilation. */
91 map<Common::Module*, size_t> inst_cnts;
92
93 Ass_pard(const Ass_pard& p);
94 Ass_pard& operator=(const Ass_pard& p);
95 /** Split the formal parameterlist */
96 void preparse_pars();
97 public:
98 Ass_pard(Block *p_parlist_block);
99 virtual ~Ass_pard();
100 virtual Ass_pard* clone() const
101 {return new Ass_pard(*this);}
102 void set_my_ass(Assignment *p_my_ass) {my_ass=p_my_ass;}
103 /** Returns the number of formal parameters. 0 means the parameter
104 * list is erroneous. */
105 size_t get_nof_pars();
106 const Identifier& get_nth_dummyref(size_t i);
107 TokenBuf* clone_nth_governor(size_t i);
108 /** Returns the next instance number for target module \a p_mod */
109 size_t new_instnum(Common::Module *p_mod);
110 };
111
112 /**
113 * Abstract class to represent different kinds of assignments.
114 */
115 class Assignment : public Common::Assignment {
116 protected: // many classes derived
117 Ass_pard *ass_pard;
118 bool dontgen;
119
120 Assignment(const Assignment& p);
121 Assignment& operator=(const Assignment& p);
122 virtual string get_genname() const;
123 virtual Assignment* new_instance0();
124 public:
125 Assignment(asstype_t p_asstype, Identifier *p_id, Ass_pard *p_ass_pard);
126 virtual ~Assignment();
127 virtual Assignment* clone() const =0;
128 virtual bool is_asstype(asstype_t p_asstype, ReferenceChain* refch=0);
129 virtual void set_right_scope(Scope *p_scope) =0;
130 void set_dontgen() {dontgen=true;}
131 /** Returns 0 if assignment is not parameterized! */
132 Ass_pard* get_ass_pard() const { return ass_pard; }
133 /** Returns 0 if this assignment is not parameterized! */
134 Assignment* new_instance(Common::Module *p_mod);
135 virtual Type* get_Type();
136 virtual Value* get_Value();
137 virtual ValueSet* get_ValueSet();
138 virtual ObjectClass* get_ObjectClass();
139 virtual Object* get_Object();
140 virtual ObjectSet* get_ObjectSet();
141 virtual void chk();
142 virtual void dump(unsigned level) const;
143 };
144
145 /**
146 * Class to store assignments. It also contains the ASN-related
147 * pre-defined (so-called "useful") stuff.
148 */
149 class Assignments : public Common::Assignments {
150 private:
151 /** Special assignments. */
152 static Module *_spec_asss;
153 public:
154 static void create_spec_asss();
155 static void destroy_spec_asss();
156 /** Returns whether module \a p_mod is the module that contains the
157 * special assignments */
158 static bool is_spec_asss(Common::Module *p_mod);
159 private:
160 /** The assignments. */
161 map<string, Assignment> asss_m;
162 vector<Assignment> asss_v;
163 /** Indicates whether the uniqueness of identifiers has been checked. */
164 bool checked;
165
166 Assignments(const Assignments&);
167 public:
168 /** Constructor. */
169 Assignments() : Common::Assignments(), asss_m(), asss_v(), checked(false)
170 { }
171 /** Destructor. */
172 virtual ~Assignments();
173 virtual Assignments* clone() const;
174 /** Sets the fullname. */
175 virtual void set_fullname(const string& p_fullname);
176 /** Also looks into special assignments. */
177 virtual bool has_local_ass_withId(const Identifier& p_id);
178 /** Also looks into special assignments. */
179 virtual Assignment* get_local_ass_byId(const Identifier& p_id);
180 virtual size_t get_nof_asss();
181 virtual Common::Assignment* get_ass_byIndex(size_t p_i);
182 /** Adds the Assignment. If \a checked flag is true and the id of the new
183 * assignment is not unique it reports the error. */
184 void add_ass(Assignment *p_ass);
185 /** Checks the uniqueness of identifiers. */
186 void chk_uniq();
187 /** Checks all assignments. */
188 void chk();
189 /** Sets the scope for the right side of assignments. Used in
190 * parametrization. There is a strange situation that the left
191 * and right sides of assignments can have different parent
192 * scopes. */
193 void set_right_scope(Scope *p_scope);
194 void generate_code(output_struct* target);
195 void generate_code(CodeGenHelper& cgh);
196 void dump(unsigned level) const;
197 };
198
199 /**
200 * Class to represent ASN-modules.
201 */
202 class Module : public Common::Module {
203 private:
204 /** default tagging */
205 TagDefault::tagdef_t tagdef;
206 /** extensibility implied */
207 bool extens_impl;
208 /** exported stuff */
209 Exports *exp;
210 /** imported stuff. */
211 Imports *imp;
212 /** assignments of the module */
213 Assignments *asss;
214 private:
215 /** Copy constructor not implemented. */
216 Module(const Module&);
217 /** Assignment not implemented */
218 Module& operator=(const Module&);
219 public:
220 Module(Identifier *p_modid, TagDefault::tagdef_t p_tagdef,
221 bool p_extens_impl, Exports *p_exp, Imports *p_imp,
222 Assignments *p_asss);
223 virtual ~Module();
224 virtual Module* clone() const;
225 virtual Common::Assignment* importAssignment(
226 const Identifier& p_source_modid, const Identifier& p_id) const;
227 /** Sets the fullname. */
228 virtual void set_fullname(const string& p_fullname);
229 virtual Common::Assignments* get_scope_asss();
230 virtual bool has_imported_ass_withId(const Identifier& p_id);
231 /** Returns the assignment referenced by p_ref. Appends an error
232 * message an returns 0 if assignment is not found. */
233 virtual Common::Assignment* get_ass_bySRef(Ref_simple *p_ref);
234 virtual Assignments *get_asss();
235 virtual bool exports_sym(const Identifier& p_id);
236 virtual void chk_imp(ReferenceChain& refch, vector<Common::Module>& moduleStack);
237 virtual void chk();
238 private:
239 virtual void get_imported_mods(module_set_t& p_imported_mods);
240 virtual bool has_circular_import();
241 virtual void generate_code_internal(CodeGenHelper& cgh);
242 public:
243 virtual void dump(unsigned level) const;
244 void add_ass(Assignment *p_ass);
245 TagDefault::tagdef_t get_tagdef() const { return tagdef; }
246
247 /** Generates JSON schema segments for the types defined in the module
248 * and adds them to the JSON schema parameter. */
249 virtual void add_types_to_json_schema(JSON_Tokenizer&);
250
251 /** Does nothing. ASN.1 modules can't contain functions. */
252 virtual void add_func_to_json_schema(map<Type*, JSON_Tokenizer>&) {}
253 };
254
255 /**
256 * SymbolList.
257 * Used only while parsing a module (and building the AST).
258 */
259 class Symbols : public Node {
260 friend class Exports;
261 friend class ImpMod;
262 friend class Imports;
263 private:
264 /** string => Identifier container */
265 typedef map<string, Identifier> syms_t;
266 /** temporary container for symbols (no uniqueness checks) */
267 vector<Identifier> syms_v;
268 /** the symbols */
269 syms_t syms_m;
270 private:
271 /** Copy constructor not implemented. */
272 Symbols(const Symbols&);
273 /** Assignment not implemented. */
274 Symbols& operator=(const Symbols&);
275 public:
276 /** Default constructor. */
277 Symbols() : Node(), syms_v(), syms_m() { }
278 /** Destructor. */
279 virtual ~Symbols();
280 /** Not implemented. */
281 virtual Symbols* clone() const;
282 /** Adds \a p_id to the symlist. */
283 void add_sym(Identifier *p_id);
284 /** Ensures uniqueness of identifiers in syms */
285 void chk_uniq(const Location& p_loc);
286 };
287
288 /**
289 * Exported symbols of a module.
290 */
291 class Exports : public Node, public Location {
292 private:
293 bool checked;
294 /** string => Identifier container */
295 /** my module */
296 Module *my_mod;
297 /** exports all */
298 bool expall;
299 /** exported symbols */
300 Symbols *symbols;
301 private:
302 /** Copy constructor not implemented. */
303 Exports(const Exports&);
304 /** Assignment not implemented */
305 Exports& operator=(const Exports&);
306 public:
307 /** Constructor used when the module exports all. */
308 Exports(bool p_expall=false);
309 /** Constructor used when there is a SymbolList. */
310 Exports(Symbols *p_symlist);
311 /** Destructor. */
312 virtual ~Exports();
313 /** Not implemented. */
314 virtual Exports* clone() const;
315 /** Sets the internal pointer to its module. */
316 void set_my_mod(Module *p_mod) {my_mod=p_mod;}
317 /** Returns true if a symbol with identifier \a p_id is
318 * exported. */
319 bool exports_sym(const Identifier& p_id);
320 /** Checks whether the exported symbols are defined in the module. */
321 void chk_exp();
322 };
323
324 /**
325 * Imported module.
326 */
327 class ImpMod : public Node, public Location {
328 friend class Imports;
329 private:
330 /** my module */
331 Module *my_mod;
332 /** identifier of imported module */
333 Identifier *modid;
334 /** pointer to the imported module */
335 Common::Module *mod;
336 /** imported symbols */
337 Symbols *symbols;
338 private:
339 /** Copy constructor not implemented. */
340 ImpMod(const ImpMod&);
341 /** Assignment not implemented */
342 ImpMod& operator=(const ImpMod&);
343 public:
344 /** Constructor. */
345 ImpMod(Identifier *p_modid, Symbols *p_symlist);
346 /** Destructor. */
347 virtual ~ImpMod();
348 /** Not implemented. */
349 virtual ImpMod* clone() const;
350 /** Sets the internal pointer to its module. */
351 void set_my_mod(Module *p_mod) {my_mod=p_mod;}
352 /** Gets the module id of imported module. */
353 const Identifier& get_modid() const {return *modid;}
354 void set_mod(Common::Module *p_mod) { mod = p_mod; }
355 Common::Module *get_mod() const { return mod; }
356 bool has_sym(const Identifier& p_id) const;
357 /** Checks whether the imported symbols exist. Appends error
358 * messages when needed. */
359 void chk_imp(ReferenceChain& refch);
360 void generate_code(output_struct *target);
361 };
362
363 /**
364 * Imported modules.
365 */
366 class Imports : public Node, public Location {
367 friend class Module;
368 private:
369 /** my module */
370 Module *my_mod;
371 /** imported modules */
372 vector<Asn::ImpMod> impmods_v;
373 map<string, Asn::ImpMod> impmods;
374 /** these symbols are mentioned only once in IMPORTS */
375 map<string, Common::Module> impsyms_1;
376 /** these symbols are mentioned more than once in IMPORTS. This
377 * map is used as a set. */
378 map<string, void> impsyms_m;
379 /** Indicates whether the import list has been checked. */
380 bool checked;
381 /** Indicates whether the owner module is a part of a circular import
382 * chain. */
383 bool is_circular;
384 private:
385 /** Copy constructor not implemented. */
386 Imports(const Imports&);
387 /** Assignment not implemented */
388 Imports& operator=(const Imports&);
389 public:
390 /** Constructor. */
391 Imports()
392 : Node(), my_mod(0), impmods_v(), impmods(), impsyms_1(), impsyms_m(),
393 checked(false), is_circular(false) {}
394 /** Destructor. */
395 virtual ~Imports();
396 /** Not implemented. */
397 virtual Imports* clone() const;
398 /** Adds the ImpMod. If a module with that id already exists,
399 * throws an Error_AST_uniq exception. */
400 void add_impmod(ImpMod *p_impmod);
401 /** Sets the internal pointer my_mod to \a p_mod. */
402 void set_my_mod(Module *p_mod);
403 /** Recursive check. Checks whether the modules with the id exist,
404 * then checks that ImpMod. */
405 void chk_imp(ReferenceChain& refch);
406 /** Returns whether symbols from module with identifier \a p_id
407 * are imported. */
408 bool has_impmod_withId(const Identifier& p_id) const
409 { return impmods.has_key(p_id.get_name()); }
410 const ImpMod *get_impmod_byId(const Identifier& p_id) const
411 { return impmods[p_id.get_name()]; }
412 /** Returns whether a symbol with identifier \a p_id is imported from one
413 * or more modules */
414 bool has_impsym_withId(const Identifier& p_id) const;
415 void get_imported_mods(Module::module_set_t& p_imported_mods);
416 bool get_is_circular() const { return is_circular; }
417 void generate_code(output_struct *target);
418 };
419
420 /**
421 * Undefined assignment.
422 */
423 class Ass_Undef : public Assignment {
424 private:
425 Node *left, *right;
426 Scope *right_scope;
427 /** the classified assignment */
428 Assignment *ass;
429
430 void classify_ass(ReferenceChain *refch=0);
431 virtual Assignment* new_instance0();
432 Ass_Undef(const Ass_Undef& p);
433 /// Assignment disabled
434 Ass_Undef& operator=(const Ass_Undef& p);
435 public:
436 Ass_Undef(Identifier *p_id, Ass_pard *p_ass_pard,
437 Node *p_left, Node *p_right);
438 virtual ~Ass_Undef();
439 virtual Assignment* clone() const;
440 virtual asstype_t get_asstype() const;
441 virtual void set_fullname(const string& p_fullname);
442 virtual void set_my_scope(Scope *p_scope);
443 virtual void set_right_scope(Scope *p_scope);
444 virtual bool is_asstype(asstype_t p_asstype, ReferenceChain* refch=0);
445 virtual Setting* get_Setting();
446 virtual Type* get_Type();
447 virtual Value* get_Value();
448 virtual ValueSet* get_ValueSet();
449 virtual ObjectClass* get_ObjectClass();
450 virtual Object* get_Object();
451 virtual ObjectSet* get_ObjectSet();
452 virtual void chk();
453 virtual void generate_code(output_struct *target, bool clean_up = false);
454 virtual void generate_code(CodeGenHelper& cgh);
455 virtual void dump(unsigned level) const;
456 private:
457 bool _error_if_pard();
458 };
459
460 /**
461 * Erroneous assignment.
462 */
463 class Ass_Error : public Assignment {
464 private:
465 Setting *setting_error;
466 Type *type_error;
467 Value *value_error;
468
469 /// Copy constructor not implemented (even though clone works)
470 Ass_Error(const Ass_Error& p);
471 /// Assignment disabled
472 Ass_Error& operator=(const Ass_Error& p);
473 virtual Assignment* new_instance0();
474 public:
475 Ass_Error(Identifier *p_id, Ass_pard *p_ass_pard);
476 virtual ~Ass_Error();
477 virtual Assignment* clone() const;
478 virtual bool is_asstype(asstype_t p_asstype, ReferenceChain* refch=0);
479 virtual void set_right_scope(Scope *) {}
480 virtual Setting* get_Setting();
481 virtual Type* get_Type();
482 virtual Value* get_Value();
483 virtual ValueSet* get_ValueSet();
484 virtual ObjectClass* get_ObjectClass();
485 virtual Object* get_Object();
486 virtual ObjectSet* get_ObjectSet();
487 virtual void chk();
488 virtual void dump(unsigned level) const;
489 };
490
491 /**
492 * Type assignment.
493 */
494 class Ass_T : public Assignment {
495 private:
496 Type *right;
497
498 virtual Assignment* new_instance0();
499 /// Copy constructor for clone() only
500 Ass_T(const Ass_T& p);
501 /// Assignment disabled
502 Ass_T& operator=(const Ass_T& p);
503 public:
504 Ass_T(Identifier *p_id, Ass_pard *p_ass_pard, Type *p_right);
505 virtual ~Ass_T();
506 virtual Ass_T* clone() const
507 {return new Ass_T(*this);}
508 virtual void set_fullname(const string& p_fullname);
509 virtual void set_my_scope(Scope *p_scope);
510 virtual void set_right_scope(Scope *p_scope);
511 virtual Setting* get_Setting() {return get_Type();}
512 virtual Type* get_Type();
513 virtual void chk();
514 virtual void generate_code(output_struct *target, bool clean_up = false);
515 virtual void generate_code(CodeGenHelper& cgh);
516 virtual void dump(unsigned level) const;
517 };
518
519 /**
520 * Value assignment.
521 */
522 class Ass_V : public Assignment {
523 private:
524 Type *left;
525 Value *right;
526
527 virtual Assignment* new_instance0();
528 /// Copy constructor for clone() only
529 Ass_V(const Ass_V& p);
530 /// Assignment disabled
531 Ass_V& operator=(const Ass_V& p);
532 public:
533 Ass_V(Identifier *p_id, Ass_pard *p_ass_pard,
534 Type *p_left, Value *p_right);
535 virtual ~Ass_V();
536 virtual Ass_V* clone() const
537 {return new Ass_V(*this);}
538 virtual void set_fullname(const string& p_fullname);
539 virtual void set_my_scope(Scope *p_scope);
540 virtual void set_right_scope(Scope *p_scope);
541 virtual Setting* get_Setting() {return get_Value();}
542 virtual Type* get_Type();
543 virtual Value* get_Value();
544 virtual void chk();
545 virtual void generate_code(output_struct *target, bool clean_up = false);
546 virtual void generate_code(CodeGenHelper& cgh);
547 virtual void dump(unsigned level) const;
548 };
549
550 /**
551 * ValueSet assignment.
552 */
553 class Ass_VS : public Assignment {
554 private:
555 Type *left;
556 Block *right;
557 Scope *right_scope;
558
559 virtual Assignment* new_instance0();
560 /// Copy constructor for clone() only
561 Ass_VS(const Ass_VS& p);
562 /// Assignment disabled
563 Ass_VS& operator=(const Ass_VS& p);
564 public:
565 Ass_VS(Identifier *p_id, Ass_pard *p_ass_pard,
566 Type *p_left, Block *p_right);
567 virtual ~Ass_VS();
568 virtual Ass_VS* clone() const
569 {return new Ass_VS(*this);}
570 virtual void set_fullname(const string& p_fullname);
571 virtual void set_my_scope(Scope *p_scope);
572 virtual void set_right_scope(Scope *p_scope);
573 virtual Setting* get_Setting() {return get_Type();}
574 virtual Type* get_Type();
575 virtual void chk();
576 virtual void generate_code(output_struct *target, bool clean_up = false);
577 virtual void generate_code(CodeGenHelper& cgh);
578 virtual void dump(unsigned level) const;
579 };
580
581 /**
582 * ObjectClass assignment.
583 */
584 class Ass_OC : public Assignment {
585 private:
586 ObjectClass *right;
587
588 virtual Assignment* new_instance0();
589 /// Copy constructor for clone() only
590 Ass_OC(const Ass_OC& p);
591 /// Assignment disabled
592 Ass_OC& operator=(const Ass_OC& p);
593 public:
594 Ass_OC(Identifier *p_id, Ass_pard *p_ass_pard, ObjectClass *p_right);
595 virtual ~Ass_OC();
596 virtual Ass_OC* clone() const
597 {return new Ass_OC(*this);}
598 virtual void set_fullname(const string& p_fullname);
599 virtual void set_my_scope(Scope *p_scope);
600 virtual void set_right_scope(Scope *p_scope);
601 virtual void chk();
602 virtual Setting* get_Setting() {return get_ObjectClass();}
603 virtual ObjectClass* get_ObjectClass();
604 virtual void generate_code(output_struct *target, bool clean_up = false);
605 virtual void generate_code(CodeGenHelper& cgh);
606 virtual void dump(unsigned level) const;
607 };
608
609 /**
610 * Object assignment.
611 */
612 class Ass_O : public Assignment {
613 private:
614 ObjectClass *left;
615 Object *right;
616
617 virtual Assignment* new_instance0();
618 /// Copy constructor for clone() only
619 Ass_O(const Ass_O& p);
620 /// Assignment disabled
621 Ass_O& operator=(const Ass_O& p);
622 public:
623 Ass_O(Identifier *p_id, Ass_pard *p_ass_pard,
624 ObjectClass *p_left, Object *p_right);
625 virtual ~Ass_O();
626 virtual Ass_O* clone() const
627 {return new Ass_O(*this);}
628 virtual void set_fullname(const string& p_fullname);
629 virtual void set_my_scope(Scope *p_scope);
630 virtual void set_right_scope(Scope *p_scope);
631 virtual Setting* get_Setting() {return get_Object();}
632 //virtual ObjectClass* get_ObjectClass();
633 virtual Object* get_Object();
634 virtual void chk();
635 virtual void generate_code(output_struct *target, bool clean_up = false);
636 virtual void generate_code(CodeGenHelper& cgh);
637 virtual void dump(unsigned level) const;
638 };
639
640 /**
641 * ObjectSet assignment.
642 */
643 class Ass_OS : public Assignment {
644 private:
645 ObjectClass *left;
646 ObjectSet *right;
647
648 virtual Assignment* new_instance0();
649 /// Copy constructor for clone() only
650 Ass_OS(const Ass_OS& p);
651 /// Assignment disabled
652 Ass_OS& operator=(const Ass_OS& p);
653 public:
654 Ass_OS(Identifier *p_id, Ass_pard *p_ass_pard,
655 ObjectClass *p_left, ObjectSet *p_right);
656 virtual ~Ass_OS();
657 virtual Ass_OS* clone() const
658 {return new Ass_OS(*this);}
659 virtual void set_fullname(const string& p_fullname);
660 virtual void set_my_scope(Scope *p_scope);
661 virtual void set_right_scope(Scope *p_scope);
662 virtual Setting* get_Setting() {return get_ObjectSet();}
663 //virtual ObjectClass* get_ObjectClass();
664 virtual ObjectSet* get_ObjectSet();
665 virtual void chk();
666 virtual void generate_code(output_struct *target, bool clean_up = false);
667 virtual void generate_code(CodeGenHelper& cgh);
668 virtual void dump(unsigned level) const;
669 };
670
671 /** @} end of AST group */
672
673 } // namespace Asn
674
675 #endif // _Asn_AST_HH
This page took 0.05246 seconds and 4 git commands to generate.