ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnc::ArtifactGeneratorCppCode Class Reference

The ACL C++ artifact source code producer. More...

#include <ArtifactGeneratorCppCode.h>

Collaboration diagram for nnc::ArtifactGeneratorCppCode:

Public Member Functions

 ArtifactGeneratorCppCode (std::ostream &out)
 
void visit (const ArtifactLiteral *node) override
 
void visit (const ArtifactId *node) override
 
void visit (const ArtifactRef *node) override
 
void visit (const ArtifactDeref *node) override
 
void visit (const ArtifactVariable *node) override
 
void visit (const ArtifactFunctionCall *node) override
 
void visit (const ArtifactUnaryExpr *node) override
 
void visit (const ArtifactBinaryExpr *node) override
 
void visit (const ArtifactIndex *node) override
 
void visit (const ArtifactRet *node) override
 
void visit (const ArtifactBreak *node) override
 
void visit (const ArtifactCont *node) override
 
void visit (const ArtifactBlock *node) override
 
void visit (const ArtifactForLoop *node) override
 
void visit (const ArtifactIf *node) override
 
void visit (const ArtifactFunction *node) override
 
void visit (const ArtifactClass *node) override
 
void visit (const ArtifactClassVariable *node) override
 
void visit (const ArtifactClassFunction *node) override
 
void visit (const ArtifactModule *node) override
 
- Public Member Functions inherited from nnc::IArtifactGenerator
virtual ~IArtifactGenerator ()=default
 

Detailed Description

The ACL C++ artifact source code producer.

Definition at line 31 of file ArtifactGeneratorCppCode.h.

Constructor & Destructor Documentation

◆ ArtifactGeneratorCppCode()

nnc::ArtifactGeneratorCppCode::ArtifactGeneratorCppCode ( std::ostream &  out)
explicit

Definition at line 29 of file ArtifactGeneratorCppCode.cpp.

29: _out(out) {}

Member Function Documentation

◆ visit() [1/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactBinaryExpr node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 119 of file ArtifactGeneratorCppCode.cpp.

120{
121 static const char *bin_op_str[] = {"==", "!=", "<", "<=", ">", ">=", "=", "+",
122 "-", "*", "/", "+=", "-=", "*=", "/="};
123 node->getLeft()->accept(this);
124 _out << " " << bin_op_str[static_cast<int>(node->getOp())] << " ";
125 node->getRight()->accept(this);
126}

References nnc::ArtifactBinaryExpr::getLeft(), nnc::ArtifactBinaryExpr::getOp(), and nnc::ArtifactBinaryExpr::getRight().

◆ visit() [2/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactBlock node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 146 of file ArtifactGeneratorCppCode.cpp.

147{
148 _out << " {" << endl;
149 ++_ind;
150
151 for (const auto &st : node->getStatements())
152 {
153 _out << _ind;
154 st->accept(this);
155
156 if (!st->isBlock())
157 _out << ";" << endl;
158 }
159
160 --_ind;
161 _out << _ind << "}" << endl;
162}

References nnc::ArtifactBlock::getStatements().

◆ visit() [3/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactBreak node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 142 of file ArtifactGeneratorCppCode.cpp.

142{ _out << "break"; }

◆ visit() [4/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactClass node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 204 of file ArtifactGeneratorCppCode.cpp.

205{
206 // Generate a public default constructor here.
207 _out << node->name() << "::" << node->name() << "()";
208
209 if (!node->privateVariables().empty())
210 {
211 _out << " : ";
212 bool add_delim = false;
213
214 for (const auto &v : node->privateVariables())
215 {
216 if (add_delim)
217 _out << ",\n";
218
219 v->accept(this);
220 add_delim = true;
221 }
222 }
223
224 // TODO add constructors of public variables
225
226 node->getConstrBlock()->accept(this);
227 _out << endl;
228
229 // Then generate the other stuff.
230
231 for (const auto &e : node->publicFunctions())
232 e->accept(this);
233
234 for (const auto &e : node->privateFunctions())
235 e->accept(this);
236}

References nnc::ArtifactBlock::accept(), nnc::ArtifactClass::getConstrBlock(), nnc::ArtifactNamed::name(), nnc::ArtifactClass::privateFunctions(), nnc::ArtifactClass::privateVariables(), and nnc::ArtifactClass::publicFunctions().

◆ visit() [5/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactClassFunction node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 255 of file ArtifactGeneratorCppCode.cpp.

256{
257 _out << node->getRetTypeName();
258
259 if (!node->getRetTypeName().empty())
260 _out << " ";
261
262 _out << node->owner()->name() << "::" << node->name() << "(";
263 bool add_comma = false;
264
265 for (const auto &p : node->getParameters())
266 {
267 if (add_comma)
268 _out << ", ";
269
270 p->accept(this);
271 add_comma = true;
272 }
273
274 _out << ")";
275 node->getBlock()->accept(this);
276 _out << endl;
277}

References nnc::ArtifactBlock::accept(), nnc::ArtifactFunction::getBlock(), nnc::ArtifactFunction::getParameters(), nnc::ArtifactFunction::getRetTypeName(), nnc::ArtifactNamed::name(), and nnc::ArtifactClassMember::owner().

◆ visit() [6/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactClassVariable node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 238 of file ArtifactGeneratorCppCode.cpp.

239{
240 _out << node->name() << "(";
241 bool add_comma = false;
242
243 for (const auto &i : node->getInitializers())
244 {
245 if (add_comma)
246 _out << ", ";
247
248 i->accept(this);
249 add_comma = true;
250 }
251
252 _out << ")";
253}

References nnc::ArtifactVariable::getInitializers(), and nnc::ArtifactNamed::name().

◆ visit() [7/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactCont node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 144 of file ArtifactGeneratorCppCode.cpp.

144{ _out << "continue"; }

◆ visit() [8/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactDeref node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 41 of file ArtifactGeneratorCppCode.cpp.

42{
43 _out << "*";
44 node->obj()->accept(this);
45}

References nnc::ArtifactDeref::obj().

◆ visit() [9/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactForLoop node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 164 of file ArtifactGeneratorCppCode.cpp.

165{
166 _out << "for(";
167
168 if (node->getInit())
169 node->getInit()->accept(this);
170
171 _out << "; ";
172
173 if (node->getCond())
174 node->getCond()->accept(this);
175
176 _out << "; ";
177
178 if (node->getIter())
179 node->getIter()->accept(this);
180
181 _out << ")";
182 node->getBlock()->accept(this);
183}

References nnc::ArtifactBlock::accept(), nnc::ArtifactForLoop::getBlock(), nnc::ArtifactForLoop::getCond(), nnc::ArtifactForLoop::getInit(), and nnc::ArtifactForLoop::getIter().

◆ visit() [10/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactFunction node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 199 of file ArtifactGeneratorCppCode.cpp.

200{
201 // TODO implement this function
202}

◆ visit() [11/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactFunctionCall node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 76 of file ArtifactGeneratorCppCode.cpp.

77{
78 static const char *call_type_str[] = {".", "->", "::"};
79
80 if (node->on())
81 {
82 node->on()->accept(this);
83 _out << call_type_str[static_cast<int>(node->callType())];
84 }
85
86 _out << node->funcName();
87 _out << "(";
88 bool add_comma = false;
89
90 for (const auto &par : node->paramList())
91 {
92 if (add_comma)
93 _out << ", ";
94
95 par->accept(this);
96 add_comma = true;
97 }
98
99 _out << ")";
100}

References nnc::ArtifactFunctionCall::callType(), nnc::ArtifactFunctionCall::funcName(), nnc::ArtifactFunctionCall::on(), and nnc::ArtifactFunctionCall::paramList().

◆ visit() [12/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactId node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 33 of file ArtifactGeneratorCppCode.cpp.

33{ _out << node->name(); }

References nnc::ArtifactId::name().

◆ visit() [13/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactIf node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 185 of file ArtifactGeneratorCppCode.cpp.

186{
187 _out << "if(";
188 node->getCond()->accept(this);
189 _out << ")";
190 node->getBlock()->accept(this);
191
192 if (!node->getElseBlock()->getStatements().empty())
193 {
194 _out << _ind << "else";
195 node->getElseBlock()->accept(this);
196 }
197}

References nnc::ArtifactBlock::accept(), nnc::ArtifactIf::getBlock(), nnc::ArtifactIf::getCond(), nnc::ArtifactIf::getElseBlock(), and nnc::ArtifactBlock::getStatements().

◆ visit() [14/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactIndex node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 128 of file ArtifactGeneratorCppCode.cpp.

129{
130 node->getExpr()->accept(this);
131 _out << "[";
132 node->getInd()->accept(this);
133 _out << "]";
134}

References nnc::ArtifactIndex::getExpr(), and nnc::ArtifactIndex::getInd().

◆ visit() [15/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactLiteral node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 31 of file ArtifactGeneratorCppCode.cpp.

31{ _out << node->getValue(); }

References nnc::ArtifactLiteral::getValue().

◆ visit() [16/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactModule node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 279 of file ArtifactGeneratorCppCode.cpp.

280{
281 _out << "#include \"" << node->name() << ".h\"" << endl << endl;
282
283 for (const auto &i : node->sourceSysIncludes())
284 _out << "#include <" << i << ">" << endl;
285
286 if (!node->sourceSysIncludes().empty())
287 _out << endl;
288
289 for (const auto &i : node->sourceIncludes())
290 _out << "#include \"" << i << "\"" << endl;
291
292 if (!node->sourceIncludes().empty())
293 _out << endl;
294
295 _out.write(AclArtifactUtilities, sizeof(AclArtifactUtilities));
296 _out << endl;
297
298 for (const auto &e : node->entities())
299 e->accept(this);
300}

References nnc::ArtifactModule::entities(), nnc::ArtifactModule::name(), nnc::ArtifactModule::sourceIncludes(), and nnc::ArtifactModule::sourceSysIncludes().

◆ visit() [17/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactRef node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 35 of file ArtifactGeneratorCppCode.cpp.

36{
37 _out << "&";
38 node->obj()->accept(this);
39}

References nnc::ArtifactRef::obj().

◆ visit() [18/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactRet node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 136 of file ArtifactGeneratorCppCode.cpp.

137{
138 _out << "return ";
139 node->expr()->accept(this);
140}

References nnc::ArtifactRet::expr().

◆ visit() [19/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactUnaryExpr node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 102 of file ArtifactGeneratorCppCode.cpp.

103{
104 // The trailing space is intended in new and delete!
105 static const char *un_op_str[] = {"++", "--", "new ", "delete ", "++", "--"};
106
107 if (node->getOp() < ArtifactUnOp::postIncr)
108 {
109 _out << un_op_str[static_cast<int>(node->getOp())];
110 node->getExpr()->accept(this);
111 }
112 else
113 {
114 node->getExpr()->accept(this);
115 _out << un_op_str[static_cast<int>(node->getOp())];
116 }
117}

References nnc::ArtifactUnaryExpr::getExpr(), nnc::ArtifactUnaryExpr::getOp(), and nnc::postIncr.

◆ visit() [20/20]

void nnc::ArtifactGeneratorCppCode::visit ( const ArtifactVariable node)
overridevirtual

Implements nnc::IArtifactGenerator.

Definition at line 47 of file ArtifactGeneratorCppCode.cpp.

48{
49 _out << node->typeName() << " " << node->name();
50
51 for (const auto &d : node->getDimensions())
52 {
53 _out << "[";
54 d->accept(this);
55 _out << "]";
56 }
57
58 if (!node->getInitializers().empty())
59 {
60 _out << "(";
61 bool add_comma = false;
62
63 for (const auto &i : node->getInitializers())
64 {
65 if (add_comma)
66 _out << ", ";
67
68 i->accept(this);
69 add_comma = true;
70 }
71
72 _out << ")";
73 }
74}

References nnc::ArtifactVariable::getDimensions(), nnc::ArtifactVariable::getInitializers(), nnc::ArtifactNamed::name(), and nnc::ArtifactVariable::typeName().


The documentation for this class was generated from the following files: