1
2
3
4
5
6 #include <cmajor/symbols/Value.hpp>
7 #include <cmajor/symbols/TypeSymbol.hpp>
8 #include <cmajor/symbols/Exception.hpp>
9 #include <cmajor/symbols/SymbolTable.hpp>
10 #include <cmajor/symbols/Module.hpp>
11 #include <soulng/util/Unicode.hpp>
12
13 namespace cmajor { namespace symbols {
14
15 using namespace soulng::unicode;
16
17 const char* valueTypeStr[]
18 {
19 "none", "bool", "sbyte", "byte", "short", "ushort", "int", "uint", "long", "ulong", "float", "double", "char", "wchar", "uchar", "string", "wstring", "ustring", "null", "pointer", "array", "structure", "uuid"
20 };
21
22 std::string ValueTypeStr(ValueType valueType)
23 {
24 return valueTypeStr[uint8_t(valueType)];
25 }
26
27 ValueType commonType[uint8_t(ValueType::maxValue)][uint8_t(ValueType::maxValue)] =
28 {
29
30 {
31 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
32 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
33 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
34 ValueType::none, ValueType::none, ValueType::none, ValueType::none
35 }
36
37
38 {
39 ValueType::none, ValueType::boolValue, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
40 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
41 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
42 ValueType::none, ValueType::none, ValueType::none, ValueType::none
43 }
44
45
46 {
47 ValueType::none, ValueType::none, ValueType::sbyteValue, ValueType::shortValue, ValueType::shortValue, ValueType::intValue, ValueType::intValue, ValueType::longValue,
48 ValueType::longValue, ValueType::none, ValueType::floatValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
49 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
50 ValueType::none, ValueType::none, ValueType::none, ValueType::none
51 }
52
53
54 {
55 ValueType::none, ValueType::none, ValueType::shortValue, ValueType::byteValue, ValueType::shortValue, ValueType::ushortValue, ValueType::intValue, ValueType::uintValue,
56 ValueType::longValue, ValueType::ulongValue, ValueType::floatValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
57 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
58 ValueType::none, ValueType::none, ValueType::none, ValueType::none
59 }
60
61
62 {
63 ValueType::none, ValueType::none, ValueType::shortValue, ValueType::shortValue, ValueType::shortValue, ValueType::intValue, ValueType::intValue, ValueType::longValue,
64 ValueType::longValue, ValueType::none, ValueType::floatValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
65 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
66 ValueType::none, ValueType::none, ValueType::none, ValueType::none
67 }
68
69
70 {
71 ValueType::none, ValueType::none, ValueType::intValue, ValueType::ushortValue, ValueType::intValue, ValueType::ushortValue, ValueType::intValue, ValueType::uintValue,
72 ValueType::longValue, ValueType::ulongValue, ValueType::floatValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
73 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
74 ValueType::none, ValueType::none, ValueType::none, ValueType::none
75 }
76
77
78 {
79 ValueType::none, ValueType::none, ValueType::intValue, ValueType::intValue, ValueType::intValue, ValueType::intValue, ValueType::intValue, ValueType::longValue,
80 ValueType::longValue, ValueType::none, ValueType::floatValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
81 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
82 ValueType::none, ValueType::none, ValueType::none, ValueType::none
83 }
84
85
86 {
87 ValueType::none, ValueType::none, ValueType::longValue, ValueType::uintValue, ValueType::longValue, ValueType::uintValue, ValueType::longValue, ValueType::uintValue,
88 ValueType::longValue, ValueType::ulongValue, ValueType::floatValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
89 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
90 ValueType::none, ValueType::none, ValueType::none, ValueType::none
91 }
92
93
94 {
95 ValueType::none, ValueType::none, ValueType::longValue, ValueType::longValue, ValueType::longValue, ValueType::longValue, ValueType::longValue, ValueType::longValue,
96 ValueType::longValue, ValueType::none, ValueType::floatValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
97 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
98 ValueType::none, ValueType::none, ValueType::none, ValueType::none
99 }
100
101
102 {
103 ValueType::none, ValueType::none, ValueType::none, ValueType::ulongValue, ValueType::none, ValueType::ulongValue, ValueType::none, ValueType::ulongValue,
104 ValueType::none, ValueType::ulongValue, ValueType::floatValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
105 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
106 ValueType::none, ValueType::none, ValueType::none, ValueType::none
107 }
108
109
110 {
111 ValueType::none, ValueType::none, ValueType::floatValue, ValueType::floatValue, ValueType::floatValue, ValueType::floatValue, ValueType::floatValue, ValueType::floatValue,
112 ValueType::floatValue, ValueType::floatValue, ValueType::floatValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
113 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
114 ValueType::none, ValueType::none, ValueType::none, ValueType::none
115 }
116
117
118 {
119 ValueType::none, ValueType::none, ValueType::doubleValue, ValueType::doubleValue, ValueType::doubleValue, ValueType::doubleValue, ValueType::doubleValue, ValueType::doubleValue,
120 ValueType::doubleValue, ValueType::doubleValue, ValueType::doubleValue, ValueType::doubleValue, ValueType::none, ValueType::none, ValueType::none,
121 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
122 ValueType::none, ValueType::none, ValueType::none, ValueType::none
123 }
124
125
126 {
127 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
128 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::charValue, ValueType::wcharValue, ValueType::ucharValue,
129 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
130 ValueType::none, ValueType::none, ValueType::none, ValueType::none
131 }
132
133
134 {
135 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
136 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::wcharValue, ValueType::wcharValue, ValueType::ucharValue,
137 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
138 ValueType::none, ValueType::none, ValueType::none, ValueType::none
139 }
140
141
142 {
143 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
144 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::ucharValue, ValueType::ucharValue, ValueType::ucharValue,
145 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
146 ValueType::none, ValueType::none, ValueType::none, ValueType::none
147 }
148
149
150 {
151 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
152 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
153 ValueType::stringValue, ValueType::none, ValueType::none, ValueType::none,
154 ValueType::none, ValueType::none, ValueType::none, ValueType::none
155 }
156
157
158 {
159 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
160 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
161 ValueType::none, ValueType::wstringValue, ValueType::none, ValueType::none,
162 ValueType::none, ValueType::none, ValueType::none, ValueType::none
163 }
164
165
166 {
167 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
168 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
169 ValueType::none, ValueType::none, ValueType::ustringValue, ValueType::none,
170 ValueType::none, ValueType::none, ValueType::none, ValueType::none
171 }
172
173
174 {
175 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
176 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
177 ValueType::none, ValueType::none, ValueType::none, ValueType::nullValue,
178 ValueType::pointerValue, ValueType::none, ValueType::none, ValueType::none
179 }
180
181
182 {
183 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
184 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
185 ValueType::none, ValueType::none, ValueType::none, ValueType::pointerValue,
186 ValueType::pointerValue, ValueType::none, ValueType::none, ValueType::none
187 }
188
189
190 {
191 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
192 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
193 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
194 ValueType::none, ValueType::arrayValue, ValueType::none, ValueType::none
195 }
196
197
198 {
199 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
200 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
201 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
202 ValueType::none, ValueType::none, ValueType::structuredValue, ValueType::none
203 }
204
205
206 {
207 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
208 ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none, ValueType::none,
209 ValueType::none, ValueType::none, ValueType::none, ValueType::none,
210 ValueType::none, ValueType::none, ValueType::none, ValueType::uuidValue
211 }
212 };
213
214 ValueType CommonType(ValueType left, ValueType right)
215 {
216 return commonType[uint8_t(left)][uint8_t(right)];
217 }
218
219 TypeSymbol* GetTypeFor(ValueType valueType, SymbolTable* symbolTable)
220 {
221 switch (valueType)
222 {
223 case ValueType::boolValue: return symbolTable->GetTypeByName(U"bool");
224 case ValueType::sbyteValue: return symbolTable->GetTypeByName(U"sbyte");
225 case ValueType::byteValue: return symbolTable->GetTypeByName(U"byte");
226 case ValueType::shortValue: return symbolTable->GetTypeByName(U"short");
227 case ValueType::ushortValue: return symbolTable->GetTypeByName(U"ushort");
228 case ValueType::intValue: return symbolTable->GetTypeByName(U"int");
229 case ValueType::uintValue: return symbolTable->GetTypeByName(U"uint");
230 case ValueType::longValue: return symbolTable->GetTypeByName(U"long");
231 case ValueType::ulongValue: return symbolTable->GetTypeByName(U"ulong");
232 case ValueType::floatValue: return symbolTable->GetTypeByName(U"float");
233 case ValueType::doubleValue: return symbolTable->GetTypeByName(U"double");
234 case ValueType::charValue: return symbolTable->GetTypeByName(U"char");
235 case ValueType::wcharValue: return symbolTable->GetTypeByName(U"wchar");
236 case ValueType::ucharValue: return symbolTable->GetTypeByName(U"uchar");
237 case ValueType::nullValue: return symbolTable->GetTypeByName(U"@nullptr_type");
238 }
239 return nullptr;
240 }
241
242 Value::Value(const Span& span_, const boost::uuids::uuid& moduleId_, ValueType valueType_) : span(span_), moduleId(moduleId_), valueType(valueType_)
243 {
244 }
245
246 Value::~Value()
247 {
248 }
249
250 std::std::unique_ptr<sngxml::dom::Element>Value::ToDomElement()
251 {
252 std::u32string className = ToUtf32(ClassName());
253 std::unique_ptr<sngxml::dom::Element> element(new sngxml::dom::Element(className));
254 element->SetAttribute(U"info", ToUtf32(ToString()));
255 return element;
256 }
257
258 BoolValue::BoolValue(const Span& span_, const boost::uuids::uuid& moduleId_, bool value_) : Value(span_, moduleId_, ValueType::boolValue), value(value_)
259 {
260 }
261
262 void* BoolValue::IrValue(Emitter& emitter)
263 {
264 return emitter.CreateIrValueForBool(value);
265 }
266
267 void BoolValue::Write(BinaryWriter& writer)
268 {
269 writer.Write(value);
270 }
271
272 void BoolValue::Read(BinaryReader& reader)
273 {
274 reader.ReadBool();
275 }
276
277 Value* BoolValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
278 {
279 switch (targetType->GetValueType())
280 {
281 case ValueType::boolValue:
282 {
283 return new BoolValue(span, moduleId, value);
284 }
285 case ValueType::sbyteValue:
286 {
287 if (cast)
288 {
289 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
290 }
291 else
292 {
293 if (dontThrow)
294 {
295 return nullptr;
296 }
297 else
298 {
299 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
300 }
301 }
302 }
303 case ValueType::byteValue:
304 {
305 if (cast)
306 {
307 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
308 }
309 else
310 {
311 if (dontThrow)
312 {
313 return nullptr;
314 }
315 else
316 {
317 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
318 }
319 }
320 }
321 case ValueType::shortValue:
322 {
323 if (cast)
324 {
325 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
326 }
327 else
328 {
329 if (dontThrow)
330 {
331 return nullptr;
332 }
333 else
334 {
335 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
336 }
337 }
338 }
339 case ValueType::ushortValue:
340 {
341 if (cast)
342 {
343 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
344 }
345 else
346 {
347 if (dontThrow)
348 {
349 return nullptr;
350 }
351 else
352 {
353 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
354 }
355 }
356 }
357 case ValueType::intValue:
358 {
359 if (cast)
360 {
361 return new IntValue(span, moduleId, static_cast<int32_t>(value));
362 }
363 else
364 {
365 if (dontThrow)
366 {
367 return nullptr;
368 }
369 else
370 {
371 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
372 }
373 }
374 }
375 case ValueType::uintValue:
376 {
377 if (cast)
378 {
379 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
380 }
381 else
382 {
383 if (dontThrow)
384 {
385 return nullptr;
386 }
387 else
388 {
389 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
390 }
391 }
392 }
393 case ValueType::longValue:
394 {
395 if (cast)
396 {
397 return new LongValue(span, moduleId, static_cast<int64_t>(value));
398 }
399 else
400 {
401 if (dontThrow)
402 {
403 return nullptr;
404 }
405 else
406 {
407 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
408 }
409 }
410 }
411 case ValueType::ulongValue:
412 {
413 if (cast)
414 {
415 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
416 }
417 else
418 {
419 if (dontThrow)
420 {
421 return nullptr;
422 }
423 else
424 {
425 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
426 }
427 }
428 }
429 case ValueType::floatValue:
430 {
431 if (cast)
432 {
433 return new FloatValue(span, moduleId, static_cast<float>(value));
434 }
435 else
436 {
437 if (dontThrow)
438 {
439 return nullptr;
440 }
441 else
442 {
443 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
444 }
445 }
446 }
447 case ValueType::doubleValue:
448 {
449 if (cast)
450 {
451 return new DoubleValue(span, moduleId, static_cast<double>(value));
452 }
453 else
454 {
455 if (dontThrow)
456 {
457 return nullptr;
458 }
459 else
460 {
461 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
462 }
463 }
464 }
465 case ValueType::charValue:
466 {
467 if (cast)
468 {
469 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
470 }
471 else
472 {
473 if (dontThrow)
474 {
475 return nullptr;
476 }
477 else
478 {
479 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
480 }
481 }
482 }
483 case ValueType::wcharValue:
484 {
485 if (cast)
486 {
487 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
488 }
489 else
490 {
491 if (dontThrow)
492 {
493 return nullptr;
494 }
495 else
496 {
497 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
498 }
499 }
500 }
501 case ValueType::ucharValue:
502 {
503 if (cast)
504 {
505 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
506 }
507 else
508 {
509 if (dontThrow)
510 {
511 return nullptr;
512 }
513 else
514 {
515 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
516 }
517 }
518 }
519 default:
520 {
521 if (dontThrow)
522 {
523 return nullptr;
524 }
525 else
526 {
527 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
528 }
529 }
530 }
531 }
532
533 TypeSymbol* BoolValue::GetType(SymbolTable* symbolTable)
534 {
535 return symbolTable->GetTypeByName(U"bool");
536 }
537
538 SByteValue::SByteValue(const Span& span_, const boost::uuids::uuid& moduleId_, int8_t value_) : Value(span_, moduleId_, ValueType::sbyteValue), value(value_)
539 {
540 }
541
542 void* SByteValue::IrValue(Emitter& emitter)
543 {
544 return emitter.CreateIrValueForSByte(value);
545 }
546
547 void SByteValue::Write(BinaryWriter& writer)
548 {
549 writer.Write(value);
550 }
551
552 void SByteValue::Read(BinaryReader& reader)
553 {
554 value = reader.ReadSByte();
555 }
556
557 Value* SByteValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
558 {
559 switch (targetType->GetValueType())
560 {
561 case ValueType::boolValue:
562 {
563 if (cast)
564 {
565 return new BoolValue(span, moduleId, static_cast<bool>(value));
566 }
567 else
568 {
569 if (dontThrow)
570 {
571 return nullptr;
572 }
573 else
574 {
575 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
576 }
577 }
578 }
579 case ValueType::sbyteValue:
580 {
581 return new SByteValue(span, moduleId, value);
582 }
583 case ValueType::byteValue:
584 {
585 if (cast)
586 {
587 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
588 }
589 else
590 {
591 if (dontThrow)
592 {
593 return nullptr;
594 }
595 else
596 {
597 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
598 }
599 }
600 }
601 case ValueType::shortValue:
602 {
603 return new ShortValue(span, moduleId, value);
604 }
605 case ValueType::ushortValue:
606 {
607 if (cast)
608 {
609 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
610 }
611 else
612 {
613 if (dontThrow)
614 {
615 return nullptr;
616 }
617 else
618 {
619 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
620 }
621 }
622 }
623 case ValueType::intValue:
624 {
625 return new IntValue(span, moduleId, value);
626 }
627 case ValueType::uintValue:
628 {
629 if (cast)
630 {
631 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
632 }
633 else
634 {
635 if (dontThrow)
636 {
637 return nullptr;
638 }
639 else
640 {
641 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
642 }
643 }
644 }
645 case ValueType::longValue:
646 {
647 return new LongValue(span, moduleId, value);
648 }
649 case ValueType::ulongValue:
650 {
651 if (cast)
652 {
653 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
654 }
655 else
656 {
657 if (dontThrow)
658 {
659 return nullptr;
660 }
661 else
662 {
663 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
664 }
665 }
666 }
667 case ValueType::floatValue:
668 {
669 return new FloatValue(span, moduleId, value);
670 }
671 case ValueType::doubleValue:
672 {
673 return new FloatValue(span, moduleId, value);
674 }
675 case ValueType::charValue:
676 {
677 if (cast)
678 {
679 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
680 }
681 else
682 {
683 if (dontThrow)
684 {
685 return nullptr;
686 }
687 else
688 {
689 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
690 }
691 }
692 }
693 case ValueType::wcharValue:
694 {
695 if (cast)
696 {
697 return new CharValue(span, moduleId, static_cast<uint16_t>(value));
698 }
699 else
700 {
701 if (dontThrow)
702 {
703 return nullptr;
704 }
705 else
706 {
707 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
708 }
709 }
710 }
711 case ValueType::ucharValue:
712 {
713 if (cast)
714 {
715 return new CharValue(span, moduleId, static_cast<uint32_t>(value));
716 }
717 else
718 {
719 if (dontThrow)
720 {
721 return nullptr;
722 }
723 else
724 {
725 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
726 }
727 }
728 }
729 default:
730 {
731 if (dontThrow)
732 {
733 return nullptr;
734 }
735 else
736 {
737 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
738 }
739 }
740 }
741 }
742
743 TypeSymbol* SByteValue::GetType(SymbolTable* symbolTable)
744 {
745 return symbolTable->GetTypeByName(U"sbyte");
746 }
747
748 ByteValue::ByteValue(const Span& span_, const boost::uuids::uuid& moduleId_, uint8_t value_) : Value(span_, moduleId_, ValueType::byteValue), value(value_)
749 {
750 }
751
752 void* ByteValue::IrValue(Emitter& emitter)
753 {
754 return emitter.CreateIrValueForByte(value);
755 }
756
757 void ByteValue::Write(BinaryWriter& writer)
758 {
759 writer.Write(value);
760 }
761
762 void ByteValue::Read(BinaryReader& reader)
763 {
764 value = reader.ReadByte();
765 }
766
767 Value* ByteValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
768 {
769 switch (targetType->GetValueType())
770 {
771 case ValueType::boolValue:
772 {
773 if (cast)
774 {
775 return new BoolValue(span, moduleId, static_cast<bool>(value));
776 }
777 else
778 {
779 if (dontThrow)
780 {
781 return nullptr;
782 }
783 else
784 {
785 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
786 }
787 }
788 }
789 case ValueType::sbyteValue:
790 {
791 if (cast)
792 {
793 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
794 }
795 else
796 {
797 if (dontThrow)
798 {
799 return nullptr;
800 }
801 else
802 {
803 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
804 }
805 }
806 }
807 case ValueType::byteValue:
808 {
809 return new ByteValue(span, moduleId, value);
810 }
811 case ValueType::shortValue:
812 {
813 return new ShortValue(span, moduleId, value);
814 }
815 case ValueType::ushortValue:
816 {
817 return new UShortValue(span, moduleId, value);
818 }
819 case ValueType::intValue:
820 {
821 return new IntValue(span, moduleId, value);
822 }
823 case ValueType::uintValue:
824 {
825 return new UIntValue(span, moduleId, value);
826 }
827 case ValueType::longValue:
828 {
829 return new LongValue(span, moduleId, value);
830 }
831 case ValueType::ulongValue:
832 {
833 return new ULongValue(span, moduleId, value);
834 }
835 case ValueType::floatValue:
836 {
837 return new FloatValue(span, moduleId, value);
838 }
839 case ValueType::doubleValue:
840 {
841 return new DoubleValue(span, moduleId, value);
842 }
843 case ValueType::charValue:
844 {
845 if (cast)
846 {
847 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
848 }
849 else
850 {
851 if (dontThrow)
852 {
853 return nullptr;
854 }
855 else
856 {
857 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
858 }
859 }
860 }
861 case ValueType::wcharValue:
862 {
863 if (cast)
864 {
865 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
866 }
867 else
868 {
869 if (dontThrow)
870 {
871 return nullptr;
872 }
873 else
874 {
875 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
876 }
877 }
878 }
879 case ValueType::ucharValue:
880 {
881 if (cast)
882 {
883 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
884 }
885 else
886 {
887 if (dontThrow)
888 {
889 return nullptr;
890 }
891 else
892 {
893 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
894 }
895 }
896 }
897 default:
898 {
899 if (dontThrow)
900 {
901 return nullptr;
902 }
903 else
904 {
905 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
906 }
907 }
908 }
909 }
910
911 TypeSymbol* ByteValue::GetType(SymbolTable* symbolTable)
912 {
913 return symbolTable->GetTypeByName(U"byte");
914 }
915
916 ShortValue::ShortValue(const Span& span_, const boost::uuids::uuid& moduleId_, int16_t value_) : Value(span_, moduleId_, ValueType::shortValue), value(value_)
917 {
918 }
919
920 void* ShortValue::IrValue(Emitter& emitter)
921 {
922 return emitter.CreateIrValueForShort(value);
923 }
924
925 void ShortValue::Write(BinaryWriter& writer)
926 {
927 writer.Write(value);
928 }
929
930 void ShortValue::Read(BinaryReader& reader)
931 {
932 value = reader.ReadShort();
933 }
934
935 Value* ShortValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
936 {
937 switch (targetType->GetValueType())
938 {
939 case ValueType::boolValue:
940 {
941 if (cast)
942 {
943 return new BoolValue(span, moduleId, static_cast<bool>(value));
944 }
945 else
946 {
947 if (dontThrow)
948 {
949 return nullptr;
950 }
951 else
952 {
953 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
954 }
955 }
956 }
957 case ValueType::sbyteValue:
958 {
959 if (cast)
960 {
961 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
962 }
963 else
964 {
965 if (dontThrow)
966 {
967 return nullptr;
968 }
969 else
970 {
971 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
972 }
973 }
974 }
975 case ValueType::byteValue:
976 {
977 if (cast)
978 {
979 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
980 }
981 else
982 {
983 if (dontThrow)
984 {
985 return nullptr;
986 }
987 else
988 {
989 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
990 }
991 }
992 }
993 case ValueType::shortValue:
994 {
995 return new ShortValue(span, moduleId, value);
996 }
997 case ValueType::ushortValue:
998 {
999 if (cast)
1000 {
1001 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
1002 }
1003 else
1004 {
1005 if (dontThrow)
1006 {
1007 return nullptr;
1008 }
1009 else
1010 {
1011 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1012 }
1013 }
1014 }
1015 case ValueType::intValue:
1016 {
1017 return new IntValue(span, moduleId, value);
1018 }
1019 case ValueType::uintValue:
1020 {
1021 if (cast)
1022 {
1023 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
1024 }
1025 else
1026 {
1027 if (dontThrow)
1028 {
1029 return nullptr;
1030 }
1031 else
1032 {
1033 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1034 }
1035 }
1036 }
1037 case ValueType::longValue:
1038 {
1039 return new LongValue(span, moduleId, value);
1040 }
1041 case ValueType::ulongValue:
1042 {
1043 if (cast)
1044 {
1045 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
1046 }
1047 else
1048 {
1049 if (dontThrow)
1050 {
1051 return nullptr;
1052 }
1053 else
1054 {
1055 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1056 }
1057 }
1058 }
1059 case ValueType::floatValue:
1060 {
1061 return new FloatValue(span, moduleId, value);
1062 }
1063 case ValueType::doubleValue:
1064 {
1065 return new DoubleValue(span, moduleId, value);
1066 }
1067 case ValueType::charValue:
1068 {
1069 if (cast)
1070 {
1071 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
1072 }
1073 else
1074 {
1075 if (dontThrow)
1076 {
1077 return nullptr;
1078 }
1079 else
1080 {
1081 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1082 }
1083 }
1084 }
1085 case ValueType::wcharValue:
1086 {
1087 if (cast)
1088 {
1089 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
1090 }
1091 else
1092 {
1093 if (dontThrow)
1094 {
1095 return nullptr;
1096 }
1097 else
1098 {
1099 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1100 }
1101 }
1102 }
1103 case ValueType::ucharValue:
1104 {
1105 if (cast)
1106 {
1107 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
1108 }
1109 else
1110 {
1111 if (dontThrow)
1112 {
1113 return nullptr;
1114 }
1115 else
1116 {
1117 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1118 }
1119 }
1120 }
1121 default:
1122 {
1123 if (dontThrow)
1124 {
1125 return nullptr;
1126 }
1127 else
1128 {
1129 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
1130 }
1131 }
1132 }
1133 }
1134
1135 TypeSymbol* ShortValue::GetType(SymbolTable* symbolTable)
1136 {
1137 return symbolTable->GetTypeByName(U"short");
1138 }
1139
1140 UShortValue::UShortValue(const Span& span_, const boost::uuids::uuid& moduleId_, uint16_t value_) : Value(span_, moduleId_, ValueType::ushortValue), value(value_)
1141 {
1142 }
1143
1144 void* UShortValue::IrValue(Emitter& emitter)
1145 {
1146 return emitter.CreateIrValueForUShort(value);
1147 }
1148
1149 void UShortValue::Write(BinaryWriter& writer)
1150 {
1151 writer.Write(value);
1152 }
1153
1154 void UShortValue::Read(BinaryReader& reader)
1155 {
1156 value = reader.ReadUShort();
1157 }
1158
1159 Value* UShortValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
1160 {
1161 switch (targetType->GetValueType())
1162 {
1163 case ValueType::boolValue:
1164 {
1165 if (cast)
1166 {
1167 return new BoolValue(span, moduleId, static_cast<bool>(value));
1168 }
1169 else
1170 {
1171 if (dontThrow)
1172 {
1173 return nullptr;
1174 }
1175 else
1176 {
1177 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1178 }
1179 }
1180 }
1181 case ValueType::sbyteValue:
1182 {
1183 if (cast)
1184 {
1185 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
1186 }
1187 else
1188 {
1189 if (dontThrow)
1190 {
1191 return nullptr;
1192 }
1193 else
1194 {
1195 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1196 }
1197 }
1198 }
1199 case ValueType::byteValue:
1200 {
1201 if (cast)
1202 {
1203 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
1204 }
1205 else
1206 {
1207 if (dontThrow)
1208 {
1209 return nullptr;
1210 }
1211 else
1212 {
1213 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1214 }
1215 }
1216 }
1217 case ValueType::shortValue:
1218 {
1219 if (cast)
1220 {
1221 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
1222 }
1223 else
1224 {
1225 if (dontThrow)
1226 {
1227 return nullptr;
1228 }
1229 else
1230 {
1231 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1232 }
1233 }
1234 }
1235 case ValueType::ushortValue:
1236 {
1237 return new UShortValue(span, moduleId, value);
1238 }
1239 case ValueType::intValue:
1240 {
1241 return new IntValue(span, moduleId, value);
1242 }
1243 case ValueType::uintValue:
1244 {
1245 return new UIntValue(span, moduleId, value);
1246 }
1247 case ValueType::longValue:
1248 {
1249 return new LongValue(span, moduleId, value);
1250 }
1251 case ValueType::ulongValue:
1252 {
1253 return new ULongValue(span, moduleId, value);
1254 }
1255 case ValueType::floatValue:
1256 {
1257 return new FloatValue(span, moduleId, value);
1258 }
1259 case ValueType::doubleValue:
1260 {
1261 return new DoubleValue(span, moduleId, value);
1262 }
1263 case ValueType::charValue:
1264 {
1265 if (cast)
1266 {
1267 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
1268 }
1269 else
1270 {
1271 if (dontThrow)
1272 {
1273 return nullptr;
1274 }
1275 else
1276 {
1277 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1278 }
1279 }
1280 }
1281 case ValueType::wcharValue:
1282 {
1283 if (cast)
1284 {
1285 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
1286 }
1287 else
1288 {
1289 if (dontThrow)
1290 {
1291 return nullptr;
1292 }
1293 else
1294 {
1295 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1296 }
1297 }
1298 }
1299 case ValueType::ucharValue:
1300 {
1301 if (cast)
1302 {
1303 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
1304 }
1305 else
1306 {
1307 if (dontThrow)
1308 {
1309 return nullptr;
1310 }
1311 else
1312 {
1313 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1314 }
1315 }
1316 }
1317 default:
1318 {
1319 if (dontThrow)
1320 {
1321 return nullptr;
1322 }
1323 else
1324 {
1325 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
1326 }
1327 }
1328 }
1329 }
1330
1331 TypeSymbol* UShortValue::GetType(SymbolTable* symbolTable)
1332 {
1333 return symbolTable->GetTypeByName(U"ushort");
1334 }
1335
1336 IntValue::IntValue(const Span& span_, const boost::uuids::uuid& moduleId_, int32_t value_) : Value(span_, moduleId_, ValueType::intValue), value(value_)
1337 {
1338 }
1339
1340 void* IntValue::IrValue(Emitter& emitter)
1341 {
1342 return emitter.CreateIrValueForInt(value);
1343 }
1344
1345 void IntValue::Write(BinaryWriter& writer)
1346 {
1347 writer.Write(value);
1348 }
1349
1350 void IntValue::Read(BinaryReader& reader)
1351 {
1352 value = reader.ReadInt();
1353 }
1354
1355 Value* IntValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
1356 {
1357 switch (targetType->GetValueType())
1358 {
1359 case ValueType::boolValue:
1360 {
1361 if (cast)
1362 {
1363 return new BoolValue(span, moduleId, static_cast<bool>(value));
1364 }
1365 else
1366 {
1367 if (dontThrow)
1368 {
1369 return nullptr;
1370 }
1371 else
1372 {
1373 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1374 }
1375 }
1376 }
1377 case ValueType::sbyteValue:
1378 {
1379 if (cast)
1380 {
1381 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
1382 }
1383 else
1384 {
1385 if (dontThrow)
1386 {
1387 return nullptr;
1388 }
1389 else
1390 {
1391 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1392 }
1393 }
1394 }
1395 case ValueType::byteValue:
1396 {
1397 if (cast)
1398 {
1399 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
1400 }
1401 else
1402 {
1403 if (dontThrow)
1404 {
1405 return nullptr;
1406 }
1407 else
1408 {
1409 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1410 }
1411 }
1412 }
1413 case ValueType::shortValue:
1414 {
1415 if (cast)
1416 {
1417 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
1418 }
1419 else
1420 {
1421 if (dontThrow)
1422 {
1423 return nullptr;
1424 }
1425 else
1426 {
1427 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1428 }
1429 }
1430 }
1431 case ValueType::ushortValue:
1432 {
1433 if (cast)
1434 {
1435 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
1436 }
1437 else
1438 {
1439 if (dontThrow)
1440 {
1441 return nullptr;
1442 }
1443 else
1444 {
1445 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1446 }
1447 }
1448 }
1449 case ValueType::intValue:
1450 {
1451 return new IntValue(span, moduleId, value);
1452 }
1453 case ValueType::uintValue:
1454 {
1455 if (cast)
1456 {
1457 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
1458 }
1459 else
1460 {
1461 if (dontThrow)
1462 {
1463 return nullptr;
1464 }
1465 else
1466 {
1467 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1468 }
1469 }
1470 }
1471 case ValueType::longValue:
1472 {
1473 return new LongValue(span, moduleId, value);
1474 }
1475 case ValueType::ulongValue:
1476 {
1477 if (cast)
1478 {
1479 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
1480 }
1481 else
1482 {
1483 if (dontThrow)
1484 {
1485 return nullptr;
1486 }
1487 else
1488 {
1489 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1490 }
1491 }
1492 }
1493 case ValueType::floatValue:
1494 {
1495 return new FloatValue(span, moduleId, value);
1496 }
1497 case ValueType::doubleValue:
1498 {
1499 return new DoubleValue(span, moduleId, value);
1500 }
1501 case ValueType::charValue:
1502 {
1503 if (cast)
1504 {
1505 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
1506 }
1507 else
1508 {
1509 if (dontThrow)
1510 {
1511 return nullptr;
1512 }
1513 else
1514 {
1515 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1516 }
1517 }
1518 }
1519 case ValueType::wcharValue:
1520 {
1521 if (cast)
1522 {
1523 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
1524 }
1525 else
1526 {
1527 if (dontThrow)
1528 {
1529 return nullptr;
1530 }
1531 else
1532 {
1533 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1534 }
1535 }
1536 }
1537 case ValueType::ucharValue:
1538 {
1539 if (cast)
1540 {
1541 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
1542 }
1543 else
1544 {
1545 if (dontThrow)
1546 {
1547 return nullptr;
1548 }
1549 else
1550 {
1551 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1552 }
1553 }
1554 }
1555 default:
1556 {
1557 if (dontThrow)
1558 {
1559 return nullptr;
1560 }
1561 else
1562 {
1563 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
1564 }
1565 }
1566 }
1567 }
1568
1569 TypeSymbol* IntValue::GetType(SymbolTable* symbolTable)
1570 {
1571 return symbolTable->GetTypeByName(U"int");
1572 }
1573
1574 UIntValue::UIntValue(const Span& span_, const boost::uuids::uuid& moduleId_, uint32_t value_) : Value(span_, moduleId_, ValueType::uintValue), value(value_)
1575 {
1576 }
1577
1578 void* UIntValue::IrValue(Emitter& emitter)
1579 {
1580 return emitter.CreateIrValueForUInt(value);
1581 }
1582
1583 void UIntValue::Write(BinaryWriter& writer)
1584 {
1585 writer.Write(value);
1586 }
1587
1588 void UIntValue::Read(BinaryReader& reader)
1589 {
1590 value = reader.ReadUInt();
1591 }
1592
1593 Value* UIntValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
1594 {
1595 switch (targetType->GetValueType())
1596 {
1597 case ValueType::boolValue:
1598 {
1599 if (cast)
1600 {
1601 return new BoolValue(span, moduleId, static_cast<bool>(value));
1602 }
1603 else
1604 {
1605 if (dontThrow)
1606 {
1607 return nullptr;
1608 }
1609 else
1610 {
1611 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1612 }
1613 }
1614 }
1615 case ValueType::sbyteValue:
1616 {
1617 if (cast)
1618 {
1619 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
1620 }
1621 else
1622 {
1623 if (dontThrow)
1624 {
1625 return nullptr;
1626 }
1627 else
1628 {
1629 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1630 }
1631 }
1632 }
1633 case ValueType::byteValue:
1634 {
1635 if (cast)
1636 {
1637 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
1638 }
1639 else
1640 {
1641 if (dontThrow)
1642 {
1643 return nullptr;
1644 }
1645 else
1646 {
1647 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1648 }
1649 }
1650 }
1651 case ValueType::shortValue:
1652 {
1653 if (cast)
1654 {
1655 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
1656 }
1657 else
1658 {
1659 if (dontThrow)
1660 {
1661 return nullptr;
1662 }
1663 else
1664 {
1665 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1666 }
1667 }
1668 }
1669 case ValueType::ushortValue:
1670 {
1671 if (cast)
1672 {
1673 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
1674 }
1675 else
1676 {
1677 if (dontThrow)
1678 {
1679 return nullptr;
1680 }
1681 else
1682 {
1683 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1684 }
1685 }
1686 }
1687 case ValueType::intValue:
1688 {
1689 if (cast)
1690 {
1691 return new IntValue(span, moduleId, static_cast<int32_t>(value));
1692 }
1693 else
1694 {
1695 if (dontThrow)
1696 {
1697 return nullptr;
1698 }
1699 else
1700 {
1701 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1702 }
1703 }
1704 }
1705 case ValueType::uintValue:
1706 {
1707 return new UIntValue(span, moduleId, value);
1708 }
1709 case ValueType::longValue:
1710 {
1711 return new LongValue(span, moduleId, value);
1712 }
1713 case ValueType::ulongValue:
1714 {
1715 return new ULongValue(span, moduleId, value);
1716 }
1717 case ValueType::floatValue:
1718 {
1719 return new FloatValue(span, moduleId, value);
1720 }
1721 case ValueType::doubleValue:
1722 {
1723 return new DoubleValue(span, moduleId, value);
1724 }
1725 case ValueType::charValue:
1726 {
1727 if (cast)
1728 {
1729 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
1730 }
1731 else
1732 {
1733 if (dontThrow)
1734 {
1735 return nullptr;
1736 }
1737 else
1738 {
1739 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1740 }
1741 }
1742 }
1743 case ValueType::wcharValue:
1744 {
1745 if (cast)
1746 {
1747 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
1748 }
1749 else
1750 {
1751 if (dontThrow)
1752 {
1753 return nullptr;
1754 }
1755 else
1756 {
1757 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1758 }
1759 }
1760 }
1761 case ValueType::ucharValue:
1762 {
1763 if (cast)
1764 {
1765 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
1766 }
1767 else
1768 {
1769 if (dontThrow)
1770 {
1771 return nullptr;
1772 }
1773 else
1774 {
1775 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1776 }
1777 }
1778 }
1779 default:
1780 {
1781 if (dontThrow)
1782 {
1783 return nullptr;
1784 }
1785 else
1786 {
1787 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
1788 }
1789 }
1790 }
1791 }
1792
1793 TypeSymbol* UIntValue::GetType(SymbolTable* symbolTable)
1794 {
1795 return symbolTable->GetTypeByName(U"uint");
1796 }
1797
1798 LongValue::LongValue(const Span& span_, const boost::uuids::uuid& moduleId_, int64_t value_) : Value(span_, moduleId_, ValueType::longValue), value(value_)
1799 {
1800 }
1801
1802 void* LongValue::IrValue(Emitter& emitter)
1803 {
1804 return emitter.CreateIrValueForLong(value);
1805 }
1806
1807 void LongValue::Write(BinaryWriter& writer)
1808 {
1809 writer.Write(value);
1810 }
1811
1812 void LongValue::Read(BinaryReader& reader)
1813 {
1814 value = reader.ReadLong();
1815 }
1816
1817 Value* LongValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
1818 {
1819 switch (targetType->GetValueType())
1820 {
1821 case ValueType::boolValue:
1822 {
1823 if (cast)
1824 {
1825 return new BoolValue(span, moduleId, static_cast<bool>(value));
1826 }
1827 else
1828 {
1829 if (dontThrow)
1830 {
1831 return nullptr;
1832 }
1833 else
1834 {
1835 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1836 }
1837 }
1838 }
1839 case ValueType::sbyteValue:
1840 {
1841 if (cast)
1842 {
1843 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
1844 }
1845 else
1846 {
1847 if (dontThrow)
1848 {
1849 return nullptr;
1850 }
1851 else
1852 {
1853 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1854 }
1855 }
1856 }
1857 case ValueType::byteValue:
1858 {
1859 if (cast)
1860 {
1861 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
1862 }
1863 else
1864 {
1865 if (dontThrow)
1866 {
1867 return nullptr;
1868 }
1869 else
1870 {
1871 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1872 }
1873 }
1874 }
1875 case ValueType::shortValue:
1876 {
1877 if (cast)
1878 {
1879 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
1880 }
1881 else
1882 {
1883 if (dontThrow)
1884 {
1885 return nullptr;
1886 }
1887 else
1888 {
1889 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1890 }
1891 }
1892 }
1893 case ValueType::ushortValue:
1894 {
1895 if (cast)
1896 {
1897 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
1898 }
1899 else
1900 {
1901 if (dontThrow)
1902 {
1903 return nullptr;
1904 }
1905 else
1906 {
1907 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1908 }
1909 }
1910 }
1911 case ValueType::intValue:
1912 {
1913 if (cast)
1914 {
1915 return new IntValue(span, moduleId, static_cast<int32_t>(value));
1916 }
1917 else
1918 {
1919 if (dontThrow)
1920 {
1921 return nullptr;
1922 }
1923 else
1924 {
1925 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1926 }
1927 }
1928 }
1929 case ValueType::uintValue:
1930 {
1931 if (cast)
1932 {
1933 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
1934 }
1935 else
1936 {
1937 if (dontThrow)
1938 {
1939 return nullptr;
1940 }
1941 else
1942 {
1943 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1944 }
1945 }
1946 }
1947 case ValueType::longValue:
1948 {
1949 return new LongValue(span, moduleId, value);
1950 }
1951 case ValueType::ulongValue:
1952 {
1953 if (cast)
1954 {
1955 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
1956 }
1957 else
1958 {
1959 if (dontThrow)
1960 {
1961 return nullptr;
1962 }
1963 else
1964 {
1965 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1966 }
1967 }
1968 }
1969 case ValueType::floatValue:
1970 {
1971 return new FloatValue(span, moduleId, value);
1972 }
1973 case ValueType::doubleValue:
1974 {
1975 return new DoubleValue(span, moduleId, value);
1976 }
1977 case ValueType::charValue:
1978 {
1979 if (cast)
1980 {
1981 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
1982 }
1983 else
1984 {
1985 if (dontThrow)
1986 {
1987 return nullptr;
1988 }
1989 else
1990 {
1991 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
1992 }
1993 }
1994 }
1995 case ValueType::wcharValue:
1996 {
1997 if (cast)
1998 {
1999 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
2000 }
2001 else
2002 {
2003 if (dontThrow)
2004 {
2005 return nullptr;
2006 }
2007 else
2008 {
2009 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2010 }
2011 }
2012 }
2013 case ValueType::ucharValue:
2014 {
2015 if (cast)
2016 {
2017 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
2018 }
2019 else
2020 {
2021 if (dontThrow)
2022 {
2023 return nullptr;
2024 }
2025 else
2026 {
2027 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2028 }
2029 }
2030 }
2031 default:
2032 {
2033 if (dontThrow)
2034 {
2035 return nullptr;
2036 }
2037 else
2038 {
2039 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
2040 }
2041 }
2042 }
2043 }
2044
2045 TypeSymbol* LongValue::GetType(SymbolTable* symbolTable)
2046 {
2047 return symbolTable->GetTypeByName(U"long");
2048 }
2049
2050 ULongValue::ULongValue(const Span& span_, const boost::uuids::uuid& moduleId_, uint64_t value_) : Value(span_, moduleId_, ValueType::ulongValue), value(value_)
2051 {
2052 }
2053
2054 void* ULongValue::IrValue(Emitter& emitter)
2055 {
2056 return emitter.CreateIrValueForULong(value);
2057 }
2058
2059 void ULongValue::Write(BinaryWriter& writer)
2060 {
2061 writer.Write(value);
2062 }
2063
2064 void ULongValue::Read(BinaryReader& reader)
2065 {
2066 value = reader.ReadULong();
2067 }
2068
2069 Value* ULongValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
2070 {
2071 switch (targetType->GetValueType())
2072 {
2073 case ValueType::boolValue:
2074 {
2075 if (cast)
2076 {
2077 return new BoolValue(span, moduleId, static_cast<bool>(value));
2078 }
2079 else
2080 {
2081 if (dontThrow)
2082 {
2083 return nullptr;
2084 }
2085 else
2086 {
2087 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2088 }
2089 }
2090 }
2091 case ValueType::sbyteValue:
2092 {
2093 if (cast)
2094 {
2095 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
2096 }
2097 else
2098 {
2099 if (dontThrow)
2100 {
2101 return nullptr;
2102 }
2103 else
2104 {
2105 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2106 }
2107 }
2108 }
2109 case ValueType::byteValue:
2110 {
2111 if (cast)
2112 {
2113 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
2114 }
2115 else
2116 {
2117 if (dontThrow)
2118 {
2119 return nullptr;
2120 }
2121 else
2122 {
2123 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2124 }
2125 }
2126 }
2127 case ValueType::shortValue:
2128 {
2129 if (cast)
2130 {
2131 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
2132 }
2133 else
2134 {
2135 if (dontThrow)
2136 {
2137 return nullptr;
2138 }
2139 else
2140 {
2141 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2142 }
2143 }
2144 }
2145 case ValueType::ushortValue:
2146 {
2147 if (cast)
2148 {
2149 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
2150 }
2151 else
2152 {
2153 if (dontThrow)
2154 {
2155 return nullptr;
2156 }
2157 else
2158 {
2159 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2160 }
2161 }
2162 }
2163 case ValueType::intValue:
2164 {
2165 if (cast)
2166 {
2167 return new IntValue(span, moduleId, static_cast<int32_t>(value));
2168 }
2169 else
2170 {
2171 if (dontThrow)
2172 {
2173 return nullptr;
2174 }
2175 else
2176 {
2177 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2178 }
2179 }
2180 }
2181 case ValueType::uintValue:
2182 {
2183 if (cast)
2184 {
2185 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
2186 }
2187 else
2188 {
2189 if (dontThrow)
2190 {
2191 return nullptr;
2192 }
2193 else
2194 {
2195 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2196 }
2197 }
2198 }
2199 case ValueType::longValue:
2200 {
2201 if (cast)
2202 {
2203 return new LongValue(span, moduleId, static_cast<int64_t>(value));
2204 }
2205 else
2206 {
2207 if (dontThrow)
2208 {
2209 return nullptr;
2210 }
2211 else
2212 {
2213 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2214 }
2215 }
2216 }
2217 case ValueType::ulongValue:
2218 {
2219 return new ULongValue(span, moduleId, value);
2220 }
2221 case ValueType::floatValue:
2222 {
2223 return new FloatValue(span, moduleId, value);
2224 }
2225 case ValueType::doubleValue:
2226 {
2227 return new DoubleValue(span, moduleId, value);
2228 }
2229 case ValueType::charValue:
2230 {
2231 if (cast)
2232 {
2233 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
2234 }
2235 else
2236 {
2237 if (dontThrow)
2238 {
2239 return nullptr;
2240 }
2241 else
2242 {
2243 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2244 }
2245 }
2246 }
2247 case ValueType::wcharValue:
2248 {
2249 if (cast)
2250 {
2251 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
2252 }
2253 else
2254 {
2255 if (dontThrow)
2256 {
2257 return nullptr;
2258 }
2259 else
2260 {
2261 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2262 }
2263 }
2264 }
2265 case ValueType::ucharValue:
2266 {
2267 if (cast)
2268 {
2269 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
2270 }
2271 else
2272 {
2273 if (dontThrow)
2274 {
2275 return nullptr;
2276 }
2277 else
2278 {
2279 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2280 }
2281 }
2282 }
2283 default:
2284 {
2285 if (dontThrow)
2286 {
2287 return nullptr;
2288 }
2289 else
2290 {
2291 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
2292 }
2293 }
2294 }
2295 }
2296
2297 TypeSymbol* ULongValue::GetType(SymbolTable* symbolTable)
2298 {
2299 return symbolTable->GetTypeByName(U"ulong");
2300 }
2301
2302 FloatValue::FloatValue(const Span& span_, const boost::uuids::uuid& moduleId_, float value_) : Value(span_, moduleId_, ValueType::floatValue), value(value_)
2303 {
2304 }
2305
2306 void* FloatValue::IrValue(Emitter& emitter)
2307 {
2308 return emitter.CreateIrValueForFloat(value);
2309 }
2310
2311 void FloatValue::Write(BinaryWriter& writer)
2312 {
2313 writer.Write(value);
2314 }
2315
2316 void FloatValue::Read(BinaryReader& reader)
2317 {
2318 value = reader.ReadFloat();
2319 }
2320
2321 Value* FloatValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
2322 {
2323 switch (targetType->GetValueType())
2324 {
2325 case ValueType::boolValue:
2326 {
2327 if (cast)
2328 {
2329 return new BoolValue(span, moduleId, static_cast<bool>(value));
2330 }
2331 else
2332 {
2333 if (dontThrow)
2334 {
2335 return nullptr;
2336 }
2337 else
2338 {
2339 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2340 }
2341 }
2342 }
2343 case ValueType::sbyteValue:
2344 {
2345 if (cast)
2346 {
2347 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
2348 }
2349 else
2350 {
2351 if (dontThrow)
2352 {
2353 return nullptr;
2354 }
2355 else
2356 {
2357 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2358 }
2359 }
2360 }
2361 case ValueType::byteValue:
2362 {
2363 if (cast)
2364 {
2365 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
2366 }
2367 else
2368 {
2369 if (dontThrow)
2370 {
2371 return nullptr;
2372 }
2373 else
2374 {
2375 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2376 }
2377 }
2378 }
2379 case ValueType::shortValue:
2380 {
2381 if (cast)
2382 {
2383 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
2384 }
2385 else
2386 {
2387 if (dontThrow)
2388 {
2389 return nullptr;
2390 }
2391 else
2392 {
2393 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2394 }
2395 }
2396 }
2397 case ValueType::ushortValue:
2398 {
2399 if (cast)
2400 {
2401 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
2402 }
2403 else
2404 {
2405 if (dontThrow)
2406 {
2407 return nullptr;
2408 }
2409 else
2410 {
2411 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2412 }
2413 }
2414 }
2415 case ValueType::intValue:
2416 {
2417 if (cast)
2418 {
2419 return new IntValue(span, moduleId, static_cast<int32_t>(value));
2420 }
2421 else
2422 {
2423 if (dontThrow)
2424 {
2425 return nullptr;
2426 }
2427 else
2428 {
2429 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2430 }
2431 }
2432 }
2433 case ValueType::uintValue:
2434 {
2435 if (cast)
2436 {
2437 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
2438 }
2439 else
2440 {
2441 if (dontThrow)
2442 {
2443 return nullptr;
2444 }
2445 else
2446 {
2447 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2448 }
2449 }
2450 }
2451 case ValueType::longValue:
2452 {
2453 if (cast)
2454 {
2455 return new LongValue(span, moduleId, static_cast<int64_t>(value));
2456 }
2457 else
2458 {
2459 if (dontThrow)
2460 {
2461 return nullptr;
2462 }
2463 else
2464 {
2465 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2466 }
2467 }
2468 }
2469 case ValueType::ulongValue:
2470 {
2471 if (cast)
2472 {
2473 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
2474 }
2475 else
2476 {
2477 if (dontThrow)
2478 {
2479 return nullptr;
2480 }
2481 else
2482 {
2483 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2484 }
2485 }
2486 }
2487 case ValueType::floatValue:
2488 {
2489 return new FloatValue(span, moduleId, value);
2490 }
2491 case ValueType::doubleValue:
2492 {
2493 return new DoubleValue(span, moduleId, value);
2494 }
2495 case ValueType::charValue:
2496 {
2497 if (cast)
2498 {
2499 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
2500 }
2501 else
2502 {
2503 if (dontThrow)
2504 {
2505 return nullptr;
2506 }
2507 else
2508 {
2509 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2510 }
2511 }
2512 }
2513 case ValueType::wcharValue:
2514 {
2515 if (cast)
2516 {
2517 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
2518 }
2519 else
2520 {
2521 if (dontThrow)
2522 {
2523 return nullptr;
2524 }
2525 else
2526 {
2527 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2528 }
2529 }
2530 }
2531 case ValueType::ucharValue:
2532 {
2533 if (cast)
2534 {
2535 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
2536 }
2537 else
2538 {
2539 if (dontThrow)
2540 {
2541 return nullptr;
2542 }
2543 else
2544 {
2545 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2546 }
2547 }
2548 }
2549 default:
2550 {
2551 if (dontThrow)
2552 {
2553 return nullptr;
2554 }
2555 else
2556 {
2557 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
2558 }
2559 }
2560 }
2561 }
2562
2563 TypeSymbol* FloatValue::GetType(SymbolTable* symbolTable)
2564 {
2565 return symbolTable->GetTypeByName(U"float");
2566 }
2567
2568 DoubleValue::DoubleValue(const Span& span_, const boost::uuids::uuid& moduleId_, double value_) : Value(span_, moduleId_, ValueType::doubleValue), value(value_)
2569 {
2570 }
2571
2572 void* DoubleValue::IrValue(Emitter& emitter)
2573 {
2574 return emitter.CreateIrValueForDouble(value);
2575 }
2576
2577 void DoubleValue::Write(BinaryWriter& writer)
2578 {
2579 writer.Write(value);
2580 }
2581
2582 void DoubleValue::Read(BinaryReader& reader)
2583 {
2584 value = reader.ReadDouble();
2585 }
2586
2587 Value* DoubleValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
2588 {
2589 switch (targetType->GetValueType())
2590 {
2591 case ValueType::boolValue:
2592 {
2593 if (cast)
2594 {
2595 return new BoolValue(span, moduleId, static_cast<bool>(value));
2596 }
2597 else
2598 {
2599 if (dontThrow)
2600 {
2601 return nullptr;
2602 }
2603 else
2604 {
2605 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2606 }
2607 }
2608 }
2609 case ValueType::sbyteValue:
2610 {
2611 if (cast)
2612 {
2613 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
2614 }
2615 else
2616 {
2617 if (dontThrow)
2618 {
2619 return nullptr;
2620 }
2621 else
2622 {
2623 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2624 }
2625 }
2626 }
2627 case ValueType::byteValue:
2628 {
2629 if (cast)
2630 {
2631 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
2632 }
2633 else
2634 {
2635 if (dontThrow)
2636 {
2637 return nullptr;
2638 }
2639 else
2640 {
2641 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2642 }
2643 }
2644 }
2645 case ValueType::shortValue:
2646 {
2647 if (cast)
2648 {
2649 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
2650 }
2651 else
2652 {
2653 if (dontThrow)
2654 {
2655 return nullptr;
2656 }
2657 else
2658 {
2659 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2660 }
2661 }
2662 }
2663 case ValueType::ushortValue:
2664 {
2665 if (cast)
2666 {
2667 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
2668 }
2669 else
2670 {
2671 if (dontThrow)
2672 {
2673 return nullptr;
2674 }
2675 else
2676 {
2677 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2678 }
2679 }
2680 }
2681 case ValueType::intValue:
2682 {
2683 if (cast)
2684 {
2685 return new IntValue(span, moduleId, static_cast<int32_t>(value));
2686 }
2687 else
2688 {
2689 if (dontThrow)
2690 {
2691 return nullptr;
2692 }
2693 else
2694 {
2695 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2696 }
2697 }
2698 }
2699 case ValueType::uintValue:
2700 {
2701 if (cast)
2702 {
2703 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
2704 }
2705 else
2706 {
2707 if (dontThrow)
2708 {
2709 return nullptr;
2710 }
2711 else
2712 {
2713 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2714 }
2715 }
2716 }
2717 case ValueType::longValue:
2718 {
2719 if (cast)
2720 {
2721 return new LongValue(span, moduleId, static_cast<int64_t>(value));
2722 }
2723 else
2724 {
2725 if (dontThrow)
2726 {
2727 return nullptr;
2728 }
2729 else
2730 {
2731 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2732 }
2733 }
2734 }
2735 case ValueType::ulongValue:
2736 {
2737 if (cast)
2738 {
2739 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
2740 }
2741 else
2742 {
2743 if (dontThrow)
2744 {
2745 return nullptr;
2746 }
2747 else
2748 {
2749 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2750 }
2751 }
2752 }
2753 case ValueType::floatValue:
2754 {
2755 if (cast)
2756 {
2757 return new FloatValue(span, moduleId, static_cast<float>(value));
2758 }
2759 else
2760 {
2761 if (dontThrow)
2762 {
2763 return nullptr;
2764 }
2765 else
2766 {
2767 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2768 }
2769 }
2770 }
2771 case ValueType::doubleValue:
2772 {
2773 return new DoubleValue(span, moduleId, value);
2774 }
2775 case ValueType::charValue:
2776 {
2777 if (cast)
2778 {
2779 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
2780 }
2781 else
2782 {
2783 if (dontThrow)
2784 {
2785 return nullptr;
2786 }
2787 else
2788 {
2789 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2790 }
2791 }
2792 }
2793 case ValueType::wcharValue:
2794 {
2795 if (cast)
2796 {
2797 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
2798 }
2799 else
2800 {
2801 if (dontThrow)
2802 {
2803 return nullptr;
2804 }
2805 else
2806 {
2807 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2808 }
2809 }
2810 }
2811 case ValueType::ucharValue:
2812 {
2813 if (cast)
2814 {
2815 return new UCharValue(span, moduleId, static_cast<uint32_t>(value));
2816 }
2817 else
2818 {
2819 if (dontThrow)
2820 {
2821 return nullptr;
2822 }
2823 else
2824 {
2825 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2826 }
2827 }
2828 }
2829 default:
2830 {
2831 if (dontThrow)
2832 {
2833 return nullptr;
2834 }
2835 else
2836 {
2837 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
2838 }
2839 }
2840 }
2841 }
2842
2843 TypeSymbol* DoubleValue::GetType(SymbolTable* symbolTable)
2844 {
2845 return symbolTable->GetTypeByName(U"double");
2846 }
2847
2848 CharValue::CharValue(const Span& span_, const boost::uuids::uuid& moduleId_, unsigned char value_) : Value(span_, moduleId_, ValueType::charValue), value(value_)
2849 {
2850 }
2851
2852 void* CharValue::IrValue(Emitter& emitter)
2853 {
2854 return emitter.CreateIrValueForChar(static_cast<uint8_t>(value));
2855 }
2856
2857 void CharValue::Write(BinaryWriter& writer)
2858 {
2859 writer.Write(value);
2860 }
2861
2862 void CharValue::Read(BinaryReader& reader)
2863 {
2864 value = reader.ReadChar();
2865 }
2866
2867 Value* CharValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
2868 {
2869 switch (targetType->GetValueType())
2870 {
2871 case ValueType::boolValue:
2872 {
2873 if (cast)
2874 {
2875 return new BoolValue(span, moduleId, static_cast<bool>(value));
2876 }
2877 else
2878 {
2879 if (dontThrow)
2880 {
2881 return nullptr;
2882 }
2883 else
2884 {
2885 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2886 }
2887 }
2888 }
2889 case ValueType::sbyteValue:
2890 {
2891 if (cast)
2892 {
2893 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
2894 }
2895 else
2896 {
2897 if (dontThrow)
2898 {
2899 return nullptr;
2900 }
2901 else
2902 {
2903 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2904 }
2905 }
2906 }
2907 case ValueType::byteValue:
2908 {
2909 if (cast)
2910 {
2911 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
2912 }
2913 else
2914 {
2915 if (dontThrow)
2916 {
2917 return nullptr;
2918 }
2919 else
2920 {
2921 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2922 }
2923 }
2924 }
2925 case ValueType::shortValue:
2926 {
2927 if (cast)
2928 {
2929 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
2930 }
2931 else
2932 {
2933 if (dontThrow)
2934 {
2935 return nullptr;
2936 }
2937 else
2938 {
2939 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2940 }
2941 }
2942 }
2943 case ValueType::ushortValue:
2944 {
2945 if (cast)
2946 {
2947 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
2948 }
2949 else
2950 {
2951 if (dontThrow)
2952 {
2953 return nullptr;
2954 }
2955 else
2956 {
2957 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2958 }
2959 }
2960 }
2961 case ValueType::intValue:
2962 {
2963 if (cast)
2964 {
2965 return new IntValue(span, moduleId, static_cast<int32_t>(value));
2966 }
2967 else
2968 {
2969 if (dontThrow)
2970 {
2971 return nullptr;
2972 }
2973 else
2974 {
2975 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2976 }
2977 }
2978 }
2979 case ValueType::uintValue:
2980 {
2981 if (cast)
2982 {
2983 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
2984 }
2985 else
2986 {
2987 if (dontThrow)
2988 {
2989 return nullptr;
2990 }
2991 else
2992 {
2993 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
2994 }
2995 }
2996 }
2997 case ValueType::longValue:
2998 {
2999 if (cast)
3000 {
3001 return new LongValue(span, moduleId, static_cast<int64_t>(value));
3002 }
3003 else
3004 {
3005 if (dontThrow)
3006 {
3007 return nullptr;
3008 }
3009 else
3010 {
3011 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3012 }
3013 }
3014 }
3015 case ValueType::ulongValue:
3016 {
3017 if (cast)
3018 {
3019 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
3020 }
3021 else
3022 {
3023 if (dontThrow)
3024 {
3025 return nullptr;
3026 }
3027 else
3028 {
3029 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3030 }
3031 }
3032 }
3033 case ValueType::floatValue:
3034 {
3035 if (cast)
3036 {
3037 return new FloatValue(span, moduleId, static_cast<float>(value));
3038 }
3039 else
3040 {
3041 if (dontThrow)
3042 {
3043 return nullptr;
3044 }
3045 else
3046 {
3047 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3048 }
3049 }
3050 }
3051 case ValueType::doubleValue:
3052 {
3053 if (cast)
3054 {
3055 return new DoubleValue(span, moduleId, static_cast<double>(value));
3056 }
3057 else
3058 {
3059 if (dontThrow)
3060 {
3061 return nullptr;
3062 }
3063 else
3064 {
3065 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3066 }
3067 }
3068 }
3069 case ValueType::charValue:
3070 {
3071 return new CharValue(span, moduleId, value);
3072 }
3073 case ValueType::wcharValue:
3074 {
3075 return new WCharValue(span, moduleId, value);
3076 }
3077 case ValueType::ucharValue:
3078 {
3079 return new UCharValue(span, moduleId, value);
3080 }
3081 default:
3082 {
3083 if (dontThrow)
3084 {
3085 return nullptr;
3086 }
3087 else
3088 {
3089 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
3090 }
3091 }
3092 }
3093 }
3094
3095 TypeSymbol* CharValue::GetType(SymbolTable* symbolTable)
3096 {
3097 return symbolTable->GetTypeByName(U"char");
3098 }
3099
3100 WCharValue::WCharValue(const Span& span_, const boost::uuids::uuid& moduleId_, char16_t value_) : Value(span_, moduleId_, ValueType::wcharValue), value(value_)
3101 {
3102 }
3103
3104 void* WCharValue::IrValue(Emitter& emitter)
3105 {
3106 return emitter.CreateIrValueForWChar(static_cast<uint16_t>(value));
3107 }
3108
3109 void WCharValue::Write(BinaryWriter& writer)
3110 {
3111 writer.Write(value);
3112 }
3113
3114 void WCharValue::Read(BinaryReader& reader)
3115 {
3116 value = reader.ReadWChar();
3117 }
3118
3119 Value* WCharValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
3120 {
3121 switch (targetType->GetValueType())
3122 {
3123 case ValueType::boolValue:
3124 {
3125 if (cast)
3126 {
3127 return new BoolValue(span, moduleId, static_cast<bool>(value));
3128 }
3129 else
3130 {
3131 if (dontThrow)
3132 {
3133 return nullptr;
3134 }
3135 else
3136 {
3137 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3138 }
3139 }
3140 }
3141 case ValueType::sbyteValue:
3142 {
3143 if (cast)
3144 {
3145 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
3146 }
3147 else
3148 {
3149 if (dontThrow)
3150 {
3151 return nullptr;
3152 }
3153 else
3154 {
3155 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3156 }
3157 }
3158 }
3159 case ValueType::byteValue:
3160 {
3161 if (cast)
3162 {
3163 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
3164 }
3165 else
3166 {
3167 if (dontThrow)
3168 {
3169 return nullptr;
3170 }
3171 else
3172 {
3173 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3174 }
3175 }
3176 }
3177 case ValueType::shortValue:
3178 {
3179 if (cast)
3180 {
3181 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
3182 }
3183 else
3184 {
3185 if (dontThrow)
3186 {
3187 return nullptr;
3188 }
3189 else
3190 {
3191 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3192 }
3193 }
3194 }
3195 case ValueType::ushortValue:
3196 {
3197 if (cast)
3198 {
3199 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
3200 }
3201 else
3202 {
3203 if (dontThrow)
3204 {
3205 return nullptr;
3206 }
3207 else
3208 {
3209 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3210 }
3211 }
3212 }
3213 case ValueType::intValue:
3214 {
3215 if (cast)
3216 {
3217 return new IntValue(span, moduleId, static_cast<int32_t>(value));
3218 }
3219 else
3220 {
3221 if (dontThrow)
3222 {
3223 return nullptr;
3224 }
3225 else
3226 {
3227 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3228 }
3229 }
3230 }
3231 case ValueType::uintValue:
3232 {
3233 if (cast)
3234 {
3235 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
3236 }
3237 else
3238 {
3239 if (dontThrow)
3240 {
3241 return nullptr;
3242 }
3243 else
3244 {
3245 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3246 }
3247 }
3248 }
3249 case ValueType::longValue:
3250 {
3251 if (cast)
3252 {
3253 return new LongValue(span, moduleId, static_cast<int64_t>(value));
3254 }
3255 else
3256 {
3257 if (dontThrow)
3258 {
3259 return nullptr;
3260 }
3261 else
3262 {
3263 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3264 }
3265 }
3266 }
3267 case ValueType::ulongValue:
3268 {
3269 if (cast)
3270 {
3271 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
3272 }
3273 else
3274 {
3275 if (dontThrow)
3276 {
3277 return nullptr;
3278 }
3279 else
3280 {
3281 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3282 }
3283 }
3284 }
3285 case ValueType::floatValue:
3286 {
3287 if (cast)
3288 {
3289 return new FloatValue(span, moduleId, static_cast<float>(value));
3290 }
3291 else
3292 {
3293 if (dontThrow)
3294 {
3295 return nullptr;
3296 }
3297 else
3298 {
3299 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3300 }
3301 }
3302 }
3303 case ValueType::doubleValue:
3304 {
3305 if (cast)
3306 {
3307 return new DoubleValue(span, moduleId, static_cast<double>(value));
3308 }
3309 else
3310 {
3311 if (dontThrow)
3312 {
3313 return nullptr;
3314 }
3315 else
3316 {
3317 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3318 }
3319 }
3320 }
3321 case ValueType::charValue:
3322 {
3323 if (cast)
3324 {
3325 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
3326 }
3327 else
3328 {
3329 if (dontThrow)
3330 {
3331 return nullptr;
3332 }
3333 else
3334 {
3335 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3336 }
3337 }
3338 }
3339 case ValueType::wcharValue:
3340 {
3341 return new WCharValue(span, moduleId, value);
3342 }
3343 case ValueType::ucharValue:
3344 {
3345 return new UCharValue(span, moduleId, value);
3346 }
3347 default:
3348 {
3349 if (dontThrow)
3350 {
3351 return nullptr;
3352 }
3353 else
3354 {
3355 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
3356 }
3357 }
3358 }
3359 }
3360
3361 TypeSymbol* WCharValue::GetType(SymbolTable* symbolTable)
3362 {
3363 return symbolTable->GetTypeByName(U"wchar");
3364 }
3365
3366 UCharValue::UCharValue(const Span& span_, const boost::uuids::uuid& moduleId_, char32_t value_) : Value(span_, moduleId_, ValueType::ucharValue), value(value_)
3367 {
3368 }
3369
3370 void* UCharValue::IrValue(Emitter& emitter)
3371 {
3372 return emitter.CreateIrValueForUChar(static_cast<uint32_t>(value));
3373 }
3374
3375 void UCharValue::Write(BinaryWriter& writer)
3376 {
3377 writer.Write(value);
3378 }
3379
3380 void UCharValue::Read(BinaryReader& reader)
3381 {
3382 value = reader.ReadUChar();
3383 }
3384
3385 Value* UCharValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
3386 {
3387 switch (targetType->GetValueType())
3388 {
3389 case ValueType::boolValue:
3390 {
3391 if (cast)
3392 {
3393 return new BoolValue(span, moduleId, static_cast<bool>(value));
3394 }
3395 else
3396 {
3397 if (dontThrow)
3398 {
3399 return nullptr;
3400 }
3401 else
3402 {
3403 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3404 }
3405 }
3406 }
3407 case ValueType::sbyteValue:
3408 {
3409 if (cast)
3410 {
3411 return new SByteValue(span, moduleId, static_cast<int8_t>(value));
3412 }
3413 else
3414 {
3415 if (dontThrow)
3416 {
3417 return nullptr;
3418 }
3419 else
3420 {
3421 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3422 }
3423 }
3424 }
3425 case ValueType::byteValue:
3426 {
3427 if (cast)
3428 {
3429 return new ByteValue(span, moduleId, static_cast<uint8_t>(value));
3430 }
3431 else
3432 {
3433 if (dontThrow)
3434 {
3435 return nullptr;
3436 }
3437 else
3438 {
3439 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3440 }
3441 }
3442 }
3443 case ValueType::shortValue:
3444 {
3445 if (cast)
3446 {
3447 return new ShortValue(span, moduleId, static_cast<int16_t>(value));
3448 }
3449 else
3450 {
3451 if (dontThrow)
3452 {
3453 return nullptr;
3454 }
3455 else
3456 {
3457 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3458 }
3459 }
3460 }
3461 case ValueType::ushortValue:
3462 {
3463 if (cast)
3464 {
3465 return new UShortValue(span, moduleId, static_cast<uint16_t>(value));
3466 }
3467 else
3468 {
3469 if (dontThrow)
3470 {
3471 return nullptr;
3472 }
3473 else
3474 {
3475 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3476 }
3477 }
3478 }
3479 case ValueType::intValue:
3480 {
3481 if (cast)
3482 {
3483 return new IntValue(span, moduleId, static_cast<int32_t>(value));
3484 }
3485 else
3486 {
3487 if (dontThrow)
3488 {
3489 return nullptr;
3490 }
3491 else
3492 {
3493 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3494 }
3495 }
3496 }
3497 case ValueType::uintValue:
3498 {
3499 if (cast)
3500 {
3501 return new UIntValue(span, moduleId, static_cast<uint32_t>(value));
3502 }
3503 else
3504 {
3505 if (dontThrow)
3506 {
3507 return nullptr;
3508 }
3509 else
3510 {
3511 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3512 }
3513 }
3514 }
3515 case ValueType::longValue:
3516 {
3517 if (cast)
3518 {
3519 return new LongValue(span, moduleId, static_cast<int64_t>(value));
3520 }
3521 else
3522 {
3523 if (dontThrow)
3524 {
3525 return nullptr;
3526 }
3527 else
3528 {
3529 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3530 }
3531 }
3532 }
3533 case ValueType::ulongValue:
3534 {
3535 if (cast)
3536 {
3537 return new ULongValue(span, moduleId, static_cast<uint64_t>(value));
3538 }
3539 else
3540 {
3541 if (dontThrow)
3542 {
3543 return nullptr;
3544 }
3545 else
3546 {
3547 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3548 }
3549 }
3550 }
3551 case ValueType::floatValue:
3552 {
3553 if (cast)
3554 {
3555 return new FloatValue(span, moduleId, static_cast<float>(value));
3556 }
3557 else
3558 {
3559 if (dontThrow)
3560 {
3561 return nullptr;
3562 }
3563 else
3564 {
3565 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3566 }
3567 }
3568 }
3569 case ValueType::doubleValue:
3570 {
3571 if (cast)
3572 {
3573 return new DoubleValue(span, moduleId, static_cast<double>(value));
3574 }
3575 else
3576 {
3577 if (dontThrow)
3578 {
3579 return nullptr;
3580 }
3581 else
3582 {
3583 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3584 }
3585 }
3586 }
3587 case ValueType::charValue:
3588 {
3589 if (cast)
3590 {
3591 return new CharValue(span, moduleId, static_cast<uint8_t>(value));
3592 }
3593 else
3594 {
3595 if (dontThrow)
3596 {
3597 return nullptr;
3598 }
3599 else
3600 {
3601 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3602 }
3603 }
3604 }
3605 case ValueType::wcharValue:
3606 {
3607 if (cast)
3608 {
3609 return new WCharValue(span, moduleId, static_cast<uint16_t>(value));
3610 }
3611 else
3612 {
3613 if (dontThrow)
3614 {
3615 return nullptr;
3616 }
3617 else
3618 {
3619 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3620 }
3621 }
3622 }
3623 case ValueType::ucharValue:
3624 {
3625 return new UCharValue(span, moduleId, value);
3626 }
3627 default:
3628 {
3629 if (dontThrow)
3630 {
3631 return nullptr;
3632 }
3633 else
3634 {
3635 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
3636 }
3637 }
3638 }
3639 }
3640
3641 TypeSymbol* UCharValue::GetType(SymbolTable* symbolTable)
3642 {
3643 return symbolTable->GetTypeByName(U"uchar");
3644 }
3645
3646 StringValue::StringValue(const Span& span_, const boost::uuids::uuid& moduleId_, int stringId_, const std::string& str_) : Value(span_, moduleId_, ValueType::stringValue), stringId(stringId_), str(str_)
3647 {
3648 }
3649
3650 void* StringValue::IrValue(Emitter& emitter)
3651 {
3652 if (stringId == -1)
3653 {
3654 stringId = emitter.Install(str);
3655 }
3656 return emitter.GetGlobalStringPtr(stringId);
3657 }
3658
3659 void StringValue::Write(BinaryWriter& writer)
3660 {
3661 writer.Write(str);
3662 }
3663
3664 void StringValue::Read(BinaryReader& reader)
3665 {
3666 str = reader.ReadUtf8String();
3667 }
3668
3669 Value* StringValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
3670 {
3671 switch (targetType->GetValueType())
3672 {
3673 case ValueType::stringValue:
3674 {
3675 return new StringValue(span, moduleId, stringId, str);
3676 }
3677 default:
3678 {
3679 if (dontThrow)
3680 {
3681 return nullptr;
3682 }
3683 else
3684 {
3685 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
3686 }
3687 }
3688 }
3689 }
3690
3691 TypeSymbol* StringValue::GetType(SymbolTable* symbolTable)
3692 {
3693 return symbolTable->GetTypeByName(U"char")->AddConst(GetSpan(), ModuleId())->AddPointer(GetSpan(), ModuleId());
3694 }
3695
3696 WStringValue::WStringValue(const Span& span_, const boost::uuids::uuid& moduleId_, int stringId_, const std::u16string& str_) : Value(span_, moduleId_, ValueType::wstringValue), stringId(stringId_), str(str_)
3697 {
3698 }
3699
3700 void* WStringValue::IrValue(Emitter& emitter)
3701 {
3702 if (stringId == -1)
3703 {
3704 stringId = emitter.Install(str);
3705 }
3706 void* wstringConstant = emitter.GetGlobalWStringConstant(stringId);
3707 return emitter.CreateIrValueForWString(wstringConstant);
3708 }
3709
3710 void WStringValue::Write(BinaryWriter& writer)
3711 {
3712 }
3713
3714 void WStringValue::Read(BinaryReader& reader)
3715 {
3716 }
3717
3718 Value* WStringValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
3719 {
3720 switch (targetType->GetValueType())
3721 {
3722 case ValueType::wstringValue:
3723 {
3724 return new WStringValue(span, moduleId, stringId, str);
3725 }
3726 default:
3727 {
3728 if (dontThrow)
3729 {
3730 return nullptr;
3731 }
3732 else
3733 {
3734 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
3735 }
3736 }
3737 }
3738 }
3739
3740 TypeSymbol* WStringValue::GetType(SymbolTable* symbolTable)
3741 {
3742 return symbolTable->GetTypeByName(U"wchar")->AddConst(GetSpan(), ModuleId())->AddPointer(GetSpan(), ModuleId());
3743 }
3744
3745 UStringValue::UStringValue(const Span& span_, const boost::uuids::uuid& moduleId_, int stringId_, const std::u32string& str_) : Value(span_, moduleId_, ValueType::ustringValue), stringId(stringId_), str(str_)
3746 {
3747 }
3748
3749 void* UStringValue::IrValue(Emitter& emitter)
3750 {
3751 if (stringId == -1)
3752 {
3753 stringId = emitter.Install(str);
3754 }
3755 void* ustringConstant = emitter.GetGlobalUStringConstant(stringId);
3756 return emitter.CreateIrValueForUString(ustringConstant);
3757 }
3758
3759 void UStringValue::Write(BinaryWriter& writer)
3760 {
3761 }
3762
3763 void UStringValue::Read(BinaryReader& reader)
3764 {
3765 }
3766
3767 Value* UStringValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
3768 {
3769 switch (targetType->GetValueType())
3770 {
3771 case ValueType::ustringValue:
3772 {
3773 return new UStringValue(span, moduleId, stringId, str);
3774 }
3775 default:
3776 {
3777 if (dontThrow)
3778 {
3779 return nullptr;
3780 }
3781 else
3782 {
3783 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
3784 }
3785 }
3786 }
3787 }
3788
3789 TypeSymbol* UStringValue::GetType(SymbolTable* symbolTable)
3790 {
3791 return symbolTable->GetTypeByName(U"uchar")->AddConst(GetSpan(), ModuleId())->AddPointer(GetSpan(), ModuleId());
3792 }
3793
3794 NullValue::NullValue(const Span& span_, const boost::uuids::uuid& moduleId_, TypeSymbol* nullPtrType_) : Value(span_, moduleId_, ValueType::nullValue), nullPtrType(nullPtrType_)
3795 {
3796 }
3797
3798 void* NullValue::IrValue(Emitter& emitter)
3799 {
3800 return emitter.CreateDefaultIrValueForPtrType(nullPtrType->IrType(emitter));
3801 }
3802
3803 void NullValue::Write(BinaryWriter& writer)
3804 {
3805 }
3806
3807 void NullValue::Read(BinaryReader& reader)
3808 {
3809 }
3810
3811 Value* NullValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
3812 {
3813 switch (targetType->GetValueType())
3814 {
3815 case ValueType::nullValue:
3816 {
3817 return new NullValue(span, moduleId, nullPtrType);
3818 }
3819 case ValueType::pointerValue:
3820 {
3821 if (targetType->IsPointerType())
3822 {
3823 return new PointerValue(span, moduleId, targetType, nullptr);
3824 }
3825 else
3826 {
3827 if (dontThrow)
3828 {
3829 return nullptr;
3830 }
3831 else
3832 {
3833 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
3834 }
3835 }
3836 }
3837 default:
3838 {
3839 if (dontThrow)
3840 {
3841 return nullptr;
3842 }
3843 else
3844 {
3845 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
3846 }
3847 }
3848 }
3849 }
3850
3851 TypeSymbol* NullValue::GetType(SymbolTable* symbolTable)
3852 {
3853 return nullPtrType;
3854 }
3855
3856 PointerValue::PointerValue(const Span& span_, const boost::uuids::uuid& moduleId_, TypeSymbol* type_, const void* ptr_) : Value(span_, moduleId_, ValueType::pointerValue), type(type_), ptr(ptr_)
3857 {
3858 }
3859
3860 void* PointerValue::IrValue(Emitter& emitter)
3861 {
3862 if (ptr)
3863 {
3864 throw std::runtime_error("IrValue for non-null pointers not supported");
3865 }
3866 else
3867 {
3868 return emitter.CreateDefaultIrValueForPtrType(type->IrType(emitter));
3869 }
3870 }
3871
3872 void PointerValue::Write(BinaryWriter& writer)
3873 {
3874 }
3875
3876 void PointerValue::Read(BinaryReader& reader)
3877 {
3878 ptr = nullptr;
3879 }
3880
3881 Value* PointerValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
3882 {
3883 switch (targetType->GetValueType())
3884 {
3885 case ValueType::pointerValue:
3886 {
3887 if (cast)
3888 {
3889 return new PointerValue(span, moduleId, targetType, ptr);
3890 }
3891 else
3892 {
3893 if (dontThrow)
3894 {
3895 return nullptr;
3896 }
3897 else
3898 {
3899 throw Exception("cannot convert " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " without a cast", span, moduleId);
3900 }
3901 }
3902 }
3903 default:
3904 {
3905 if (dontThrow)
3906 {
3907 return nullptr;
3908 }
3909 else
3910 {
3911 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
3912 }
3913 }
3914 }
3915 }
3916
3917 TypeSymbol* PointerValue::GetType(SymbolTable* symbolTable)
3918 {
3919 return type;
3920 }
3921
3922 TypeSymbol* PointerValue::PointeeType() const
3923 {
3924 return type->RemovePointer(GetSpan(), ModuleId());
3925 }
3926
3927 Value* PointerValue::Add(int64_t offset) const
3928 {
3929 ValueType pointeeValueType = PointeeType()->GetValueType();
3930 switch (pointeeValueType)
3931 {
3932 case ValueType::boolValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const BoolValue::OperandType*>(ptr) + offset);
3933 case ValueType::sbyteValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const SByteValue::OperandType*>(ptr) + offset);
3934 case ValueType::byteValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const ByteValue::OperandType*>(ptr) + offset);
3935 case ValueType::shortValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const ShortValue::OperandType*>(ptr) + offset);
3936 case ValueType::ushortValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const UShortValue::OperandType*>(ptr) + offset);
3937 case ValueType::intValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const IntValue::OperandType*>(ptr) + offset);
3938 case ValueType::uintValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const UIntValue::OperandType*>(ptr) + offset);
3939 case ValueType::longValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const LongValue::OperandType*>(ptr) + offset);
3940 case ValueType::ulongValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const ULongValue::OperandType*>(ptr) + offset);
3941 case ValueType::floatValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const FloatValue::OperandType*>(ptr) + offset);
3942 case ValueType::doubleValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const DoubleValue::OperandType*>(ptr) + offset);
3943 case ValueType::charValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const CharValue::OperandType*>(ptr) + offset);
3944 case ValueType::wcharValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const WCharValue::OperandType*>(ptr) + offset);
3945 case ValueType::ucharValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const UCharValue::OperandType*>(ptr) + offset);
3946 case ValueType::pointerValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const PointerValue::OperandType*>(ptr) + offset);
3947 }
3948 return nullptr;
3949 }
3950
3951 Value* PointerValue::Sub(int64_t offset) const
3952 {
3953 ValueType pointeeValueType = PointeeType()->GetValueType();
3954 switch (pointeeValueType)
3955 {
3956 case ValueType::boolValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const BoolValue::OperandType*>(ptr) - offset);
3957 case ValueType::sbyteValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const SByteValue::OperandType*>(ptr) - offset);
3958 case ValueType::byteValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const ByteValue::OperandType*>(ptr) - offset);
3959 case ValueType::shortValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const ShortValue::OperandType*>(ptr) - offset);
3960 case ValueType::ushortValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const UShortValue::OperandType*>(ptr) - offset);
3961 case ValueType::intValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const IntValue::OperandType*>(ptr) - offset);
3962 case ValueType::uintValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const UIntValue::OperandType*>(ptr) - offset);
3963 case ValueType::longValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const LongValue::OperandType*>(ptr) - offset);
3964 case ValueType::ulongValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const ULongValue::OperandType*>(ptr) - offset);
3965 case ValueType::floatValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const FloatValue::OperandType*>(ptr) - offset);
3966 case ValueType::doubleValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const DoubleValue::OperandType*>(ptr) - offset);
3967 case ValueType::charValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const CharValue::OperandType*>(ptr) - offset);
3968 case ValueType::wcharValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const WCharValue::OperandType*>(ptr) - offset);
3969 case ValueType::ucharValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const UCharValue::OperandType*>(ptr) - offset);
3970 case ValueType::pointerValue: return new PointerValue(GetSpan(), ModuleId(), type, static_cast<const PointerValue::OperandType*>(ptr) - offset);
3971 }
3972 return nullptr;
3973 }
3974
3975 Value* PointerValue::Sub(const void* thatPtr) const
3976 {
3977 ValueType pointeeValueType = PointeeType()->GetValueType();
3978 switch (pointeeValueType)
3979 {
3980 case ValueType::boolValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const BoolValue::OperandType*>(ptr) - static_cast<const BoolValue::OperandType*>(thatPtr));
3981 case ValueType::sbyteValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const SByteValue::OperandType*>(ptr) - static_cast<const SByteValue::OperandType*>(thatPtr));
3982 case ValueType::byteValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const ByteValue::OperandType*>(ptr) - static_cast<const ByteValue::OperandType*>(thatPtr));
3983 case ValueType::shortValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const ShortValue::OperandType*>(ptr) - static_cast<const ShortValue::OperandType*>(thatPtr));
3984 case ValueType::ushortValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const UShortValue::OperandType*>(ptr) - static_cast<const UShortValue::OperandType*>(thatPtr));
3985 case ValueType::intValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const IntValue::OperandType*>(ptr) - static_cast<const IntValue::OperandType*>(thatPtr));
3986 case ValueType::uintValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const UIntValue::OperandType*>(ptr) - static_cast<const UIntValue::OperandType*>(thatPtr));
3987 case ValueType::longValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const LongValue::OperandType*>(ptr) - static_cast<const LongValue::OperandType*>(thatPtr));
3988 case ValueType::ulongValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const ULongValue::OperandType*>(ptr) - static_cast<const ULongValue::OperandType*>(thatPtr));
3989 case ValueType::floatValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const FloatValue::OperandType*>(ptr) - static_cast<const FloatValue::OperandType*>(thatPtr));
3990 case ValueType::doubleValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const DoubleValue::OperandType*>(ptr) - static_cast<const DoubleValue::OperandType*>(thatPtr));
3991 case ValueType::charValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const CharValue::OperandType*>(ptr) - static_cast<const CharValue::OperandType*>(thatPtr));
3992 case ValueType::wcharValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const WCharValue::OperandType*>(ptr) - static_cast<const WCharValue::OperandType*>(thatPtr));
3993 case ValueType::ucharValue: return new LongValue(GetSpan(), ModuleId(), static_cast<const UCharValue::OperandType*>(ptr) - static_cast<const UCharValue::OperandType*>(thatPtr));
3994 }
3995 return nullptr;
3996 }
3997
3998 Value* PointerValue::Deref() const
3999 {
4000 ValueType pointeeValueType = PointeeType()->GetValueType();
4001 switch (pointeeValueType)
4002 {
4003 case ValueType::boolValue: return new BoolValue(GetSpan(), ModuleId(), *static_cast<const BoolValue::OperandType*>(ptr));
4004 case ValueType::sbyteValue: return new SByteValue(GetSpan(), ModuleId(), *static_cast<const SByteValue::OperandType*>(ptr));
4005 case ValueType::byteValue: return new ByteValue(GetSpan(), ModuleId(), *static_cast<const ByteValue::OperandType*>(ptr));
4006 case ValueType::shortValue: return new ShortValue(GetSpan(), ModuleId(), *static_cast<const ShortValue::OperandType*>(ptr));
4007 case ValueType::ushortValue: return new UShortValue(GetSpan(), ModuleId(), *static_cast<const UShortValue::OperandType*>(ptr));
4008 case ValueType::intValue: return new IntValue(GetSpan(), ModuleId(), *static_cast<const IntValue::OperandType*>(ptr));
4009 case ValueType::uintValue: return new UIntValue(GetSpan(), ModuleId(), *static_cast<const UIntValue::OperandType*>(ptr));
4010 case ValueType::longValue: return new LongValue(GetSpan(), ModuleId(), *static_cast<const LongValue::OperandType*>(ptr));
4011 case ValueType::ulongValue: return new ULongValue(GetSpan(), ModuleId(), *static_cast<const ULongValue::OperandType*>(ptr));
4012 case ValueType::floatValue: return new FloatValue(GetSpan(), ModuleId(), *static_cast<const FloatValue::OperandType*>(ptr));
4013 case ValueType::doubleValue: return new DoubleValue(GetSpan(), ModuleId(), *static_cast<const DoubleValue::OperandType*>(ptr));
4014 case ValueType::charValue: return new CharValue(GetSpan(), ModuleId(), *static_cast<const CharValue::OperandType*>(ptr));
4015 case ValueType::wcharValue: return new WCharValue(GetSpan(), ModuleId(), *static_cast<const WCharValue::OperandType*>(ptr));
4016 case ValueType::ucharValue: return new UCharValue(GetSpan(), ModuleId(), *static_cast<const UCharValue::OperandType*>(ptr));
4017 }
4018 return nullptr;
4019 }
4020
4021 ArrayValue::ArrayValue(const Span& span_, const boost::uuids::uuid& moduleId_, TypeSymbol* type_, std::std::vector<std::std::unique_ptr<Value>>&&elementValues_):
4022 Value(span_, moduleId_, ValueType::arrayValue), type(type_), elementValues(std::move(elementValues_))
4023 {
4024 }
4025
4026 Value* ArrayValue::Clone() const
4027 {
4028 std::vector<std::std::unique_ptr<Value>>clonedElementValues;
4029 int64_t n = elementValues.size();
4030 for (int64_t i = 0; i < n; ++i)
4031 {
4032 clonedElementValues.push_back(std::unique_ptr<Value>(elementValues[i]->Clone()));
4033 }
4034 return new ArrayValue(GetSpan(), ModuleId(), type, std::move(clonedElementValues));
4035 }
4036
4037 void* ArrayValue::IrValue(Emitter& emitter)
4038 {
4039 std::vector<void*> elementConstants;
4040 int64_t n = elementValues.size();
4041 for (int64_t i = 0; i < n; ++i)
4042 {
4043 elementConstants.push_back(elementValues[i]->IrValue(emitter));
4044 }
4045 return emitter.CreateIrValueForConstantArray(type->IrType(emitter), elementConstants, std::string());
4046 }
4047
4048 void ArrayValue::Write(BinaryWriter& writer)
4049 {
4050 int64_t length = elementValues.size();
4051 writer.Write(length);
4052 for (int64_t i = 0; i < length; ++i)
4053 {
4054 Value* value = elementValues[i].get();
4055 value->Write(writer);
4056 }
4057 }
4058
4059 void ArrayValue::Read(BinaryReader& reader)
4060 {
4061 int64_t length = reader.ReadLong();
4062 for (int64_t i = 0; i < length; ++i)
4063 {
4064 Value* elementValue = static_cast<ArrayTypeSymbol*>(type)->ElementType()->MakeValue();
4065 elementValue->Read(reader);
4066 elementValues.push_back(std::unique_ptr<Value>(elementValue));
4067 }
4068 }
4069
4070 Value* ArrayValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
4071 {
4072 if (TypesEqual(targetType, type))
4073 {
4074 return Clone();
4075 }
4076 else
4077 {
4078 if (dontThrow)
4079 {
4080 return nullptr;
4081 }
4082 else
4083 {
4084 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
4085 }
4086 }
4087 }
4088
4089 StructuredValue::StructuredValue(const Span& span_, const boost::uuids::uuid& moduleId_, TypeSymbol* type_, std::std::vector<std::std::unique_ptr<Value>>&&memberValues_):
4090 Value(span_, moduleId_, ValueType::structuredValue), type(type_), memberValues(std::move(memberValues_))
4091 {
4092 }
4093
4094 Value* StructuredValue::Clone() const
4095 {
4096 std::vector<std::std::unique_ptr<Value>>clonedMemberValues;
4097 int n = memberValues.size();
4098 for (int i = 0; i < n; ++i)
4099 {
4100 std::unique_ptr<Value> memberValue(memberValues[i]->Clone());
4101 clonedMemberValues.push_back(std::move(memberValue));
4102 }
4103 return new StructuredValue(GetSpan(), ModuleId(), type, std::move(clonedMemberValues));
4104 }
4105
4106 void* StructuredValue::IrValue(Emitter& emitter)
4107 {
4108 std::vector<void*> memberConstants;
4109 int64_t n = memberValues.size();
4110 if (n == 0)
4111 {
4112 ClassTypeSymbol* classType = static_cast<ClassTypeSymbol*>(type);
4113 const std::std::vector<TypeSymbol*>&objectLayout=classType->ObjectLayout();
4114 n = objectLayout.size();
4115 for (int64_t i = 0; i < n; ++i)
4116 {
4117 TypeSymbol* type = objectLayout[i];
4118 memberConstants.push_back(type->CreateDefaultIrValue(emitter));
4119 }
4120 }
4121 else
4122 {
4123 for (int64_t i = 0; i < n; ++i)
4124 {
4125 memberConstants.push_back(memberValues[i]->IrValue(emitter));
4126 }
4127 }
4128 return emitter.CreateIrValueForConstantStruct(type->IrType(emitter), memberConstants);
4129 }
4130
4131 void StructuredValue::Write(BinaryWriter& writer)
4132 {
4133 for (const std::std::unique_ptr<Value>&memberValue : memberValues)
4134 {
4135 memberValue->Write(writer);
4136 }
4137 }
4138
4139 void StructuredValue::Read(BinaryReader& reader)
4140 {
4141 for (MemberVariableSymbol* memberVariable : static_cast<ClassTypeSymbol*>(type)->MemberVariables())
4142 {
4143 Value* memberValue = memberVariable->GetType()->MakeValue();
4144 memberValue->Read(reader);
4145 memberValues.push_back(std::unique_ptr<Value>(memberValue));
4146 }
4147 }
4148
4149 Value* StructuredValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
4150 {
4151 if (TypesEqual(targetType, type))
4152 {
4153 return Clone();
4154 }
4155 else
4156 {
4157 if (dontThrow)
4158 {
4159 return nullptr;
4160 }
4161 else
4162 {
4163 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
4164 }
4165 }
4166 }
4167
4168 UuidValue::UuidValue(const Span& span_, const boost::uuids::uuid& moduleId_, int uuidId_) : Value(span_, moduleId_, ValueType::uuidValue), uuidId(uuidId_)
4169 {
4170 }
4171
4172 void* UuidValue::IrValue(Emitter& emitter)
4173 {
4174 void* uuidConstant = emitter.GetGlobalUuidConstant(uuidId);
4175 return emitter.CreateIrValueForUuid(uuidConstant);
4176 }
4177
4178 void UuidValue::Write(BinaryWriter& writer)
4179 {
4180 Assert(false, "write for uuid value not supported");
4181 }
4182
4183 void UuidValue::Read(BinaryReader& reader)
4184 {
4185 Assert(false, "read for uuid value not supported");
4186 }
4187
4188 Value* UuidValue::As(TypeSymbol* targetType, bool cast, const Span& span, const boost::uuids::uuid& moduleId, bool dontThrow) const
4189 {
4190 switch (targetType->GetValueType())
4191 {
4192 case ValueType::uuidValue:
4193 {
4194 return new UuidValue(span, moduleId, uuidId);
4195 }
4196 default:
4197 {
4198 if (dontThrow)
4199 {
4200 return nullptr;
4201 }
4202 else
4203 {
4204 throw Exception("conversion from " + ValueTypeStr(GetValueType()) + " to " + ValueTypeStr(targetType->GetValueType()) + " is not valid", span, moduleId);
4205 }
4206 }
4207 }
4208 }
4209
4210 TypeSymbol* UuidValue::GetType(SymbolTable* symbolTable)
4211 {
4212 return symbolTable->GetTypeByName(U"void")->AddPointer(GetSpan(), ModuleId());
4213 }
4214
4215 bool operator==(IntegralValue left, IntegralValue right)
4216 {
4217 if (left.value->GetValueType() != right.value->GetValueType()) return false;
4218 switch (left.value->GetValueType())
4219 {
4220 case ValueType::boolValue: return ValuesEqual(*static_cast<BoolValue*>(left.value), *static_cast<BoolValue*>(right.value));
4221 case ValueType::sbyteValue: return ValuesEqual(*static_cast<SByteValue*>(left.value), *static_cast<SByteValue*>(right.value));
4222 case ValueType::byteValue: return ValuesEqual(*static_cast<ByteValue*>(left.value), *static_cast<ByteValue*>(right.value));
4223 case ValueType::shortValue: return ValuesEqual(*static_cast<ShortValue*>(left.value), *static_cast<ShortValue*>(right.value));
4224 case ValueType::ushortValue: return ValuesEqual(*static_cast<UShortValue*>(left.value), *static_cast<UShortValue*>(right.value));
4225 case ValueType::intValue: return ValuesEqual(*static_cast<IntValue*>(left.value), *static_cast<IntValue*>(right.value));
4226 case ValueType::uintValue: return ValuesEqual(*static_cast<UIntValue*>(left.value), *static_cast<UIntValue*>(right.value));
4227 case ValueType::longValue: return ValuesEqual(*static_cast<LongValue*>(left.value), *static_cast<LongValue*>(right.value));
4228 case ValueType::ulongValue: return ValuesEqual(*static_cast<ULongValue*>(left.value), *static_cast<ULongValue*>(right.value));
4229 case ValueType::charValue: return ValuesEqual(*static_cast<CharValue*>(left.value), *static_cast<CharValue*>(right.value));
4230 case ValueType::wcharValue: return ValuesEqual(*static_cast<WCharValue*>(left.value), *static_cast<WCharValue*>(right.value));
4231 case ValueType::ucharValue: return ValuesEqual(*static_cast<UCharValue*>(left.value), *static_cast<UCharValue*>(right.value));
4232 }
4233 return false;
4234 }
4235
4236 size_t IntegralValueHash::operator()(IntegralValue integralValue) const
4237 {
4238 switch (integralValue.value->GetValueType())
4239 {
4240 case ValueType::boolValue: return GetHashCode(*static_cast<BoolValue*>(integralValue.value));
4241 case ValueType::sbyteValue: return GetHashCode(*static_cast<SByteValue*>(integralValue.value));
4242 case ValueType::byteValue: return GetHashCode(*static_cast<ByteValue*>(integralValue.value));
4243 case ValueType::shortValue: return GetHashCode(*static_cast<ShortValue*>(integralValue.value));
4244 case ValueType::ushortValue: return GetHashCode(*static_cast<UShortValue*>(integralValue.value));
4245 case ValueType::intValue: return GetHashCode(*static_cast<IntValue*>(integralValue.value));
4246 case ValueType::uintValue: return GetHashCode(*static_cast<UIntValue*>(integralValue.value));
4247 case ValueType::longValue: return GetHashCode(*static_cast<LongValue*>(integralValue.value));
4248 case ValueType::ulongValue: return GetHashCode(*static_cast<ULongValue*>(integralValue.value));
4249 case ValueType::charValue: return GetHashCode(*static_cast<CharValue*>(integralValue.value));
4250 case ValueType::wcharValue: return GetHashCode(*static_cast<WCharValue*>(integralValue.value));
4251 case ValueType::ucharValue: return GetHashCode(*static_cast<UCharValue*>(integralValue.value));
4252 }
4253 return 0;
4254 }
4255
4256 void WriteValue(Value* value, BinaryWriter& writer)
4257 {
4258 writer.Write(static_cast<uint8_t>(value->GetValueType()));
4259 value->Write(writer);
4260 }
4261
4262 std::std::unique_ptr<Value>ReadValue(BinaryReader&reader, constSpan&span, constboost::uuids::uuid& moduleId)
4263 {
4264 ValueType valueType = static_cast<ValueType>(reader.ReadByte());
4265 std::unique_ptr<Value> value;
4266 switch (valueType)
4267 {
4268 case ValueType::boolValue: value.reset(new BoolValue(Span(), boost::uuids::nil_uuid(), false)); break;
4269 case ValueType::sbyteValue: value.reset(new SByteValue(Span(), boost::uuids::nil_uuid(), 0)); break;
4270 case ValueType::byteValue: value.reset(new ByteValue(Span(), boost::uuids::nil_uuid(), 0)); break;
4271 case ValueType::shortValue: value.reset(new ShortValue(Span(), boost::uuids::nil_uuid(), 0)); break;
4272 case ValueType::ushortValue: value.reset(new UShortValue(Span(), boost::uuids::nil_uuid(), 0)); break;
4273 case ValueType::intValue: value.reset(new IntValue(Span(), boost::uuids::nil_uuid(), 0)); break;
4274 case ValueType::uintValue: value.reset(new UIntValue(Span(), boost::uuids::nil_uuid(), 0)); break;
4275 case ValueType::longValue: value.reset(new LongValue(Span(), boost::uuids::nil_uuid(), 0)); break;
4276 case ValueType::ulongValue: value.reset(new ULongValue(Span(), boost::uuids::nil_uuid(), 0)); break;
4277 case ValueType::floatValue: value.reset(new FloatValue(Span(), boost::uuids::nil_uuid(), 0.0)); break;
4278 case ValueType::doubleValue: value.reset(new DoubleValue(Span(), boost::uuids::nil_uuid(), 0.0)); break;
4279 case ValueType::charValue: value.reset(new CharValue(Span(), boost::uuids::nil_uuid(), '\0')); break;
4280 case ValueType::wcharValue: value.reset(new WCharValue(Span(), boost::uuids::nil_uuid(), '\0')); break;
4281 case ValueType::ucharValue: value.reset(new UCharValue(Span(), boost::uuids::nil_uuid(), '\0')); break;
4282 case ValueType::pointerValue: value.reset(new PointerValue(Span(), boost::uuids::nil_uuid(), nullptr, nullptr)); break;
4283 case ValueType::stringValue: value.reset(new StringValue(Span(), boost::uuids::nil_uuid(), -1, ""));
4284 case ValueType::wstringValue: value.reset(new WStringValue(Span(), boost::uuids::nil_uuid(), -1, u""));
4285 case ValueType::ustringValue: value.reset(new UStringValue(Span(), boost::uuids::nil_uuid(), -1, U""));
4286 }
4287 if (value)
4288 {
4289 value->Read(reader);
4290 return value;
4291 }
4292 else
4293 {
4294 throw Exception( "internal error: could not read value of type '" + ValueTypeStr(valueType) + "'", span, moduleId);
4295 }
4296 }
4297
4298 } }