66#include " cppcodegenerator.h"
77#include " cppassignment.h"
88#include " cppfunctioncall.h"
9+ #include " cppswitch.h"
910#include " cppvalue.h"
1011#include " cppconstidentifier.h"
1112#include " ../typeutils.h"
@@ -47,7 +48,6 @@ CppCodeGenerator::~CppCodeGenerator()
4748
4849const CppIdentifier *CppCodeGenerator::castValue (CppValue *value, Compiler::StaticType targetType)
4950{
50- // TODO: Implement this
5151 if (value->isConst ()) {
5252 if (!TypeUtils::isSingleType (targetType))
5353 return createValue (value);
@@ -58,7 +58,97 @@ const CppIdentifier *CppCodeGenerator::castValue(CppValue *value, Compiler::Stat
5858 if (value->isRawValue )
5959 return castRawValue (value, targetType);
6060
61- return getNamedIdentifier (value, m_valueTypeMap.at (targetType), " castValue" , false );
61+ if (!TypeUtils::isSingleType (targetType))
62+ return createValue (value);
63+
64+ assert (value->type () != Compiler::StaticType::Void && targetType != Compiler::StaticType::Void);
65+ const CppNamedIdentifier *source = static_cast <const CppNamedIdentifier *>(createValue (value));
66+ const CppNamedIdentifier *result = getNamedIdentifier (value, m_valueTypeMap.at (targetType), " castValue" , false );
67+ const CppNamedIdentifier *valueType = createNamedIdentifier (CppIdentifier::Type::ValueType, " valueType" , true );
68+
69+ m_codeStream.beginLine ();
70+
71+ CppAssignment typeAssignment (m_codeStream, valueType);
72+ m_codeStream.addToLine (source->name + " .type" );
73+ typeAssignment.close ();
74+ m_codeStream.endLine ();
75+
76+ const CppNamedIdentifier *stringResult = targetType == Compiler::StaticType::String ? addStringAlloca () : nullptr ;
77+ CppSwitch sw (m_codeStream, valueType);
78+ const auto emitCase = [&](ValueType fromType, const std::string &, const std::string &expression) {
79+ sw.addCase (" ValueType::" + (fromType == ValueType::Number ? std::string (" Number" ) : fromType == ValueType::Bool ? " Bool" : " String" ));
80+ m_codeStream.beginLine ();
81+ CppAssignment assignment (m_codeStream, result);
82+ m_codeStream.addToLine (expression);
83+ assignment.close ();
84+ m_codeStream.endLine ();
85+ m_codeStream.beginLine ();
86+ m_codeStream.addToLine (" break;" );
87+ m_codeStream.endLine ();
88+ };
89+
90+ includeHeader (Header::ValueData);
91+
92+ if (targetType == Compiler::StaticType::Number) {
93+ emitCase (ValueType::Number, " numberValue" , source->name + " .numberValue" );
94+ emitCase (ValueType::Bool, " boolValue" , " static_cast<double>(" + source->name + " .boolValue)" );
95+ includeHeader (Header::ValueFunctions);
96+ emitCase (ValueType::String, " stringValue" , " value_stringToDouble(" + source->name + " .stringValue)" );
97+ } else if (targetType == Compiler::StaticType::Bool) {
98+ emitCase (ValueType::Number, " numberValue" , " (" + source->name + " .numberValue != 0.0)" );
99+ emitCase (ValueType::Bool, " boolValue" , source->name + " .boolValue" );
100+ includeHeader (Header::ValueFunctions);
101+ emitCase (ValueType::String, " stringValue" , " value_stringToBool(" + source->name + " .stringValue)" );
102+ } else if (targetType == Compiler::StaticType::String) {
103+ includeHeader (Header::ValueFunctions);
104+
105+ // The string destination is passed to the number conversion routine; bool conversion returns a stable pointer.
106+ sw.addCase (" ValueType::Number" );
107+ m_codeStream.beginLine ();
108+
109+ CppFunctionCall numberCall (m_codeStream, " value_doubleToStringPtr" );
110+ numberCall.param (source->name + " .numberValue" ).param (stringResult->name );
111+ numberCall.close ();
112+ m_codeStream.addToLine (" ;" );
113+ m_codeStream.endLine ();
114+ m_codeStream.beginLine ();
115+
116+ CppAssignment stringAssignment (m_codeStream, result);
117+ m_codeStream.addToLine (stringResult->name );
118+ stringAssignment.close ();
119+ m_codeStream.endLine ();
120+ m_codeStream.beginLine ();
121+ m_codeStream.addToLine (" break;" );
122+ m_codeStream.endLine ();
123+ sw.addCase (" ValueType::Bool" );
124+ m_codeStream.beginLine ();
125+
126+ CppAssignment boolAssignment (m_codeStream, result);
127+ m_codeStream.addToLine (" value_boolToStringPtr(" + source->name + " .boolValue)" );
128+ boolAssignment.close ();
129+ m_codeStream.endLine ();
130+ m_codeStream.beginLine ();
131+ m_codeStream.addToLine (" break;" );
132+ m_codeStream.endLine ();
133+ sw.addCase (" ValueType::String" );
134+ m_codeStream.beginLine ();
135+
136+ CppAssignment identityAssignment (m_codeStream, result);
137+ m_codeStream.addToLine (source->name + " .stringValue" );
138+ identityAssignment.close ();
139+ m_codeStream.endLine ();
140+ m_codeStream.beginLine ();
141+ m_codeStream.addToLine (" break;" );
142+ m_codeStream.endLine ();
143+ }
144+
145+ sw.addDefaultCase (" " );
146+ m_codeStream.beginLine ();
147+ m_codeStream.addToLine (" __builtin_unreachable();" );
148+ m_codeStream.endLine ();
149+ sw.close ();
150+
151+ return result;
62152}
63153
64154void CppCodeGenerator::createCall (const std::string &name, bool ctxArg, bool targetArg, const std::vector<const CppIdentifier *> &args)
@@ -216,18 +306,76 @@ void CppCodeGenerator::addArrayElement(const CppNamedIdentifier *array, size_t i
216306
217307const CppConstIdentifier *CppCodeGenerator::castConstValue (const Value &value, Compiler::StaticType targetType)
218308{
219- // TODO: Implement this
220- auto identifierType = m_valueTypeMap.at (TypeUtils::mapType (value.type ()));
221- auto ret = std::make_shared<CppConstIdentifier>(identifierType, value);
309+ Value casted = value;
310+
311+ switch (targetType) {
312+ case Compiler::StaticType::Number:
313+ casted = Value (value.toDouble ());
314+ break ;
315+
316+ case Compiler::StaticType::Bool:
317+ casted = Value (value.toBool ());
318+ break ;
319+
320+ case Compiler::StaticType::String:
321+ casted = Value (value.toString ());
322+ break ;
323+
324+ case Compiler::StaticType::Pointer:
325+ casted = Value (value.toPointer ());
326+ break ;
327+
328+ default :
329+ assert (false );
330+ break ;
331+ }
332+
333+ auto ret = std::make_shared<CppConstIdentifier>(m_valueTypeMap.at (targetType), casted);
222334 m_constants.push_back (ret);
223335 return ret.get ();
224336}
225337
226338const CppNamedIdentifier *CppCodeGenerator::castRawValue (CppValue *value, Compiler::StaticType targetType)
227339{
228- // TODO: Implement this
229340 assert (value->isRawValue );
230- return getNamedIdentifier (value, m_valueTypeMap.at (targetType), " castRawValue" , false );
341+
342+ const CppNamedIdentifier *source = static_cast <const CppNamedIdentifier *>(getIdentifier (value));
343+
344+ if (value->type () == targetType)
345+ return source;
346+
347+ const CppNamedIdentifier *result = getNamedIdentifier (value, m_valueTypeMap.at (targetType), " castRawValue" , false );
348+ std::string expression;
349+
350+ includeHeader (Header::ValueFunctions);
351+
352+ if (targetType == Compiler::StaticType::Number) {
353+ expression = value->type () == Compiler::StaticType::Bool ? " static_cast<double>(" + source->name + " )" : " value_stringToDouble(" + source->name + " )" ;
354+ } else if (targetType == Compiler::StaticType::Bool) {
355+ expression = value->type () == Compiler::StaticType::Number ? " (" + source->name + " != 0.0)" : " value_stringToBool(" + source->name + " )" ;
356+ } else if (targetType == Compiler::StaticType::String) {
357+ if (value->type () == Compiler::StaticType::Number) {
358+ const CppNamedIdentifier *string = addStringAlloca ();
359+
360+ m_codeStream.beginLine ();
361+ CppFunctionCall call (m_codeStream, " value_doubleToStringPtr" );
362+ call.param (source->name ).param (string->name );
363+ call.close ();
364+ m_codeStream.addToLine (" ;" );
365+ m_codeStream.endLine ();
366+
367+ expression = string->name ;
368+ } else if (value->type () == Compiler::StaticType::Bool) {
369+ expression = " value_boolToStringPtr(" + source->name + " )" ;
370+ }
371+ }
372+
373+ m_codeStream.beginLine ();
374+ CppAssignment assignment (m_codeStream, result);
375+ m_codeStream.addToLine (expression);
376+ assignment.close ();
377+ m_codeStream.endLine ();
378+ return result;
231379}
232380
233381const CppNamedIdentifier *CppCodeGenerator::createValue (CppValue *value)
0 commit comments