-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpRuleManager.cpp
733 lines (583 loc) · 32.5 KB
/
OpRuleManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Copyright (c) 2021-2024 ChilliBits. All rights reserved.
#include "OpRuleManager.h"
#include <SourceFile.h>
#include <ast/ASTNodes.h>
#include <global/RuntimeModuleManager.h>
#include <typechecker/TypeChecker.h>
namespace spice::compiler {
OpRuleManager::OpRuleManager(TypeChecker *typeChecker)
: typeChecker(typeChecker), resourceManager(typeChecker->resourceManager) {}
SymbolType OpRuleManager::getAssignResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, bool isDecl,
const char *errMsgPrefix) {
// Check if we try to assign a constant value
if (!isDecl)
ensureNoConstAssign(node, lhs.type);
// Retrieve types
SymbolType lhsType = lhs.type;
SymbolType rhsType = rhs.type;
// Skip type compatibility check if the lhs is of type dyn -> perform type inference
if (lhsType.is(TY_DYN))
return rhsType;
// Allow pointers and arrays of the same type straight away
if (lhsType.isOneOf({TY_PTR, TY_REF}) && lhsType.matches(rhsType, false, false, true))
return rhsType;
// Allow ref type to type of the same contained type straight away
if (rhsType.isRef()) {
// If this is const ref, remove both: the reference and the constness
SymbolType rhsModified = rhsType.getContainedTy();
rhsModified.specifiers.isConst = false;
if (lhsType.matches(rhsModified, false, !lhsType.isRef(), true))
return lhsType;
}
// Allow arrays, structs, interfaces, functions, procedures of the same type straight away
if (lhsType.isOneOf({TY_ARRAY, TY_STRUCT, TY_INTERFACE, TY_FUNCTION, TY_PROCEDURE}) &&
lhsType.matches(rhsType, false, true, true))
return rhsType;
// Check common type combinations
SymbolType resultType = getAssignResultTypeCommon(node, lhs, rhs, isDecl);
if (!resultType.is(TY_INVALID))
return resultType;
// Check primitive type combinations
return validateBinaryOperation(node, ASSIGN_OP_RULES, ARRAY_LENGTH(ASSIGN_OP_RULES), "=", lhsType, rhsType, true, errMsgPrefix);
}
SymbolType OpRuleManager::getFieldAssignResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, bool imm,
bool isDecl) {
// Check if we try to assign a constant value
if (!isDecl)
ensureNoConstAssign(node, lhs.type);
// Retrieve types
SymbolType lhsType = lhs.type;
SymbolType rhsType = rhs.type;
assert(!lhsType.is(TY_DYN));
// Allow pointers and arrays of the same type straight away
if (lhsType.isOneOf({TY_PTR, TY_ARRAY, TY_STRUCT}) && lhsType == rhsType)
return rhsType;
// Allow struct of the same type straight away
if (lhsType.is(TY_STRUCT) && lhsType.matches(rhsType, false, true, true))
return rhsType;
// Allow ref type to type of the same contained type straight away
if (rhsType.isRef() && lhsType.matches(rhsType.getContainedTy(), false, false, true))
return lhsType;
// Allow const ref type to type of the same contained type straight away
if (rhsType.isConstRef() && lhsType.matches(rhsType.getContainedTy().getNonConst(), false, false, true))
return lhsType;
// Allow immediate value to const ref of the same contained type straight away
if (lhsType.isRef() && lhsType.getContainedTy().isConst() && imm)
return rhsType;
// Check common type combinations
SymbolType resultType = getAssignResultTypeCommon(node, lhs, rhs, isDecl);
if (!resultType.is(TY_INVALID))
return resultType;
// Check primitive type combinations
return validateBinaryOperation(node, ASSIGN_OP_RULES, ARRAY_LENGTH(ASSIGN_OP_RULES), "=", lhsType, rhsType, true,
ERROR_FIELD_ASSIGN);
}
SymbolType OpRuleManager::getAssignResultTypeCommon(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs,
bool isDecl) {
// Retrieve types
SymbolType lhsType = lhs.type;
SymbolType rhsType = rhs.type;
// Allow type to ref type of the same contained type straight away
if (lhsType.isRef() && lhsType.getContainedTy().matches(rhsType, false, false, true)) {
if (isDecl && !lhsType.canBind(rhsType, rhs.isTemporary()))
throw SemanticError(node, TEMP_TO_NON_CONST_REF, "Temporary values can only be bound to const reference variables/fields");
return lhsType;
}
// Allow dyn[] (empty array literal) to any array
if (lhsType.isArray() && rhsType.isArrayOf(TY_DYN))
return lhsType;
// Allow char* = string
if (lhsType.isPtrOf(TY_CHAR) && rhsType.is(TY_STRING) && lhsType.specifiers == rhsType.specifiers)
return lhsType;
// Allow array to pointer
if (lhsType.isPtr() && rhsType.isArray() && lhsType.getContainedTy().matches(rhsType.getContainedTy(), false, false, true))
return lhsType;
// Allow interface* = struct* or interface& = struct that implements this interface
const bool sameChainDepth = lhsType.typeChain.size() == rhsType.typeChain.size();
const bool typesCompatible = (lhsType.isPtr() && rhsType.isPtr() && sameChainDepth) || lhsType.isRef();
if (typesCompatible && lhsType.isBaseType(TY_INTERFACE) && rhsType.isBaseType(TY_STRUCT)) {
SymbolType lhsTypeCopy = lhsType;
SymbolType rhsTypeCopy = rhsType;
SymbolType::unwrapBoth(lhsTypeCopy, rhsTypeCopy);
if (lhsTypeCopy.matchesInterfaceImplementedByStruct(rhsTypeCopy))
return lhsType;
}
// Nothing matched
return SymbolType(TY_INVALID);
}
ExprResult OpRuleManager::getPlusEqualResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_PLUS_EQUAL, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
// Check if this is an unsafe operation
if (lhsType.isPtr() && rhsType.isOneOf({TY_INT, TY_LONG, TY_SHORT})) {
ensureUnsafeAllowed(node, "+=", lhsType, rhsType);
return {lhs};
}
return {validateBinaryOperation(node, PLUS_EQUAL_OP_RULES, ARRAY_LENGTH(PLUS_EQUAL_OP_RULES), "+=", lhsType, rhsType)};
}
ExprResult OpRuleManager::getMinusEqualResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_MINUS_EQUAL, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
// Check if this is an unsafe operation
if (lhsType.isPtr() && rhsType.isOneOf({TY_INT, TY_LONG, TY_SHORT})) {
ensureUnsafeAllowed(node, "-=", lhsType, rhsType);
return {lhs};
}
return {validateBinaryOperation(node, MINUS_EQUAL_OP_RULES, ARRAY_LENGTH(MINUS_EQUAL_OP_RULES), "-=", lhsType, rhsType)};
}
ExprResult OpRuleManager::getMulEqualResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_MUL_EQUAL, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return {validateBinaryOperation(node, MUL_EQUAL_OP_RULES, ARRAY_LENGTH(MUL_EQUAL_OP_RULES), "*=", lhsType, rhsType)};
}
ExprResult OpRuleManager::getDivEqualResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_DIV_EQUAL, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return {validateBinaryOperation(node, DIV_EQUAL_OP_RULES, ARRAY_LENGTH(DIV_EQUAL_OP_RULES), "/=", lhsType, rhsType)};
}
SymbolType OpRuleManager::getRemEqualResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, REM_EQUAL_OP_RULES, ARRAY_LENGTH(REM_EQUAL_OP_RULES), "%=", lhsType, rhsType);
}
SymbolType OpRuleManager::getSHLEqualResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, SHL_EQUAL_OP_RULES, ARRAY_LENGTH(SHL_EQUAL_OP_RULES), "<<=", lhsType, rhsType);
}
SymbolType OpRuleManager::getSHREqualResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, SHR_EQUAL_OP_RULES, ARRAY_LENGTH(SHR_EQUAL_OP_RULES), ">>=", lhsType, rhsType);
}
SymbolType OpRuleManager::getAndEqualResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, AND_EQUAL_OP_RULES, ARRAY_LENGTH(AND_EQUAL_OP_RULES), "&=", lhsType, rhsType);
}
SymbolType OpRuleManager::getOrEqualResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, OR_EQUAL_OP_RULES, ARRAY_LENGTH(OR_EQUAL_OP_RULES), "|=", lhsType, rhsType);
}
SymbolType OpRuleManager::getXorEqualResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, XOR_EQUAL_OP_RULES, ARRAY_LENGTH(XOR_EQUAL_OP_RULES), "^=", lhsType, rhsType);
}
SymbolType OpRuleManager::getLogicalOrResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, LOGICAL_OR_OP_RULES, ARRAY_LENGTH(LOGICAL_OR_OP_RULES), "||", lhsType, rhsType);
}
SymbolType OpRuleManager::getLogicalAndResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, LOGICAL_AND_OP_RULES, ARRAY_LENGTH(LOGICAL_AND_OP_RULES), "&&", lhsType, rhsType);
}
SymbolType OpRuleManager::getBitwiseOrResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, BITWISE_OR_OP_RULES, ARRAY_LENGTH(BITWISE_OR_OP_RULES), "|", lhsType, rhsType);
}
SymbolType OpRuleManager::getBitwiseXorResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, BITWISE_XOR_OP_RULES, ARRAY_LENGTH(BITWISE_XOR_OP_RULES), "^", lhsType, rhsType);
}
SymbolType OpRuleManager::getBitwiseAndResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, BITWISE_AND_OP_RULES, ARRAY_LENGTH(BITWISE_AND_OP_RULES), "&", lhsType, rhsType);
}
ExprResult OpRuleManager::getEqualResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_EQUAL, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
// Allow 'pointer == pointer' straight away
if (lhsType.isPtr() && rhsType.isPtr())
return ExprResult(SymbolType(TY_BOOL));
// Allow 'pointer == int' straight away
if (lhsType.isPtr() && rhsType.is(TY_INT))
return ExprResult(SymbolType(TY_BOOL));
// Allow 'string == char*' and vice versa straight away
if ((lhsType.is(TY_STRING) && rhsType.isPtrOf(TY_CHAR)) || (lhsType.isPtrOf(TY_CHAR) && rhsType.is(TY_STRING)))
return ExprResult(SymbolType(TY_BOOL));
// Check primitive type combinations
return ExprResult(validateBinaryOperation(node, EQUAL_OP_RULES, ARRAY_LENGTH(EQUAL_OP_RULES), "==", lhsType, rhsType));
}
ExprResult OpRuleManager::getNotEqualResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_NOT_EQUAL, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
// Allow 'pointer != pointer' straight away
if (lhsType.isPtr() && rhsType.isPtr())
return ExprResult(SymbolType(TY_BOOL));
// Allow 'pointer != int' straight away
if (lhsType.isPtr() && rhsType.is(TY_INT))
return ExprResult(SymbolType(TY_BOOL));
// Allow 'string != char*' and vice versa straight away
if ((lhsType.is(TY_STRING) && rhsType.isPtrOf(TY_CHAR)) || (lhsType.isPtrOf(TY_CHAR) && rhsType.is(TY_STRING)))
return ExprResult(SymbolType(TY_BOOL));
// Check primitive type combinations
return ExprResult(validateBinaryOperation(node, NOT_EQUAL_OP_RULES, ARRAY_LENGTH(NOT_EQUAL_OP_RULES), "!=", lhsType, rhsType));
}
SymbolType OpRuleManager::getLessResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, LESS_OP_RULES, ARRAY_LENGTH(LESS_OP_RULES), "<", lhsType, rhsType);
}
SymbolType OpRuleManager::getGreaterResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, GREATER_OP_RULES, ARRAY_LENGTH(GREATER_OP_RULES), ">", lhsType, rhsType);
}
SymbolType OpRuleManager::getLessEqualResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, LESS_EQUAL_OP_RULES, ARRAY_LENGTH(LESS_EQUAL_OP_RULES), "<=", lhsType, rhsType);
}
SymbolType OpRuleManager::getGreaterEqualResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return validateBinaryOperation(node, GREATER_EQUAL_OP_RULES, ARRAY_LENGTH(GREATER_EQUAL_OP_RULES), ">=", lhsType, rhsType);
}
ExprResult OpRuleManager::getShiftLeftResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_SHL, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return {validateBinaryOperation(node, SHIFT_LEFT_OP_RULES, ARRAY_LENGTH(SHIFT_LEFT_OP_RULES), "<<", lhsType, rhsType)};
}
ExprResult OpRuleManager::getShiftRightResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_SHR, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return {validateBinaryOperation(node, SHIFT_RIGHT_OP_RULES, ARRAY_LENGTH(SHIFT_RIGHT_OP_RULES), ">>", lhsType, rhsType)};
}
ExprResult OpRuleManager::getPlusResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult result = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_PLUS, {lhs, rhs}, opIdx);
if (!result.type.is(TY_INVALID))
return result;
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
// Allow any* + <int/long/short>
if (lhsType.isPtr() && rhsType.isOneOf({TY_INT, TY_LONG, TY_SHORT})) {
ensureUnsafeAllowed(node, "+", lhsType, rhsType);
return {lhsType};
}
// Allow <int/long/short> + any*
if (lhsType.isOneOf({TY_INT, TY_LONG, TY_SHORT}) && rhsType.isPtr()) {
ensureUnsafeAllowed(node, "+", lhsType, rhsType);
return {rhsType};
}
return {validateBinaryOperation(node, PLUS_OP_RULES, ARRAY_LENGTH(PLUS_OP_RULES), "+", lhsType, rhsType)};
}
ExprResult OpRuleManager::getMinusResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_MINUS, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
// Allow any* - <int/long/short>
if (lhsType.isPtr() && rhsType.isOneOf({TY_INT, TY_LONG, TY_SHORT})) {
ensureUnsafeAllowed(node, "-", lhsType, rhsType);
return {lhs};
}
// Allow <int/long/short> - any*
if (lhsType.isOneOf({TY_INT, TY_LONG, TY_SHORT}) && rhsType.isPtr()) {
ensureUnsafeAllowed(node, "-", lhsType, rhsType);
return {rhs};
}
return {validateBinaryOperation(node, MINUS_OP_RULES, ARRAY_LENGTH(MINUS_OP_RULES), "-", lhsType, rhsType)};
}
ExprResult OpRuleManager::getMulResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_MUL, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return {validateBinaryOperation(node, MUL_OP_RULES, ARRAY_LENGTH(MUL_OP_RULES), "*", lhsType, rhsType)};
}
ExprResult OpRuleManager::getDivResultType(ASTNode *node, const ExprResult &lhs, const ExprResult &rhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<2>(node, OP_FCT_DIV, {lhs, rhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return {validateBinaryOperation(node, DIV_OP_RULES, ARRAY_LENGTH(DIV_OP_RULES), "/", lhsType, rhsType)};
}
ExprResult OpRuleManager::getRemResultType(const ASTNode *node, const ExprResult &lhs, const ExprResult &rhs) {
// Remove reference wrappers
const SymbolType lhsType = lhs.type.removeReferenceWrapper();
const SymbolType rhsType = rhs.type.removeReferenceWrapper();
return {validateBinaryOperation(node, REM_OP_RULES, ARRAY_LENGTH(REM_OP_RULES), "%", lhsType, rhsType)};
}
SymbolType OpRuleManager::getPrefixMinusResultType(const ASTNode *node, const ExprResult &lhs) {
// Remove reference wrappers
SymbolType lhsType = lhs.type.removeReferenceWrapper();
return validateUnaryOperation(node, PREFIX_MINUS_OP_RULES, ARRAY_LENGTH(PREFIX_MINUS_OP_RULES), "-", lhsType);
}
SymbolType OpRuleManager::getPrefixPlusPlusResultType(const ASTNode *node, const ExprResult &lhs) {
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
SymbolType lhsType = lhs.type.removeReferenceWrapper();
return validateUnaryOperation(node, PREFIX_PLUS_PLUS_OP_RULES, ARRAY_LENGTH(PREFIX_PLUS_PLUS_OP_RULES), "++", lhsType);
}
SymbolType OpRuleManager::getPrefixMinusMinusResultType(const ASTNode *node, const ExprResult &lhs) {
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
SymbolType lhsType = lhs.type.removeReferenceWrapper();
return validateUnaryOperation(node, PREFIX_MINUS_MINUS_OP_RULES, ARRAY_LENGTH(PREFIX_MINUS_MINUS_OP_RULES), "--", lhsType);
}
SymbolType OpRuleManager::getPrefixNotResultType(const ASTNode *node, const ExprResult &lhs) {
// Remove reference wrappers
SymbolType lhsType = lhs.type.removeReferenceWrapper();
return validateUnaryOperation(node, PREFIX_NOT_OP_RULES, ARRAY_LENGTH(PREFIX_NOT_OP_RULES), "!", lhsType);
}
SymbolType OpRuleManager::getPrefixBitwiseNotResultType(const ASTNode *node, const ExprResult &lhs) {
// Remove reference wrappers
SymbolType lhsType = lhs.type.removeReferenceWrapper();
return validateUnaryOperation(node, PREFIX_BITWISE_NOT_OP_RULES, ARRAY_LENGTH(PREFIX_BITWISE_NOT_OP_RULES), "~", lhsType);
}
SymbolType OpRuleManager::getPrefixMulResultType(const ASTNode *node, const ExprResult &lhs) {
// Remove reference wrappers
SymbolType lhsType = lhs.type.removeReferenceWrapper();
if (!lhsType.isPtr())
throw SemanticError(node, OPERATOR_WRONG_DATA_TYPE, "Cannot apply de-referencing operator on type " + lhsType.getName(true));
return lhsType.getContainedTy();
}
SymbolType OpRuleManager::getPrefixBitwiseAndResultType(const ASTNode *node, const ExprResult &lhs) {
// Remove reference wrappers
SymbolType lhsType = lhs.type.removeReferenceWrapper();
return lhsType.toPointer(node);
}
ExprResult OpRuleManager::getPostfixPlusPlusResultType(ASTNode *node, const ExprResult &lhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<1>(node, OP_FCT_POSTFIX_PLUS_PLUS, {lhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
SymbolType lhsType = lhs.type.removeReferenceWrapper();
return ExprResult(
validateUnaryOperation(node, POSTFIX_PLUS_PLUS_OP_RULES, ARRAY_LENGTH(POSTFIX_PLUS_PLUS_OP_RULES), "++", lhsType));
}
ExprResult OpRuleManager::getPostfixMinusMinusResultType(ASTNode *node, const ExprResult &lhs, size_t opIdx) {
// Check is there is an overloaded operator function available
ExprResult resultType = isOperatorOverloadingFctAvailable<1>(node, OP_FCT_POSTFIX_MINUS_MINUS, {lhs}, opIdx);
if (!resultType.type.is(TY_INVALID))
return resultType;
// Check if we try to assign a constant value
ensureNoConstAssign(node, lhs.type);
// Remove reference wrappers
SymbolType lhsType = lhs.type.removeReferenceWrapper();
return {validateUnaryOperation(node, POSTFIX_MINUS_MINUS_OP_RULES, ARRAY_LENGTH(POSTFIX_MINUS_MINUS_OP_RULES), "--", lhsType)};
}
SymbolType OpRuleManager::getCastResultType(const ASTNode *node, SymbolType lhsType, const ExprResult &rhs) {
// Remove reference wrappers
lhsType = lhsType.removeReferenceWrapper();
SymbolType rhsType = rhs.type.removeReferenceWrapper();
// Only allow to cast the 'heap' specifier away, if we are in unsafe mode
if (lhsType.specifiers.isHeap != rhsType.specifiers.isHeap)
ensureUnsafeAllowed(node, "(cast)", lhsType, rhsType);
// Allow casts string -> char* and string -> char[]
if (lhsType.isOneOf({TY_PTR, TY_ARRAY}) && lhsType.getContainedTy().is(TY_CHAR) && rhsType.is(TY_STRING))
return lhsType;
// Allow casts char* -> string and char[] -> string
if (lhsType.is(TY_STRING) && rhsType.isOneOf({TY_PTR, TY_ARRAY}) && rhsType.getContainedTy().is(TY_CHAR))
return lhsType;
// Allow casts any* -> any*
if (lhsType.isOneOf({TY_PTR, TY_STRING}) && rhsType.isOneOf({TY_PTR, TY_STRING})) {
if (lhsType != rhsType)
ensureUnsafeAllowed(node, "(cast)", lhsType, rhsType);
return lhsType;
}
// Check primitive type combinations
return validateBinaryOperation(node, CAST_OP_RULES, ARRAY_LENGTH(CAST_OP_RULES), "(cast)", lhsType, rhsType, true);
}
template <size_t N>
ExprResult OpRuleManager::isOperatorOverloadingFctAvailable(ASTNode *node, const char *const fctName,
const std::array<ExprResult, N> &op, size_t opIdx) {
Scope *calleeParentScope = nullptr;
Function *callee = nullptr;
for (const auto &[_, sourceFile] : typeChecker->resourceManager.sourceFiles) {
// Check if there is a registered operator function
if (!sourceFile->getNameRegistryEntry(fctName))
continue;
// Match callees in the global scope of this source file
calleeParentScope = sourceFile->globalScope.get();
const SymbolType thisType(TY_DYN);
ArgList args(N);
args[0] = {typeChecker->mapLocalTypeToImportedScopeType(calleeParentScope, op[0].type), op[0].isTemporary()};
if constexpr (N == 2)
args[1] = {typeChecker->mapLocalTypeToImportedScopeType(calleeParentScope, op[1].type), op[1].isTemporary()};
callee = FunctionManager::matchFunction(calleeParentScope, fctName, thisType, args, {}, false, node);
if (callee)
break;
}
// Return invalid type if the callee was not found
if (!callee)
return ExprResult(SymbolType(TY_INVALID));
assert(calleeParentScope != nullptr);
// Save the pointer to the operator function in the AST node
std::vector<const Function *> &opFctPointers = typeChecker->getOpFctPointers(node);
if (opFctPointers.size() <= opIdx)
opFctPointers.resize(opIdx + 1, nullptr);
opFctPointers.at(opIdx) = callee;
// Check if we need to request a re-visit, because the function body was not type-checked yet
const bool isCallToImportedSourceFile = callee->entry->scope->isImportedBy(typeChecker->rootScope);
if (!callee->alreadyTypeChecked && !isCallToImportedSourceFile)
typeChecker->reVisitRequested = true;
// Check if the called function has sufficient visibility
const bool isImported = calleeParentScope->isImportedBy(typeChecker->rootScope);
SymbolTableEntry *calleeEntry = callee->entry;
if (isImported && !calleeEntry->getType().isPublic())
throw SemanticError(node, INSUFFICIENT_VISIBILITY,
"Overloaded operator '" + callee->getSignature() + "' has insufficient visibility");
// Procedures always have the return type 'bool'
if (callee->isProcedure())
return ExprResult(SymbolType(TY_BOOL));
// Add anonymous symbol to keep track of deallocation
SymbolTableEntry *anonymousSymbol = nullptr;
if (callee->returnType.is(TY_STRUCT))
anonymousSymbol = typeChecker->currentScope->symbolTable.insertAnonymous(callee->returnType, node, opIdx);
return {typeChecker->mapImportedScopeTypeToLocalType(calleeParentScope, callee->returnType), anonymousSymbol};
}
SymbolType OpRuleManager::validateUnaryOperation(const ASTNode *node, const UnaryOpRule opRules[], size_t opRulesSize,
const char *name, const SymbolType &lhs) {
for (size_t i = 0; i < opRulesSize; i++) {
const UnaryOpRule &rule = opRules[i];
if (std::get<0>(rule) == lhs.getSuperType())
return SymbolType(SymbolSuperType(std::get<1>(rule)));
}
throw getExceptionUnary(node, name, lhs);
}
SymbolType OpRuleManager::validateBinaryOperation(const ASTNode *node, const BinaryOpRule opRules[], size_t opRulesSize,
const char *name, const SymbolType &lhs, const SymbolType &rhs,
bool preserveSpecifiersFromLhs, const char *customMessagePrefix) {
for (size_t i = 0; i < opRulesSize; i++) {
const BinaryOpRule &rule = opRules[i];
if (std::get<0>(rule) == lhs.getSuperType() && std::get<1>(rule) == rhs.getSuperType()) {
SymbolType resultType = SymbolType(SymbolSuperType(std::get<2>(rule)));
if (preserveSpecifiersFromLhs)
resultType.specifiers = lhs.specifiers;
return resultType;
}
}
throw getExceptionBinary(node, name, lhs, rhs, customMessagePrefix);
}
SemanticError OpRuleManager::getExceptionUnary(const ASTNode *node, const char *name, const SymbolType &lhs) {
return {node, OPERATOR_WRONG_DATA_TYPE, "Cannot apply '" + std::string(name) + "' operator on type " + lhs.getName(true)};
}
SemanticError OpRuleManager::getExceptionBinary(const ASTNode *node, const char *name, const SymbolType &lhs,
const SymbolType &rhs, const char *messagePrefix) {
// Build error message
std::stringstream errorMsg;
if (strlen(messagePrefix) != 0)
errorMsg << messagePrefix << ". Expected " << lhs.getName(true) << " but got " << rhs.getName(true);
else
errorMsg << "Cannot apply '" << name << "' operator on types " << lhs.getName(true) << " and " << rhs.getName(true);
// Return the exception
return {node, OPERATOR_WRONG_DATA_TYPE, errorMsg.str()};
}
void OpRuleManager::ensureUnsafeAllowed(const ASTNode *node, const char *name, const SymbolType &lhs,
const SymbolType &rhs) const {
if (typeChecker->currentScope->doesAllowUnsafeOperations())
return;
// Print error message
const std::string lhsName = lhs.getName(true);
const std::string rhsName = rhs.getName(true);
const std::string errorMsg = "Cannot apply '" + std::string(name) + "' operator on types " + lhsName + " and " + rhsName +
" as this is an unsafe operation. Please use unsafe blocks if you know what you are doing.";
SOFT_ERROR_VOID(node, UNSAFE_OPERATION_IN_SAFE_CONTEXT, errorMsg)
}
void OpRuleManager::ensureNoConstAssign(const ASTNode *node, const SymbolType &lhs) {
// Check if we try to assign a constant value
if (lhs.removeReferenceWrapper().isConst()) {
const std::string errorMessage = "Trying to assign value to an immutable variable of type " + lhs.getName(true);
throw SemanticError(node, REASSIGN_CONST_VARIABLE, errorMessage);
}
}
} // namespace spice::compiler