Blender V5.3
type_checker.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "type_checker.hh"
10#include "pratt_parser.hh"
11#include "symbol_table.hh"
12
13#include <bit>
14
15namespace bsl {
16
22 : public PrattParser<ExpressionTypeParser, ExpressionResult, ast::Node> {
25 using BindingPower = Base::BindingPower;
26
27 private:
28 std::optional<AstNodeException> err;
29 const SymbolTable *table;
30 const SymbolScope *scope;
31 ast::Node start;
32 ast::Node node;
33
34 /* The expression is supposed to be bound to a reference variable. */
35 bool is_reference;
36
37 public:
39 const SymbolScope *scope,
40 ast::Node expr_first_child,
41 bool is_reference = false)
42 : table(table),
43 scope(scope),
44 start(expr_first_child),
45 node(expr_first_child),
46 is_reference(is_reference)
47 {
48 }
49
51 {
52 ExpressionResult result = expr(0);
53
54 if (peek() != Invalid && !err) {
56 }
57 return {result, err};
58 }
59
63
65 {
66 switch (t.type()) {
67 case ast::NodeType::Constructor: {
68 ast::Constructor ctor = t;
69 return scope->lookup_class(*table, ctor.identifier()).unwrap(err);
70 }
71 case ast::NodeType::FuncCall: {
72 ast::FuncCall call = t;
73
74 /* Legacy BSL. */
75 if (call.identifier().str() == "resource_table_get") {
76 auto [param] = call.parameters().splat_1();
77 if (ast::LocalVar var = param.child_first(); var.is_valid()) {
78 return scope->lookup_class(*table, var.identifier()).unwrap(err);
79 }
81 return {table->err_cls, ConstexprError(0), false};
82 }
83 if (call.identifier().str() == "interface_get" ||
84 call.identifier().str() == "sampler_get" || call.identifier().str() == "buffer_get")
85 {
86 auto [param1, param2] = call.parameters().splat_2();
87 if (ast::LocalVar var = param1.child_first(), mem = param2.child_first();
88 var.is_valid() && mem.is_valid())
89 {
90 SymbolClass *cls = scope->lookup_class(*table, var.identifier()).unwrap(err);
91 SymbolVariable *v = cls->lookup_variable(*table, mem.identifier());
92 if (v->is_error) {
93 error(mem, Diag::UnknownVariable, string(mem.str()));
94 }
95 return v;
96 }
98 return {table->err_cls, ConstexprError(0), false};
99 }
100
101 if (is_reference) {
103 }
104 auto sym = scope->lookup_function(*table, call.identifier(), call.parameters(), *scope);
105 SymbolFunction *fn = sym.unwrap(err);
106 if (fn->is_constexpr && !fn->is_error) {
107 /* Implement builtin type casts. */
108 auto [param1, param2, param3, param4] = call.parameters().splat_4();
109 int param_count = call.parameters().child_count();
110 if (param_count <= 0 || param_count > 4) {
111 /* Not a constexpr. */
112 }
113 else if (param_count == 4) {
114 ExpressionResult res1 = sub_expression(t, param1, 0);
115 ExpressionResult res2 = sub_expression(t, param2, 0);
116 ExpressionResult res3 = sub_expression(t, param3, 0);
117 ExpressionResult res4 = sub_expression(t, param4, 0);
118
119 if (!res1.is_constexpr() || res1.array_dim != 0 || !res2.is_constexpr() ||
120 res2.array_dim != 0 || !res3.is_constexpr() || res3.array_dim != 0 ||
121 !res4.is_constexpr() || res4.array_dim != 0)
122 {
123 /* Not a constexpr. */
124 }
125 else if (fn->identifier == "float4") {
126 return {table->float4_cls,
128 ConstexprValue::FLOAT4, res1.value, res2.value, res3.value, res4.value)};
129 }
130 else if (fn->identifier == "int4") {
131 return {table->int4_cls,
133 ConstexprValue::INT4, res1.value, res2.value, res3.value, res4.value)};
134 }
135 else if (fn->identifier == "uint4") {
136 return {table->uint4_cls,
138 ConstexprValue::UINT4, res1.value, res2.value, res3.value, res4.value)};
139 }
140 else if (fn->identifier == "bool4") {
141 return {table->bool4_cls,
143 ConstexprValue::BOOL4, res1.value, res2.value, res3.value, res4.value)};
144 }
145 }
146 else if (param_count == 3) {
147 ExpressionResult res1 = sub_expression(t, param1, 0);
148 ExpressionResult res2 = sub_expression(t, param2, 0);
149 ExpressionResult res3 = sub_expression(t, param3, 0);
150
151 if (!res1.is_constexpr() || res1.array_dim != 0 || !res2.is_constexpr() ||
152 res2.array_dim != 0 || !res3.is_constexpr() || res3.array_dim != 0)
153 {
154 /* Not a constexpr. */
155 }
156 else if (fn->identifier == "float3") {
157 return {table->float3_cls,
159 }
160 else if (fn->identifier == "int3") {
161 return {table->int3_cls,
163 }
164 else if (fn->identifier == "uint3") {
165 return {table->uint3_cls,
167 }
168 else if (fn->identifier == "bool3") {
169 return {table->bool3_cls,
171 }
172 }
173 else if (param_count == 2) {
174 ExpressionResult res1 = sub_expression(t, param1, 0);
175 ExpressionResult res2 = sub_expression(t, param2, 0);
176
177 if (!res1.is_constexpr() || res1.array_dim != 0 || !res2.is_constexpr() ||
178 res2.array_dim != 0)
179 {
180 /* Not a constexpr. */
181 }
182 else if (fn->identifier == "float2") {
183 return {table->float2_cls,
185 }
186 else if (fn->identifier == "int2") {
187 return {table->int2_cls,
189 }
190 else if (fn->identifier == "uint2") {
191 return {table->uint2_cls,
193 }
194 else if (fn->identifier == "bool2") {
195 return {table->bool2_cls,
197 }
198 else if (res1.type->comp_len > 1 && res1.type->comp_len == res2.type->comp_len) {
199 if (fn->identifier == "lessThan") {
200 return {table->make_type(table->bool_cls, res1.type->comp_len),
201 res1.value < res2.value};
202 }
203 if (fn->identifier == "lessThanEqual") {
204 return {table->make_type(table->bool_cls, res1.type->comp_len),
205 res1.value <= res2.value};
206 }
207 if (fn->identifier == "greaterThan") {
208 return {table->make_type(table->bool_cls, res1.type->comp_len),
209 res1.value > res2.value};
210 }
211 if (fn->identifier == "greaterThanEqual") {
212 return {table->make_type(table->bool_cls, res1.type->comp_len),
213 res1.value >= res2.value};
214 }
215 if (fn->identifier == "equal") {
216 return {table->make_type(table->bool_cls, res1.type->comp_len),
217 res1.value == res2.value};
218 }
219 if (fn->identifier == "notEqual") {
220 return {table->make_type(table->bool_cls, res1.type->comp_len),
221 res1.value != res2.value};
222 }
223 }
224 }
225 else if (param_count == 1) {
226 ExpressionResult res = sub_expression(t, param1, 0);
227 if (!res.is_constexpr() || res.array_dim != 0) {
228 /* Not a constexpr. */
229 }
230 else if (res.type == table->float4_cls || res.type == table->int4_cls ||
231 res.type == table->bool4_cls || res.type == table->uint4_cls)
232 {
233 if (fn->identifier == "float4") {
234 return {table->float4_cls, ConstexprValue(ConstexprValue::FLOAT4, res.value)};
235 }
236 if (fn->identifier == "int4") {
237 return {table->int4_cls, ConstexprValue(ConstexprValue::INT4, res.value)};
238 }
239 if (fn->identifier == "uint4") {
240 return {table->uint4_cls, ConstexprValue(ConstexprValue::UINT4, res.value)};
241 }
242 if (fn->identifier == "bool4") {
243 return {table->bool4_cls, ConstexprValue(ConstexprValue::BOOL4, res.value)};
244 }
245 }
246 else if (res.type == table->float3_cls || res.type == table->int3_cls ||
247 res.type == table->bool3_cls || res.type == table->uint3_cls)
248 {
249 if (fn->identifier == "float3") {
250 return {table->float3_cls, ConstexprValue(ConstexprValue::FLOAT3, res.value)};
251 }
252 if (fn->identifier == "int3") {
253 return {table->int3_cls, ConstexprValue(ConstexprValue::INT3, res.value)};
254 }
255 if (fn->identifier == "uint3") {
256 return {table->uint3_cls, ConstexprValue(ConstexprValue::UINT3, res.value)};
257 }
258 if (fn->identifier == "bool3") {
259 return {table->bool3_cls, ConstexprValue(ConstexprValue::BOOL3, res.value)};
260 }
261 }
262 else if (res.type == table->float2_cls || res.type == table->int2_cls ||
263 res.type == table->bool2_cls || res.type == table->uint2_cls)
264 {
265 if (fn->identifier == "float2") {
266 return {table->float2_cls, ConstexprValue(ConstexprValue::FLOAT2, res.value)};
267 }
268 if (fn->identifier == "int2") {
269 return {table->int2_cls, ConstexprValue(ConstexprValue::INT2, res.value)};
270 }
271 if (fn->identifier == "uint2") {
272 return {table->uint2_cls, ConstexprValue(ConstexprValue::UINT2, res.value)};
273 }
274 if (fn->identifier == "bool2") {
275 return {table->bool2_cls, ConstexprValue(ConstexprValue::BOOL2, res.value)};
276 }
277 }
278 else if (res.type == table->float_cls || res.type == table->int_cls ||
279 res.type == table->bool_cls || res.type == table->uint_cls)
280 {
281 if (fn->identifier == "float4") {
282 return {table->float4_cls, ConstexprValue(ConstexprValue::FLOAT4, res.value)};
283 }
284 if (fn->identifier == "int4") {
285 return {table->int4_cls, ConstexprValue(ConstexprValue::INT4, res.value)};
286 }
287 if (fn->identifier == "uint4") {
288 return {table->uint4_cls, ConstexprValue(ConstexprValue::UINT4, res.value)};
289 }
290 if (fn->identifier == "bool4") {
291 return {table->bool4_cls, ConstexprValue(ConstexprValue::BOOL4, res.value)};
292 }
293 if (fn->identifier == "float3") {
294 return {table->float3_cls, ConstexprValue(ConstexprValue::FLOAT3, res.value)};
295 }
296 if (fn->identifier == "int3") {
297 return {table->int3_cls, ConstexprValue(ConstexprValue::INT3, res.value)};
298 }
299 if (fn->identifier == "uint3") {
300 return {table->uint3_cls, ConstexprValue(ConstexprValue::UINT3, res.value)};
301 }
302 if (fn->identifier == "bool3") {
303 return {table->bool3_cls, ConstexprValue(ConstexprValue::BOOL3, res.value)};
304 }
305 if (fn->identifier == "float2") {
306 return {table->float2_cls, ConstexprValue(ConstexprValue::FLOAT2, res.value)};
307 }
308 if (fn->identifier == "int2") {
309 return {table->int2_cls, ConstexprValue(ConstexprValue::INT2, res.value)};
310 }
311 if (fn->identifier == "uint2") {
312 return {table->uint2_cls, ConstexprValue(ConstexprValue::UINT2, res.value)};
313 }
314 if (fn->identifier == "bool2") {
315 return {table->bool2_cls, ConstexprValue(ConstexprValue::BOOL2, res.value)};
316 }
317 if (fn->identifier == "float") {
318 return {table->float_cls, ConstexprValue(ConstexprValue::FLOAT1, res.value)};
319 }
320 if (fn->identifier == "int") {
321 return {table->int_cls, ConstexprValue(ConstexprValue::INT1, res.value)};
322 }
323 if (fn->identifier == "uint") {
324 return {table->uint_cls, ConstexprValue(ConstexprValue::UINT1, res.value)};
325 }
326 if (fn->identifier == "bool") {
327 return {table->bool_cls, ConstexprValue(ConstexprValue::BOOL1, res.value)};
328 }
329 if (fn->identifier == "floatBitsToUint") {
330 return {table->uint_cls,
331 ConstexprValue(std::bit_cast<uint32_t>(res.value.comp_cast<float>(0)))};
332 }
333 if (fn->identifier == "floatBitsToInt") {
334 return {table->int_cls,
335 ConstexprValue(std::bit_cast<int32_t>(res.value.comp_cast<float>(0)))};
336 }
337 if (fn->identifier == "uintBitsToFloat") {
338 return {table->float_cls,
339 ConstexprValue(std::bit_cast<float>(res.value.comp_cast<uint32_t>(0)))};
340 }
341 if (fn->identifier == "intBitsToFloat") {
342 return {table->float_cls,
343 ConstexprValue(std::bit_cast<float>(res.value.comp_cast<int32_t>(0)))};
344 }
345 }
346 }
347 }
348 return fn->return_type;
349 }
350 case ast::NodeType::LocalVar: {
351 ast::LocalVar var = t;
352 SymbolVariable *sym = scope->lookup_variable(*table, var.identifier());
353 if (sym) {
354 return sym;
355 }
356 error(t, Diag::UnknownMember, std::string(var.identifier().str()));
357 return table->err_cls;
358 }
359 default:
361 return table->err_cls;
362 }
363 }
364
366 {
367 SymbolClass *type = table->get_literal_type(t.str());
368
369 ConstexprValue value(0);
370 try {
371 std::string str_val(t.str());
372
373 if (type == table->float_cls) {
374 value = ConstexprValue(std::stof(str_val));
375 }
376 else if (type == table->int_cls) {
377 value = ConstexprValue(std::stoi(str_val, nullptr, 0));
378 }
379 else if (type == table->uint_cls) {
380 value = ConstexprValue(uint32_t(std::stoull(str_val, nullptr, 0)));
381 }
382 else if (type == table->bool_cls) {
383 value = ConstexprValue(str_val == "true");
384 }
385 else {
386 throw std::runtime_error("Invalid Type");
387 }
388 }
389 catch (...) {
391 return table->err_cls;
392 }
393 return {type, value, /* is_temporary */ false};
394 }
395
397 {
398 return table->str_cls;
399 }
400
402 {
403 return sub_expression(t, ast::ExprSub(t).expr(), p);
404 }
405
407 {
408 SymbolClass *l = left.type;
409 ast::Node right = consume();
410 switch (right.type()) {
411 case ast::NodeType::FuncCall: {
412 ast::FuncCall call = right;
413 /* The exception is for legacy BSL. */
414 if (is_reference && call.identifier().str() != "resource_table_get" &&
415 call.identifier().str() != "interface_get" &&
416 call.identifier().str() != "sampler_get" && call.identifier().str() != "buffer_get")
417 {
419 }
420 auto sym = l->lookup_function(*table, call.identifier(), call.parameters(), *scope);
421 return sym.unwrap(err)->return_type;
422 }
423 case ast::NodeType::LocalVar: {
424 ast::LocalVar var = right;
425 SymbolVariable *sym = l->lookup_variable(*table, var.identifier());
426 if (sym) {
427 return sym;
428 }
429 error(t, Diag::UnknownMember, std::string(var.identifier().str()));
430 return table->err_cls;
431 }
432 default:
434 this->node = ast::Node{};
435 return table->err_cls;
436 }
437 }
438
440 {
441 if (is_reference) {
443 }
445 return table->err_cls;
446 }
447
449 {
450 /* Subscript operator. */
451 if (ast::Subscript sub = t; sub.is_valid()) {
452 if (left.array_dim > 0) {
454
455 if (!(table->get_conversion_rank(arg.type, table->int_cls) >= MatchRank::Promotion ||
456 table->get_conversion_rank(arg.type, table->uint_cls) >= MatchRank::Promotion))
457 {
459 }
460 if (is_reference && !(arg.flags & ExprFlag::IsConstant)) {
462 }
463
464 left.array_dim -= 1;
465 return left;
466 }
467 /* Call subscript operator */
468 if (left.type->operator_subscript) {
470 if (!(table->get_conversion_rank(arg.type, table->int_cls) >= MatchRank::Promotion ||
471 table->get_conversion_rank(arg.type, table->uint_cls) >= MatchRank::Promotion))
472 {
474 }
475 return left.type->operator_subscript->return_type;
476 }
477 }
479 return table->err_cls;
480 }
481
483 {
484 return sub_expression(sub, sub.expr(), 0);
485 }
486
488 {
489 using namespace builtin;
490 ExpressionResult right = expr(p);
491
492 if (right.array_dim > 0) {
494 return table->err_cls;
495 }
496
497 TokenType op_type = t.front().type();
498
499 ClassId type_id = unary_prefix_operator_return_type(op_type, right.type);
500 if (type_id == builtin::Invalid) {
501 error(t, Diag::InvalidUnaryArgumentType, to_str(op_type), right.type->identifier);
502 return table->err_cls;
503 }
504
505 SymbolClass *type = table->to_class(type_id);
506
507 if (is_reference && (op_type == Increment || op_type == Decrement)) {
509 return table->err_cls;
510 }
511
512#ifdef _MSC_VER
513/* Silence warning about bool operand.
514 * They cannot happen because they are caught by the above check */
515# pragma warning(push)
516# pragma warning(disable : 4805)
517#endif
518
519 ExprFlag flags = ExprFlag(right.flags);
520 if (flags & ExprFlag::IsConstexpr) {
521 switch (op_type) {
522 /* Note that side effects are not propagated. */
523 case Increment:
524 return {type, ++right.value};
525 case Decrement:
526 return {type, --right.value};
527 case Plus:
528 return {type, +right.value};
529 case Minus:
530 return {type, -right.value};
531 case Not:
532 /* Note that '!' token is of MultiTok class and can contain many unary '!'. */
533 return {type, (t.str().size() & 1) ? !right.value : !!right.value};
534 case BitwiseNot:
535 return {type, ~right.value};
536 default:
537 assert(0); /* Unreachable. */
538 return table->err_cls;
539 }
540 }
541#ifdef _MSC_VER
542# pragma warning(pop)
543#endif
544 return {type, flags};
545 }
546
548 {
549 using namespace builtin;
550 if (left.array_dim > 0) {
552 return table->err_cls;
553 }
554
555 TokenType op_type = t.front().type();
556
557 ClassId type_id = unary_suffix_operator_return_type(left.type, op_type);
558 if (type_id == builtin::Invalid) {
559 error(t, Diag::InvalidUnaryArgumentType, to_str(op_type), left.type->identifier);
560 return table->err_cls;
561 }
562
563 SymbolClass *type = table->to_class(type_id);
564
565 if (is_reference && (op_type == Increment || op_type == Decrement)) {
567 return table->err_cls;
568 }
569
570#ifdef _MSC_VER
571/* Silence warning about bool operand.
572 * They cannot happen because they are caught by the above check */
573# pragma warning(push)
574# pragma warning(disable : 4805)
575#endif
576
577 ExprFlag flags = ExprFlag(left.flags);
578 if (flags & ExprFlag::IsConstexpr) {
579 switch (op_type) {
580 /* Note that side effects are not propagated. */
581 case Increment:
582 return {type, left.value++};
583 case Decrement:
584 return {type, left.value--};
585 default:
586 assert(0); /* Unreachable. */
587 return table->err_cls;
588 }
589 }
590
591#ifdef _MSC_VER
592# pragma warning(pop)
593#endif
594 return {type, flags};
595 }
596
598 {
599 using namespace builtin;
600 ExpressionResult rhs = expr(p);
601 return rhs;
602 }
603
605 {
606 using namespace builtin;
607 ExpressionResult rhs = expr(p);
608
609 if (lhs.array_dim > 0 || rhs.array_dim > 0) {
611 return table->err_cls;
612 }
613
614 TokenType op_type = t.front().type();
615
616 ClassId type_id = binary_operator_return_type(lhs.type, op_type, rhs.type);
617 if (type_id == builtin::Invalid) {
618 error(t,
620 lhs.type->identifier,
621 to_str(op_type),
622 rhs.type->identifier);
623 return table->err_cls;
624 }
625
626 SymbolClass *type = table->to_class(type_id);
627 ExprFlag flags = ExprFlag(lhs.flags & rhs.flags);
628
629 /* For debugging. */
630 // std::cout << t.prev().str() << t.str() << t.next().str() << std::endl;
631
632#ifdef _MSC_VER
633/* Silence warning about bool operand.
634 * They cannot happen because they are caught by the above check */
635# pragma warning(push)
636# pragma warning(disable : 4805)
637#endif
638
639 if (flags & ExprFlag::IsConstexpr) {
640 switch (op_type) {
641 case Multiply:
642 return {type, lhs.value * rhs.value};
643 case Divide:
644 return {type, [&]() {
645 if (contains_zero(rhs.value)) {
647 return lhs.value * rhs.value;
648 }
649 return lhs.value / rhs.value;
650 }()};
651 case Modulo:
652 return {type, [&]() {
653 if (contains_zero(rhs.value)) {
655 return decltype(lhs.value % rhs.value)(0);
656 }
657 return lhs.value % rhs.value;
658 }()};
659 case LShift:
660 return {type, [&]() {
661 if (anyLessThan(rhs.value, 0)) {
663 return decltype(lhs.value << rhs.value)(0);
664 }
665 if (anyGreaterThanEqual(rhs.value, 32)) {
667 return decltype(lhs.value << rhs.value)(0);
668 }
669 return lhs.value << rhs.value;
670 }()};
671 case RShift:
672 return {type, [&]() {
673 if (anyLessThan(rhs.value, 0)) {
675 return decltype(lhs.value >> rhs.value)(0);
676 }
677 if (anyGreaterThanEqual(rhs.value, 32)) {
679 return decltype(lhs.value >> rhs.value)(0);
680 }
681 return lhs.value >> rhs.value;
682 }()};
683 case Plus:
684 return {type, lhs.value + rhs.value};
685 case Minus:
686 return {type, lhs.value - rhs.value};
687 case LThan:
688 return {type, lhs.value < rhs.value};
689 case LEqual:
690 return {type, lhs.value <= rhs.value};
691 case GThan:
692 return {type, lhs.value > rhs.value};
693 case GEqual:
694 return {type, lhs.value >= rhs.value};
695 case Equal:
696 return {type, lhs.value == rhs.value};
697 case NotEqual:
698 return {type, lhs.value != rhs.value};
699 case And:
700 return {type, lhs.value & rhs.value};
701 case Xor:
702 return {type, lhs.value ^ rhs.value};
703 case Or:
704 return {type, lhs.value | rhs.value};
705 case LogicalAnd:
706 return {type, lhs.value && rhs.value};
707 case LogicalOr:
708 return {type, lhs.value || rhs.value};
709 default:
710 assert(0); /* Unreachable. */
711 return table->err_cls;
712 }
713 }
714
715#ifdef _MSC_VER
716# pragma warning(pop)
717#endif
718 return {type, flags};
719 }
720
722 const ast::Node &t,
723 BindingPower p_first,
724 BindingPower p_second)
725 {
726 ExpressionResult tval = sub_expression(node, node, p_first);
727 if (consume().front().type() != Colon) {
728 error(ErrorType::ExpectedColon, Colon);
729 return table->err_cls;
730 }
731 ExpressionResult fval = expr(p_second);
732
733 if (left.type != table->bool_cls && left.type != table->bool32_t_cls) {
735 return table->err_cls;
736 }
737 if (tval.type != fval.type) {
739 return table->err_cls;
740 }
741
742 if (left.flags & ExprFlag::IsConstexpr) {
743 return {tval.type, left.value.comp_as<int>(0) ? tval.value : fval.value};
744 }
745 /* Operand types match. We can return either. */
746 return {tval.type, ExprFlag(tval.flags & fval.flags)};
747 }
748
750 {
751 switch (err) {
752 case ErrorType::ExpectedParenthesis:
754 break;
755 case ErrorType::ExpectedColon:
757 break;
758 case ErrorType::InvalidOperator:
759 error(node, Diag::OperatorTokenInvalid, to_str(type));
760 break;
761 case ErrorType::InvalidExpression:
763 break;
764 }
765 return table->err_cls;
766 }
767
769 {
770 return node.is_valid() ? node.front().type() : Invalid;
771 }
772
774 {
775 ast::Node n = node;
776 node = node.next();
777 return n;
778 }
779
781 {
782 ast::NodeType type = t.type();
783 return type != ast::NodeType::Invalid ?
784 (type == ast::NodeType::IdQualified || type == ast::NodeType::FuncCall ?
785 Word :
786 t.front().type()) :
787 Invalid;
788 }
789
790 private:
791 /* Helper to allow evaluating nested AST node inside the expression. */
792 ExpressionResult sub_expression(ast::Node node, ast::Expr expression, BindingPower p)
793 {
794 /* Enter sub-expression */
795 this->node = expression.child_first();
796 /* Parse the whole parenthesis expression. */
797 ExpressionResult v = expr(p);
798 /* Exit sub-expression. */
799 this->node = node.next();
800 return v;
801 }
802
803 void error(ast::Node node,
804 Diag diag,
805 const std::string &msg = "",
806 const std::string &msg2 = "",
807 const std::string &msg3 = "")
808 {
809 /* Only log the first error. */
810 if (!err) {
811 err = {node, diag, msg, msg2, msg3};
812 }
813 }
814
815 /* Pipe error if existing. */
816 void error(std::optional<AstNodeException> &err)
817 {
818 if (err) {
819 error(err->node, err->diag, err->param1, err->param2, err->param3);
820 }
821 }
822};
823
825 ast::Node start_node,
826 bool is_reference) const
827{
828 assert(start_node.is_valid());
829 /* Caller must not pass the Expr node but it's first child. */
830 assert(start_node.type() != ast::NodeType::Expr);
831 return ExpressionTypeParser(this, &scope, start_node, is_reference).eval();
832}
833
834} // namespace bsl
Definition codegen.cc:18
bool anyLessThan(const ConstexprValue &lhs, int rhs)
Definition constexpr.hh:557
bool anyGreaterThanEqual(const ConstexprValue &lhs, int rhs)
Definition constexpr.hh:567
bool contains_zero(const ConstexprValue &v)
Definition constexpr.hh:547
ConstexprValue ConstexprError
@ ConstexprShiftNegative
@ InvalidNumberLiteral
@ ConstexprShiftTooLarge
@ ExpectedOneTypename
@ InvalidBinaryOperands
@ InvalidUnaryArgumentType
@ TypeNotConvertibleToBool
@ OperatorCalledIsNotFunction
@ FunctionCallNotAllowedInReferenceDefinition
@ OperatorInvalidTernary
@ OperatorTokenInvalid
@ InvalidExprTypeChecker
@ ReferenceCannotBindSideEffect
@ ParserTrailingInput
@ ConstexprDivisionByZero
@ ReferenceCannotBindNonConstSubscript
@ ExpectedParenthesis
@ IsConstexpr
@ IsConstant
std::string constexpr_to_string(const ConstexprValue &v)
TokenType
Definition types.hh:41
@ RShift
Definition types.hh:126
@ GThan
Definition types.hh:149
@ LThan
Definition types.hh:150
@ LogicalOr
Definition types.hh:93
@ Not
Definition types.hh:64
@ Modulo
Definition types.hh:152
@ And
Definition types.hh:146
@ LogicalAnd
Definition types.hh:81
@ BitwiseNot
Definition types.hh:151
@ Multiply
Definition types.hh:145
@ LEqual
Definition types.hh:104
@ Plus
Definition types.hh:68
@ Colon
Definition types.hh:65
@ Decrement
Definition types.hh:88
@ Increment
Definition types.hh:112
@ NotEqual
Definition types.hh:89
@ Equal
Definition types.hh:90
@ Divide
Definition types.hh:70
@ Xor
Definition types.hh:148
@ LShift
Definition types.hh:125
@ Minus
Definition types.hh:69
@ Word
Definition types.hh:43
@ Invalid
Definition types.hh:42
@ GEqual
Definition types.hh:94
@ Or
Definition types.hh:147
std::string_view str() const
Definition ast.cc:113
ConstexprValue(int32_t s)
Definition constexpr.hh:139
T comp_cast(int i) const
Definition constexpr.hh:272
T comp_as(int i) const
Definition constexpr.hh:262
bool is_constexpr() const
ConstexprValue value
ExpressionResult suffix(ExpressionResult left, const ast::Node &t)
ExpressionResult identifier(const ast::Node &t)
ExpressionResult string_literal(const ast::Node &)
ExpressionResult prefix(const ast::Node &t, BindingPower p)
ExpressionResult comma(ExpressionResult, const ast::Node &, BindingPower p)
Base::ErrorType ErrorType
ExpressionResult ternary(ExpressionResult left, const ast::Node &t, BindingPower p_first, BindingPower p_second)
ExpressionResult subscript(ExpressionResult left, const ast::Node &t)
PrattParser< ExpressionTypeParser, ExpressionResult, ast::Node > Base
ExpressionResult member(ExpressionResult left, const ast::Node &t)
ExpressionResult error(ErrorType err, lexit::TokenType type)
ExpressionResult function_call(ExpressionResult left, const ast::Node &t)
ExpressionResult binary(ExpressionResult lhs, const ast::Node &t, BindingPower p)
ExpressionResult parenthesis(const ast::Node &t, BindingPower p)
ExpressionTypeParser(const SymbolTable *table, const SymbolScope *scope, ast::Node expr_first_child, bool is_reference=false)
ExpressionResult subscript_parameter(ast::Subscript &sub)
Base::BindingPower BindingPower
ExpressionResult number_literal(const ast::Node &t)
Result< ExpressionResult > eval()
TokenType to_type(const ast::Node &t) const
SymbolFunction * operator_subscript
SymbolClass * return_type
SymbolVariable * lookup_variable(const SymbolTable &table, ast::IdQualified id) const
Result< SymbolClass * > lookup_class(const SymbolTable &table, ast::IdQualified id, optional< SourceLocation > loc=nullopt) const
Result< ExpressionResult > expr_type_analysis(const SymbolScope &scope, ast::Node start_node, bool is_reference=false) const
const TokenType & type() const
Definition lexit.hh:354