Sync with 5.2.0
[deliverable/titan.core.git] / compiler2 / asn1 / AST_asn1.hh
CommitLineData
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#ifndef _Asn_AST_HH
9#define _Asn_AST_HH
10
11#include "../AST.hh"
12#include "../Int.hh"
13#include "Object0.hh"
14
15namespace Common {
16class CodeGenHelper;
17}
18
19/**
20 * This namespace contains things related to ASN.1 compiler.
21 */
22namespace 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! */
af710487 132 virtual Ass_pard* get_ass_pard() const { return ass_pard; }
970ed795
EL
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
af710487 247 /** Generates JSON schema segments for the types defined in the modules,
248 * and references to these types.
249 *
250 * @param json JSON document containing the main schema, schema segments for
251 * the types will be inserted here
252 * @param json_refs map of JSON documents containing the references to each type */
253 virtual void generate_json_schema(JSON_Tokenizer& json, map<Type*, JSON_Tokenizer>& json_refs);
970ed795
EL
254 };
255
256 /**
257 * SymbolList.
258 * Used only while parsing a module (and building the AST).
259 */
260 class Symbols : public Node {
261 friend class Exports;
262 friend class ImpMod;
263 friend class Imports;
264 private:
265 /** string => Identifier container */
266 typedef map<string, Identifier> syms_t;
267 /** temporary container for symbols (no uniqueness checks) */
268 vector<Identifier> syms_v;
269 /** the symbols */
270 syms_t syms_m;
271 private:
272 /** Copy constructor not implemented. */
273 Symbols(const Symbols&);
274 /** Assignment not implemented. */
275 Symbols& operator=(const Symbols&);
276 public:
277 /** Default constructor. */
278 Symbols() : Node(), syms_v(), syms_m() { }
279 /** Destructor. */
280 virtual ~Symbols();
281 /** Not implemented. */
282 virtual Symbols* clone() const;
283 /** Adds \a p_id to the symlist. */
284 void add_sym(Identifier *p_id);
285 /** Ensures uniqueness of identifiers in syms */
286 void chk_uniq(const Location& p_loc);
287 };
288
289 /**
290 * Exported symbols of a module.
291 */
292 class Exports : public Node, public Location {
293 private:
294 bool checked;
295 /** string => Identifier container */
296 /** my module */
297 Module *my_mod;
298 /** exports all */
299 bool expall;
300 /** exported symbols */
301 Symbols *symbols;
302 private:
303 /** Copy constructor not implemented. */
304 Exports(const Exports&);
305 /** Assignment not implemented */
306 Exports& operator=(const Exports&);
307 public:
308 /** Constructor used when the module exports all. */
309 Exports(bool p_expall=false);
310 /** Constructor used when there is a SymbolList. */
311 Exports(Symbols *p_symlist);
312 /** Destructor. */
313 virtual ~Exports();
314 /** Not implemented. */
315 virtual Exports* clone() const;
316 /** Sets the internal pointer to its module. */
317 void set_my_mod(Module *p_mod) {my_mod=p_mod;}
318 /** Returns true if a symbol with identifier \a p_id is
319 * exported. */
320 bool exports_sym(const Identifier& p_id);
321 /** Checks whether the exported symbols are defined in the module. */
322 void chk_exp();
323 };
324
325 /**
326 * Imported module.
327 */
328 class ImpMod : public Node, public Location {
329 friend class Imports;
330 private:
331 /** my module */
332 Module *my_mod;
333 /** identifier of imported module */
334 Identifier *modid;
335 /** pointer to the imported module */
336 Common::Module *mod;
337 /** imported symbols */
338 Symbols *symbols;
339 private:
340 /** Copy constructor not implemented. */
341 ImpMod(const ImpMod&);
342 /** Assignment not implemented */
343 ImpMod& operator=(const ImpMod&);
344 public:
345 /** Constructor. */
346 ImpMod(Identifier *p_modid, Symbols *p_symlist);
347 /** Destructor. */
348 virtual ~ImpMod();
349 /** Not implemented. */
350 virtual ImpMod* clone() const;
351 /** Sets the internal pointer to its module. */
352 void set_my_mod(Module *p_mod) {my_mod=p_mod;}
353 /** Gets the module id of imported module. */
354 const Identifier& get_modid() const {return *modid;}
355 void set_mod(Common::Module *p_mod) { mod = p_mod; }
356 Common::Module *get_mod() const { return mod; }
357 bool has_sym(const Identifier& p_id) const;
358 /** Checks whether the imported symbols exist. Appends error
359 * messages when needed. */
360 void chk_imp(ReferenceChain& refch);
361 void generate_code(output_struct *target);
362 };
363
364 /**
365 * Imported modules.
366 */
367 class Imports : public Node, public Location {
368 friend class Module;
369 private:
370 /** my module */
371 Module *my_mod;
372 /** imported modules */
373 vector<Asn::ImpMod> impmods_v;
374 map<string, Asn::ImpMod> impmods;
375 /** these symbols are mentioned only once in IMPORTS */
376 map<string, Common::Module> impsyms_1;
377 /** these symbols are mentioned more than once in IMPORTS. This
378 * map is used as a set. */
379 map<string, void> impsyms_m;
380 /** Indicates whether the import list has been checked. */
381 bool checked;
382 /** Indicates whether the owner module is a part of a circular import
383 * chain. */
384 bool is_circular;
385 private:
386 /** Copy constructor not implemented. */
387 Imports(const Imports&);
388 /** Assignment not implemented */
389 Imports& operator=(const Imports&);
390 public:
391 /** Constructor. */
392 Imports()
393 : Node(), my_mod(0), impmods_v(), impmods(), impsyms_1(), impsyms_m(),
394 checked(false), is_circular(false) {}
395 /** Destructor. */
396 virtual ~Imports();
397 /** Not implemented. */
398 virtual Imports* clone() const;
399 /** Adds the ImpMod. If a module with that id already exists,
400 * throws an Error_AST_uniq exception. */
401 void add_impmod(ImpMod *p_impmod);
402 /** Sets the internal pointer my_mod to \a p_mod. */
403 void set_my_mod(Module *p_mod);
404 /** Recursive check. Checks whether the modules with the id exist,
405 * then checks that ImpMod. */
406 void chk_imp(ReferenceChain& refch);
407 /** Returns whether symbols from module with identifier \a p_id
408 * are imported. */
409 bool has_impmod_withId(const Identifier& p_id) const
410 { return impmods.has_key(p_id.get_name()); }
411 const ImpMod *get_impmod_byId(const Identifier& p_id) const
412 { return impmods[p_id.get_name()]; }
413 /** Returns whether a symbol with identifier \a p_id is imported from one
414 * or more modules */
415 bool has_impsym_withId(const Identifier& p_id) const;
416 void get_imported_mods(Module::module_set_t& p_imported_mods);
417 bool get_is_circular() const { return is_circular; }
418 void generate_code(output_struct *target);
419 };
420
421 /**
422 * Undefined assignment.
423 */
424 class Ass_Undef : public Assignment {
425 private:
426 Node *left, *right;
427 Scope *right_scope;
428 /** the classified assignment */
429 Assignment *ass;
430
431 void classify_ass(ReferenceChain *refch=0);
432 virtual Assignment* new_instance0();
433 Ass_Undef(const Ass_Undef& p);
434 /// Assignment disabled
435 Ass_Undef& operator=(const Ass_Undef& p);
436 public:
437 Ass_Undef(Identifier *p_id, Ass_pard *p_ass_pard,
438 Node *p_left, Node *p_right);
439 virtual ~Ass_Undef();
440 virtual Assignment* clone() const;
441 virtual asstype_t get_asstype() const;
442 virtual void set_fullname(const string& p_fullname);
443 virtual void set_my_scope(Scope *p_scope);
444 virtual void set_right_scope(Scope *p_scope);
445 virtual bool is_asstype(asstype_t p_asstype, ReferenceChain* refch=0);
af710487 446 virtual Ass_pard* get_ass_pard() const;
970ed795
EL
447 virtual Setting* get_Setting();
448 virtual Type* get_Type();
449 virtual Value* get_Value();
450 virtual ValueSet* get_ValueSet();
451 virtual ObjectClass* get_ObjectClass();
452 virtual Object* get_Object();
453 virtual ObjectSet* get_ObjectSet();
454 virtual void chk();
455 virtual void generate_code(output_struct *target, bool clean_up = false);
456 virtual void generate_code(CodeGenHelper& cgh);
457 virtual void dump(unsigned level) const;
458 private:
459 bool _error_if_pard();
460 };
461
462 /**
463 * Erroneous assignment.
464 */
465 class Ass_Error : public Assignment {
466 private:
467 Setting *setting_error;
468 Type *type_error;
469 Value *value_error;
470
471 /// Copy constructor not implemented (even though clone works)
472 Ass_Error(const Ass_Error& p);
473 /// Assignment disabled
474 Ass_Error& operator=(const Ass_Error& p);
475 virtual Assignment* new_instance0();
476 public:
477 Ass_Error(Identifier *p_id, Ass_pard *p_ass_pard);
478 virtual ~Ass_Error();
479 virtual Assignment* clone() const;
480 virtual bool is_asstype(asstype_t p_asstype, ReferenceChain* refch=0);
481 virtual void set_right_scope(Scope *) {}
482 virtual Setting* get_Setting();
483 virtual Type* get_Type();
484 virtual Value* get_Value();
485 virtual ValueSet* get_ValueSet();
486 virtual ObjectClass* get_ObjectClass();
487 virtual Object* get_Object();
488 virtual ObjectSet* get_ObjectSet();
489 virtual void chk();
490 virtual void dump(unsigned level) const;
491 };
492
493 /**
494 * Type assignment.
495 */
496 class Ass_T : public Assignment {
497 private:
498 Type *right;
499
500 virtual Assignment* new_instance0();
501 /// Copy constructor for clone() only
502 Ass_T(const Ass_T& p);
503 /// Assignment disabled
504 Ass_T& operator=(const Ass_T& p);
505 public:
506 Ass_T(Identifier *p_id, Ass_pard *p_ass_pard, Type *p_right);
507 virtual ~Ass_T();
508 virtual Ass_T* clone() const
509 {return new Ass_T(*this);}
510 virtual void set_fullname(const string& p_fullname);
511 virtual void set_my_scope(Scope *p_scope);
512 virtual void set_right_scope(Scope *p_scope);
513 virtual Setting* get_Setting() {return get_Type();}
514 virtual Type* get_Type();
515 virtual void chk();
516 virtual void generate_code(output_struct *target, bool clean_up = false);
517 virtual void generate_code(CodeGenHelper& cgh);
518 virtual void dump(unsigned level) const;
519 };
520
521 /**
522 * Value assignment.
523 */
524 class Ass_V : public Assignment {
525 private:
526 Type *left;
527 Value *right;
528
529 virtual Assignment* new_instance0();
530 /// Copy constructor for clone() only
531 Ass_V(const Ass_V& p);
532 /// Assignment disabled
533 Ass_V& operator=(const Ass_V& p);
534 public:
535 Ass_V(Identifier *p_id, Ass_pard *p_ass_pard,
536 Type *p_left, Value *p_right);
537 virtual ~Ass_V();
538 virtual Ass_V* clone() const
539 {return new Ass_V(*this);}
540 virtual void set_fullname(const string& p_fullname);
541 virtual void set_my_scope(Scope *p_scope);
542 virtual void set_right_scope(Scope *p_scope);
543 virtual Setting* get_Setting() {return get_Value();}
544 virtual Type* get_Type();
545 virtual Value* get_Value();
546 virtual void chk();
547 virtual void generate_code(output_struct *target, bool clean_up = false);
548 virtual void generate_code(CodeGenHelper& cgh);
549 virtual void dump(unsigned level) const;
550 };
551
552 /**
553 * ValueSet assignment.
554 */
555 class Ass_VS : public Assignment {
556 private:
557 Type *left;
558 Block *right;
559 Scope *right_scope;
560
561 virtual Assignment* new_instance0();
562 /// Copy constructor for clone() only
563 Ass_VS(const Ass_VS& p);
564 /// Assignment disabled
565 Ass_VS& operator=(const Ass_VS& p);
566 public:
567 Ass_VS(Identifier *p_id, Ass_pard *p_ass_pard,
568 Type *p_left, Block *p_right);
569 virtual ~Ass_VS();
570 virtual Ass_VS* clone() const
571 {return new Ass_VS(*this);}
572 virtual void set_fullname(const string& p_fullname);
573 virtual void set_my_scope(Scope *p_scope);
574 virtual void set_right_scope(Scope *p_scope);
575 virtual Setting* get_Setting() {return get_Type();}
576 virtual Type* get_Type();
577 virtual void chk();
578 virtual void generate_code(output_struct *target, bool clean_up = false);
579 virtual void generate_code(CodeGenHelper& cgh);
580 virtual void dump(unsigned level) const;
581 };
582
583 /**
584 * ObjectClass assignment.
585 */
586 class Ass_OC : public Assignment {
587 private:
588 ObjectClass *right;
589
590 virtual Assignment* new_instance0();
591 /// Copy constructor for clone() only
592 Ass_OC(const Ass_OC& p);
593 /// Assignment disabled
594 Ass_OC& operator=(const Ass_OC& p);
595 public:
596 Ass_OC(Identifier *p_id, Ass_pard *p_ass_pard, ObjectClass *p_right);
597 virtual ~Ass_OC();
598 virtual Ass_OC* clone() const
599 {return new Ass_OC(*this);}
600 virtual void set_fullname(const string& p_fullname);
601 virtual void set_my_scope(Scope *p_scope);
602 virtual void set_right_scope(Scope *p_scope);
603 virtual void chk();
604 virtual Setting* get_Setting() {return get_ObjectClass();}
605 virtual ObjectClass* get_ObjectClass();
606 virtual void generate_code(output_struct *target, bool clean_up = false);
607 virtual void generate_code(CodeGenHelper& cgh);
608 virtual void dump(unsigned level) const;
609 };
610
611 /**
612 * Object assignment.
613 */
614 class Ass_O : public Assignment {
615 private:
616 ObjectClass *left;
617 Object *right;
618
619 virtual Assignment* new_instance0();
620 /// Copy constructor for clone() only
621 Ass_O(const Ass_O& p);
622 /// Assignment disabled
623 Ass_O& operator=(const Ass_O& p);
624 public:
625 Ass_O(Identifier *p_id, Ass_pard *p_ass_pard,
626 ObjectClass *p_left, Object *p_right);
627 virtual ~Ass_O();
628 virtual Ass_O* clone() const
629 {return new Ass_O(*this);}
630 virtual void set_fullname(const string& p_fullname);
631 virtual void set_my_scope(Scope *p_scope);
632 virtual void set_right_scope(Scope *p_scope);
633 virtual Setting* get_Setting() {return get_Object();}
634 //virtual ObjectClass* get_ObjectClass();
635 virtual Object* get_Object();
636 virtual void chk();
637 virtual void generate_code(output_struct *target, bool clean_up = false);
638 virtual void generate_code(CodeGenHelper& cgh);
639 virtual void dump(unsigned level) const;
640 };
641
642 /**
643 * ObjectSet assignment.
644 */
645 class Ass_OS : public Assignment {
646 private:
647 ObjectClass *left;
648 ObjectSet *right;
649
650 virtual Assignment* new_instance0();
651 /// Copy constructor for clone() only
652 Ass_OS(const Ass_OS& p);
653 /// Assignment disabled
654 Ass_OS& operator=(const Ass_OS& p);
655 public:
656 Ass_OS(Identifier *p_id, Ass_pard *p_ass_pard,
657 ObjectClass *p_left, ObjectSet *p_right);
658 virtual ~Ass_OS();
659 virtual Ass_OS* clone() const
660 {return new Ass_OS(*this);}
661 virtual void set_fullname(const string& p_fullname);
662 virtual void set_my_scope(Scope *p_scope);
663 virtual void set_right_scope(Scope *p_scope);
664 virtual Setting* get_Setting() {return get_ObjectSet();}
665 //virtual ObjectClass* get_ObjectClass();
666 virtual ObjectSet* get_ObjectSet();
667 virtual void chk();
668 virtual void generate_code(output_struct *target, bool clean_up = false);
669 virtual void generate_code(CodeGenHelper& cgh);
670 virtual void dump(unsigned level) const;
671 };
672
673 /** @} end of AST group */
674
675} // namespace Asn
676
677#endif // _Asn_AST_HH
This page took 0.064518 seconds and 5 git commands to generate.