MicroModelicaCCompiler  4.5.3
class.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #include "class.hpp"
21 
22 #include <stddef.h>
23 #include <iostream>
24 #include <list>
25 
26 #include "../util/ast_util.hpp"
27 #include "ast_builder.hpp"
28 #include "composition.hpp"
29 #include "element.hpp"
30 
31 AST_Class_::AST_Class_(AST_String n, AST_Composition comp)
32  : _name(n), _composition(comp), _father(nullptr), _basic(false), _encapsulated(false), _final(false), _prefix()
33 {
37 }
38 
40 {
41  delete _extends_list;
42  delete _sub_classes;
43  delete _components;
44  if (_composition != nullptr) {
45  delete _composition;
46  }
47  if (_name != nullptr) {
48  delete _name;
49  }
50  if (_father != nullptr) {
51  delete _father;
52  }
53 }
54 
55 CLASSP_PRINTER_IMP(AST_Class);
56 
57 AST_String AST_Class_::name() const { return _name; }
58 
59 AST_Composition AST_Class_::composition() const { return _composition; }
60 
61 AST_Element_ComponentList AST_Class_::getComponents() { return _components; }
62 
63 void AST_Class_::addComponent(AST_Element_Component c) { AST_ListAppend(_components, c); }
64 
65 bool AST_Class_::isBasic() { return _basic; }
66 
67 void AST_Class_::setComposition(AST_Composition c) { _composition = c; }
68 
69 void AST_Class_::setBasic() { _basic = true; }
70 
71 ostream &operator<<(ostream &ret, const AST_Class_ &cl)
72 {
73  MAKE_SPACE;
74  switch (cl.prefix()) {
75  case CP_CLASS:
76  ret << "class ";
77  break;
78  case CP_MODEL:
79  ret << "model ";
80  break;
81  case CP_FUNCTION:
82  ret << "function ";
83  break;
84  }
85  ret << cl.name() << endl;
87  ret << cl.composition();
88  END_BLOCK;
89  MAKE_SPACE;
90  ret << "end " << cl.name() << ";" << endl;
91  return ret;
92 }
93 
94 void AST_Class_::addClass(AST_Class c)
95 {
97  c->setFather(this);
98 }
99 
100 AST_ClassList AST_Class_::getClasses() { return _sub_classes; }
101 
102 void AST_Class_::setFather(AST_Class c) { _father = c; }
103 
104 bool AST_Class_::hasFather() { return _father != nullptr; }
105 
106 AST_Class AST_Class_::father() const { return _father; }
107 
109 
110 AST_StringList AST_Class_::getExtends() { return _extends_list; }
111 
112 void AST_Class_::setFinal() { _final = true; }
113 
114 bool AST_Class_::isFinal() { return _final; }
115 
117 
119 
121 
123 
124 bool AST_Class_::hasExtends() { return !_extends_list->empty(); }
125 
127 {
128  AST_ClassListIterator class_it;
129  foreach (class_it, _sub_classes) {
130  current_element(class_it)->accept(visitor);
131  }
132  visitor->visit(this);
133  AST_Element_ComponentListIterator component_it;
134  foreach (component_it, _components) {
135  current_element(component_it)->accept(visitor);
136  }
137  _composition->accept(visitor);
138  visitor->leave(this);
139 }
140 
141 bool AST_Class_::hasElementComponentList() { return _components->size() > 0; }
AST_Class_::setPrefixes
void setPrefixes(AST_ClassPrefix cp)
Definition: class.cpp:120
AST_Class_::_sub_classes
AST_ClassList _sub_classes
Definition: class.hpp:79
ast_builder.hpp
AST_Class_::_composition
AST_Composition _composition
Definition: class.hpp:78
AST_Class_::_name
AST_String _name
Definition: class.hpp:77
AST_Class_::_prefix
AST_ClassPrefix _prefix
Definition: class.hpp:82
END_BLOCK
#define END_BLOCK
Definition: ast_types.hpp:33
AST_Class_::getClasses
AST_ClassList getClasses()
Definition: class.cpp:100
AST_Class_::composition
AST_Composition composition() const
Definition: class.cpp:59
AST_Class_::_encapsulated
AST_Boolean _encapsulated
Definition: class.hpp:81
AST_Class_::setComposition
void setComposition(AST_Composition c)
Definition: class.cpp:67
AST_Class_::getExtends
AST_StringList getExtends()
Definition: class.cpp:110
element.hpp
composition.hpp
AST_Class_::addExtends
void addExtends(AST_String e)
Definition: class.cpp:108
AST_String
string * AST_String
Definition: ast_types.hpp:46
newAST_StringList
AST_StringList newAST_StringList()
Definition: ast_builder.cpp:315
AST_Class_::setFinal
void setFinal()
Definition: class.cpp:112
AST_Class_
Definition: class.hpp:28
BEGIN_BLOCK
#define BEGIN_BLOCK
Definition: ast_types.hpp:32
AST_Class_::AST_Class_
AST_Class_(AST_String n, AST_Composition comp)
Definition: class.cpp:31
AST_Class_::setFather
void setFather(AST_Class c)
Definition: class.cpp:102
MAKE_SPACE
#define MAKE_SPACE
Definition: ast_types.hpp:30
AST_Class_::addComponent
void addComponent(AST_Element_Component c)
Definition: class.cpp:63
AST_ClassPrefix
int AST_ClassPrefix
Definition: ast_types.hpp:51
AST_Class_::isEncapsulated
bool isEncapsulated()
Definition: class.cpp:118
AST_Class_::_final
AST_Boolean _final
Definition: class.hpp:81
AST_Class_::_basic
AST_Boolean _basic
Definition: class.hpp:81
AST_Class_::hasElementComponentList
bool hasElementComponentList()
Definition: class.cpp:141
AST_Class_::_father
AST_Class _father
Definition: class.hpp:80
AST_ListAppend
list< T1 > * AST_ListAppend(list< T1 > *l, T1 e)
Definition: ast_types.hpp:240
AST_Class_::prefix
AST_ClassPrefix prefix() const
Definition: class.cpp:122
AST_Class_::isBasic
bool isBasic()
Definition: class.cpp:65
AST_Class_::getComponents
AST_Element_ComponentList getComponents()
Definition: class.cpp:61
AST_Class_::father
AST_Class father() const
Definition: class.cpp:106
AST_Class_::hasExtends
bool hasExtends()
Definition: class.cpp:124
CLASSP_PRINTER_IMP
CLASSP_PRINTER_IMP(AST_Class)
operator<<
ostream & operator<<(ostream &ret, const AST_Class_ &cl)
Definition: class.cpp:71
newAST_ClassList
AST_ClassList newAST_ClassList()
Definition: ast_builder.cpp:99
class.hpp
AST_Class_::isFinal
bool isFinal()
Definition: class.cpp:114
AST_Visitor::visit
virtual void visit(AST_Class x)=0
AST_Class_::addClass
void addClass(AST_Class c)
Definition: class.cpp:94
AST_Class_::name
AST_String name() const
Definition: class.cpp:57
newAST_Element_ComponentList
AST_Element_ComponentList newAST_Element_ComponentList()
Definition: ast_builder.cpp:257
AST_Class_::_extends_list
AST_StringList _extends_list
Definition: class.hpp:76
CP_FUNCTION
@ CP_FUNCTION
Definition: ast_types.hpp:225
AST_Visitor
Definition: ast_util.hpp:224
AST_Class_::_components
AST_Element_ComponentList _components
Definition: class.hpp:75
AST_Class_::hasFather
bool hasFather()
Definition: class.cpp:104
CP_CLASS
@ CP_CLASS
Definition: ast_types.hpp:214
AST_Visitor::leave
virtual void leave(AST_Class x)=0
AST_Class_::~AST_Class_
~AST_Class_()
Definition: class.cpp:39
AST_Class_::accept
void accept(AST_Visitor *visitor)
Definition: class.cpp:126
CP_MODEL
@ CP_MODEL
Definition: ast_types.hpp:215
AST_Class_::setEncapsulated
void setEncapsulated()
Definition: class.cpp:116
AST_Class_::setBasic
void setBasic()
Definition: class.cpp:69
current_element
#define current_element(it)
Definition: ast_types.hpp:34