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 ///////////////////////////////////////////////////////////////////////////////
20 * This namespace contains things related to ASN.1 compiler.
32 * This is used when parsing internal stuff (e.g., EXTERNAL,
33 * EMBEDDED PDV, etc.).
35 extern Assignments *parsed_assignments;
37 using namespace Common;
55 /* not defined, only declared in this file */
65 * Struct to represent default tagging.
69 EXPLICIT, /**< EXPLICIT TAGS */
70 IMPLICIT, /**< IMPLICIT TAGS */
71 AUTOMATIC /**< AUTOMATIC TAGS */
76 * Helper-class for parameterized assignments.
78 class Ass_pard : public Node {
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
91 map<Common::Module*, size_t> inst_cnts;
93 Ass_pard(const Ass_pard& p);
94 Ass_pard& operator=(const Ass_pard& p);
95 /** Split the formal parameterlist */
98 Ass_pard(Block *p_parlist_block);
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);
113 * Abstract class to represent different kinds of assignments.
115 class Assignment : public Common::Assignment {
116 protected: // many classes derived
120 Assignment(const Assignment& p);
121 Assignment& operator=(const Assignment& p);
122 virtual string get_genname() const;
123 virtual Assignment* new_instance0();
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();
142 virtual void dump(unsigned level) const;
146 * Class to store assignments. It also contains the ASN-related
147 * pre-defined (so-called "useful") stuff.
149 class Assignments : public Common::Assignments {
151 /** Special assignments. */
152 static Module *_spec_asss;
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);
160 /** The assignments. */
161 map<string, Assignment> asss_m;
162 vector<Assignment> asss_v;
163 /** Indicates whether the uniqueness of identifiers has been checked. */
166 Assignments(const Assignments&);
169 Assignments() : Common::Assignments(), asss_m(), asss_v(), checked(false)
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. */
187 /** Checks all assignments. */
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
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;
200 * Class to represent ASN-modules.
202 class Module : public Common::Module {
204 /** default tagging */
205 TagDefault::tagdef_t tagdef;
206 /** extensibility implied */
208 /** exported stuff */
210 /** imported stuff. */
212 /** assignments of the module */
215 /** Copy constructor not implemented. */
216 Module(const Module&);
217 /** Assignment not implemented */
218 Module& operator=(const Module&);
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);
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);
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);
243 virtual void dump(unsigned level) const;
244 void add_ass(Assignment *p_ass);
245 TagDefault::tagdef_t get_tagdef() const { return tagdef; }
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&);
251 /** Does nothing. ASN.1 modules can't contain functions. */
252 virtual void add_func_to_json_schema(map<Type*, JSON_Tokenizer>&) {}
257 * Used only while parsing a module (and building the AST).
259 class Symbols : public Node {
260 friend class Exports;
262 friend class Imports;
264 /** string => Identifier container */
265 typedef map<string, Identifier> syms_t;
266 /** temporary container for symbols (no uniqueness checks) */
267 vector<Identifier> syms_v;
271 /** Copy constructor not implemented. */
272 Symbols(const Symbols&);
273 /** Assignment not implemented. */
274 Symbols& operator=(const Symbols&);
276 /** Default constructor. */
277 Symbols() : Node(), syms_v(), syms_m() { }
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);
289 * Exported symbols of a module.
291 class Exports : public Node, public Location {
294 /** string => Identifier container */
299 /** exported symbols */
302 /** Copy constructor not implemented. */
303 Exports(const Exports&);
304 /** Assignment not implemented */
305 Exports& operator=(const Exports&);
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);
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
319 bool exports_sym(const Identifier& p_id);
320 /** Checks whether the exported symbols are defined in the module. */
327 class ImpMod : public Node, public Location {
328 friend class Imports;
332 /** identifier of imported module */
334 /** pointer to the imported module */
336 /** imported symbols */
339 /** Copy constructor not implemented. */
340 ImpMod(const ImpMod&);
341 /** Assignment not implemented */
342 ImpMod& operator=(const ImpMod&);
345 ImpMod(Identifier *p_modid, Symbols *p_symlist);
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);
366 class Imports : public Node, public Location {
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. */
381 /** Indicates whether the owner module is a part of a circular import
385 /** Copy constructor not implemented. */
386 Imports(const Imports&);
387 /** Assignment not implemented */
388 Imports& operator=(const Imports&);
392 : Node(), my_mod(0), impmods_v(), impmods(), impsyms_1(), impsyms_m(),
393 checked(false), is_circular(false) {}
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
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
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);
421 * Undefined assignment.
423 class Ass_Undef : public Assignment {
427 /** the classified assignment */
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);
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();
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;
457 bool _error_if_pard();
461 * Erroneous assignment.
463 class Ass_Error : public Assignment {
465 Setting *setting_error;
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();
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();
488 virtual void dump(unsigned level) const;
494 class Ass_T : public Assignment {
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);
504 Ass_T(Identifier *p_id, Ass_pard *p_ass_pard, Type *p_right);
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();
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;
522 class Ass_V : public Assignment {
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);
533 Ass_V(Identifier *p_id, Ass_pard *p_ass_pard,
534 Type *p_left, Value *p_right);
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();
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;
551 * ValueSet assignment.
553 class Ass_VS : public Assignment {
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);
565 Ass_VS(Identifier *p_id, Ass_pard *p_ass_pard,
566 Type *p_left, Block *p_right);
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();
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;
582 * ObjectClass assignment.
584 class Ass_OC : public Assignment {
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);
594 Ass_OC(Identifier *p_id, Ass_pard *p_ass_pard, ObjectClass *p_right);
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);
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;
612 class Ass_O : public Assignment {
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);
623 Ass_O(Identifier *p_id, Ass_pard *p_ass_pard,
624 ObjectClass *p_left, Object *p_right);
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();
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;
641 * ObjectSet assignment.
643 class Ass_OS : public Assignment {
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);
654 Ass_OS(Identifier *p_id, Ass_pard *p_ass_pard,
655 ObjectClass *p_left, ObjectSet *p_right);
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();
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;
671 /** @} end of AST group */
675 #endif // _Asn_AST_HH