Skip to content

Commit d9c16a3

Browse files
committed
Implement C++ value casting
1 parent f3b4eb1 commit d9c16a3

2 files changed

Lines changed: 157 additions & 7 deletions

File tree

‎src/engine/internal/codegen/cpp/cppcodegenerator.cpp‎

Lines changed: 155 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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

4849
const 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

64154
void 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

217307
const 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

226338
const 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

233381
const CppNamedIdentifier *CppCodeGenerator::createValue(CppValue *value)

‎src/engine/internal/codegen/cpp/cppidentifier.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class CppIdentifier
2020
Pointer,
2121
Value,
2222
ValueData,
23+
ValueType,
2324
ContextPointer,
2425
TargetPointer,
2526
VariablePointerArray,
@@ -54,6 +55,7 @@ class CppIdentifier
5455
{ Type::Pointer, { "void *", false, true, false } },
5556
{ Type::Value, { "Value", true, false, false } },
5657
{ Type::ValueData, { "ValueData", true, false, false } },
58+
{ Type::ValueType, { "ValueType", false, false, false } },
5759
{ Type::ContextPointer, { "ExecutionContext *", true, true, false } },
5860
{ Type::TargetPointer, { "Target *", true, true, false } },
5961
{ Type::VariablePointerArray, { "Variable **", false, true, true } },

0 commit comments

Comments
 (0)