-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SymbolType.cpp
665 lines (576 loc) · 22.8 KB
/
SymbolType.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
// Copyright (c) 2021-2024 ChilliBits. All rights reserved.
#include "SymbolType.h"
#include <SourceFile.h>
#include <ast/Attributes.h>
#include <exception/CompilerError.h>
#include <exception/SemanticError.h>
#include <irgenerator/NameMangling.h>
#include <irgenerator/StdFunctionManager.h>
#include <model/GenericType.h>
#include <model/Struct.h>
#include <symboltablebuilder/Scope.h>
#include <symboltablebuilder/SymbolTableEntry.h>
namespace spice::compiler {
SymbolType::SymbolType(SymbolSuperType superType)
: typeChain({TypeChainElement{superType}}), specifiers(TypeSpecifiers::of(superType)) {}
SymbolType::SymbolType(SymbolSuperType superType, const std::string &subType)
: typeChain({TypeChainElement{superType, subType}}), specifiers(TypeSpecifiers::of(superType)) {}
SymbolType::SymbolType(SymbolSuperType superType, const std::string &subType, uint64_t typeId,
const SymbolType::TypeChainElementData &data, const std::vector<SymbolType> &templateTypes)
: typeChain({TypeChainElement(superType, subType, typeId, data, templateTypes)}), specifiers(TypeSpecifiers::of(superType)) {}
SymbolType::SymbolType(const TypeChain &types) : typeChain(types), specifiers(TypeSpecifiers::of(types.front().superType)) {}
SymbolType::SymbolType(TypeChain types, TypeSpecifiers specifiers) : typeChain(std::move(types)), specifiers(specifiers) {}
/**
* Get the pointer type of the current type as a new type
*
* @param node AST node for error messages
* @return Pointer type of the current type
*/
SymbolType SymbolType::toPointer(const ASTNode *node) const {
// Do not allow pointers of dyn
if (is(TY_DYN))
throw SemanticError(node, DYN_POINTERS_NOT_ALLOWED, "Just use the dyn type without '*' instead");
if (isRef())
throw SemanticError(node, REF_POINTERS_ARE_NOT_ALLOWED, "Pointers to references are not allowed. Use pointer instead");
TypeChain newTypeChain = typeChain;
newTypeChain.emplace_back(TY_PTR);
return {newTypeChain, specifiers};
}
/**
* Get the reference type of the current type as a new type
*
* @param node AST node for error messages
* @return Reference type of the current type
*/
SymbolType SymbolType::toReference(const ASTNode *node) const {
// Do not allow references of dyn
if (is(TY_DYN))
throw SemanticError(node, DYN_REFERENCES_NOT_ALLOWED, "Just use the dyn type without '&' instead");
// Do not allow references of references
if (isRef())
throw SemanticError(node, MULTI_REF_NOT_ALLOWED, "References to references are not allowed");
TypeChain newTypeChain = typeChain;
newTypeChain.emplace_back(TY_REF);
return {newTypeChain, specifiers};
}
/**
* Get the const reference type of the current type as a new type
*
* @param node AST node for error messages
* @return Const reference type of the current type
*/
SymbolType SymbolType::toConstReference(const ASTNode *node) const {
SymbolType constRefType = toReference(node);
constRefType.specifiers.isConst = true;
return constRefType;
}
/**
* Get the array type of the current type as a new type
*
* @param node AST node for error messages
* @param size Size of the array
* @return Array type of the current type
*/
SymbolType SymbolType::toArray(const ASTNode *node, unsigned int size, bool skipDynCheck /*=false*/) const {
// Do not allow arrays of dyn
if (!skipDynCheck && typeChain.back().superType == TY_DYN)
throw SemanticError(node, DYN_ARRAYS_NOT_ALLOWED, "Just use the dyn type without '[]' instead");
TypeChain newTypeChain = typeChain;
newTypeChain.emplace_back(TY_ARRAY, TypeChainElementData{.arraySize = size});
return {newTypeChain, specifiers};
}
/**
* Retrieve the base type of an array or a pointer
*
* @return Base type
*/
SymbolType SymbolType::getContainedTy() const {
if (is(TY_STRING))
return SymbolType(TY_CHAR);
assert(typeChain.size() > 1);
TypeChain newTypeChain = typeChain;
newTypeChain.pop_back();
return {newTypeChain, specifiers};
}
/**
* Replace the base type with another one
*
* @param newBaseType New base type
* @return The new type
*/
SymbolType SymbolType::replaceBaseType(const SymbolType &newBaseType) const {
assert(!typeChain.empty());
// Create new type chain
TypeChain newTypeChain = newBaseType.typeChain;
const bool doubleRef = newTypeChain.back().superType == TY_REF && typeChain.back().superType == TY_REF;
for (size_t i = 1; i < typeChain.size(); i++)
if (!doubleRef || i > 1)
newTypeChain.push_back(typeChain.at(i));
// Create new specifiers
TypeSpecifiers newSpecifiers = specifiers.merge(newBaseType.specifiers);
// Return the new chain as a symbol type
return {newTypeChain, newSpecifiers};
}
/**
* Return the LLVM type for this symbol type
*
* @param context LLVM context
* @param accessScope Access scope for structs
* @return Corresponding LLVM type
*/
llvm::Type *SymbolType::toLLVMType(llvm::LLVMContext &context, Scope *accessScope) const { // NOLINT(misc-no-recursion)
assert(!typeChain.empty() && !isOneOf({TY_DYN, TY_INVALID}));
if (is(TY_DOUBLE))
return llvm::Type::getDoubleTy(context);
if (is(TY_INT))
return llvm::Type::getInt32Ty(context);
if (is(TY_SHORT))
return llvm::Type::getInt16Ty(context);
if (is(TY_LONG))
return llvm::Type::getInt64Ty(context);
if (isOneOf({TY_CHAR, TY_BYTE}))
return llvm::Type::getInt8Ty(context);
if (is(TY_STRING))
return llvm::PointerType::get(context, 0);
if (is(TY_BOOL))
return llvm::Type::getInt1Ty(context);
if (isOneOf({TY_STRUCT, TY_INTERFACE})) {
Scope *structBodyScope = getBodyScope();
const std::string structSignature = Struct::getSignature(getSubType(), getTemplateTypes());
SymbolTableEntry *structSymbol = structBodyScope->parent->lookupStrict(structSignature);
assert(structSymbol != nullptr);
llvm::StructType *structType = structSymbol->getStructLLVMType();
// If the type is not known yet, build the LLVM type
if (!structType) {
// Collect concrete field types
std::vector<llvm::Type *> fieldTypes;
bool isPacked = false;
if (is(TY_STRUCT)) { // Struct
Struct *spiceStruct = structSymbol->getType().getStruct(structSymbol->declNode);
assert(spiceStruct != nullptr);
const std::string mangledName = NameMangling::mangleStruct(*spiceStruct);
structType = llvm::StructType::create(context, mangledName);
structSymbol->setStructLLVMType(structType);
const size_t totalFieldCount = spiceStruct->scope->getFieldCount();
fieldTypes.reserve(totalFieldCount);
// If the struct has no interface types, but a vtable was requested, add another ptr field type
assert(structSymbol->declNode->isStructDef());
auto structDeclNode = spice_pointer_cast<StructDefNode *>(structSymbol->declNode);
if (!structDeclNode->hasInterfaces && structDeclNode->emitVTable)
fieldTypes.push_back(llvm::PointerType::get(context, 0));
// Collect all field types
for (size_t i = 0; i < totalFieldCount; i++) {
const SymbolTableEntry *fieldSymbol = spiceStruct->scope->symbolTable.lookupStrictByIndex(i);
assert(fieldSymbol != nullptr);
fieldTypes.push_back(fieldSymbol->getType().toLLVMType(context, accessScope));
}
// Check if the struct is declared as packed
if (structDeclNode->attrs() && structDeclNode->attrs()->attrLst()->hasAttr(ATTR_CORE_COMPILER_PACKED))
isPacked = structDeclNode->attrs()->attrLst()->getAttrValueByName(ATTR_CORE_COMPILER_PACKED)->boolValue;
} else { // Interface
Interface *spiceInterface = structSymbol->getType().getInterface(structSymbol->declNode);
assert(spiceInterface != nullptr);
const std::string mangledName = NameMangling::mangleInterface(*spiceInterface);
structType = llvm::StructType::create(context, mangledName);
structSymbol->setStructLLVMType(structType);
fieldTypes.push_back(llvm::PointerType::get(context, 0));
}
// Set field types to struct type
structType->setBody(fieldTypes, isPacked);
}
return structType;
}
if (is(TY_ENUM))
return llvm::Type::getInt32Ty(context);
if (isPtr() || isRef() || (isArray() && getArraySize() == 0))
return llvm::PointerType::get(context, 0);
if (isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
llvm::PointerType *ptrTy = llvm::PointerType::get(context, 0);
return llvm::StructType::get(context, {ptrTy, ptrTy});
}
if (isArray()) {
llvm::Type *containedType = getContainedTy().toLLVMType(context, accessScope);
return llvm::ArrayType::get(containedType, getArraySize());
}
throw CompilerError(UNHANDLED_BRANCH, "Cannot determine LLVM type of " + getName(true)); // GCOVR_EXCL_LINE
}
/**
* Check if the current type is an iterator
*
* @param node ASTNode
* @return Iterator or not
*/
bool SymbolType::isIterator(const ASTNode *node) const {
if (!is(TY_STRUCT))
return false;
SymbolType genericType(TY_GENERIC, "T");
SymbolType iteratorType(TY_INTERFACE, IITERATOR_NAME, TYPE_ID_ITERATOR_INTERFACE, {.bodyScope = nullptr}, {genericType});
return implements(iteratorType, node);
}
/**
* Check if the current type is an iterable
* - Arrays are always considered iterable
* - Otherwise the type must be a struct that implements the iterator interface
*
* @param node ASTNode
* @return Iterable or not
*/
bool SymbolType::isIterable(const ASTNode *node) const {
if (isArray())
return true; // Arrays are always considered iterable
if (!is(TY_STRUCT))
return false;
SymbolType genericType(TY_GENERIC, "T");
SymbolType iteratorType(TY_INTERFACE, IITERATOR_NAME, TYPE_ID_ITERABLE_INTERFACE, {.bodyScope = nullptr}, {genericType});
return implements(iteratorType, node);
}
/**
* Check if the current type is a string object
*
* @return String object or not
*/
bool SymbolType::isStringObj() const {
return is(TY_STRUCT) && getSubType() == STROBJ_NAME && getBodyScope()->sourceFile->stdFile;
}
/**
* Check if the current type is a error object
*
* @return Error object or not
*/
bool SymbolType::isErrorObj() const {
return is(TY_STRUCT) && getSubType() == ERROBJ_NAME && getBodyScope()->sourceFile->stdFile;
}
/**
* Check if the current type implements the given interface type
*
* @param symbolType Interface type
* @param node ASTNode
* @return Struct implements interface or not
*/
bool SymbolType::implements(const SymbolType &symbolType, const ASTNode *node) const {
assert(is(TY_STRUCT) && symbolType.is(TY_INTERFACE));
Struct *spiceStruct = getStruct(node);
assert(spiceStruct != nullptr);
return std::ranges::any_of(spiceStruct->interfaceTypes, [&](const SymbolType &interfaceType) {
assert(interfaceType.is(TY_INTERFACE));
return symbolType.matches(interfaceType, false, false, true);
});
}
/**
* Check if the base type of the current type chain is of a certain super type
*
* @param superType Super type to check for
* @return Applicable or not
*/
bool SymbolType::isBaseType(SymbolSuperType superType) const {
assert(!typeChain.empty());
return typeChain.front().superType == superType;
}
/**
* Check if the current type is of the same container type like the other type.
* Only TY_PTR, TY_REF and TY_ARRAY are considered as container types.
*
* @param otherType Other symbol type
* @return Same container type or not
*/
bool SymbolType::isSameContainerTypeAs(const SymbolType &otherType) const {
return (isPtr() && otherType.isPtr()) || (isRef() && otherType.isRef()) || (isArray() && otherType.isArray());
}
/**
* Checks if the base type is generic itself or has generic parts in its template types
*
* @return Contains generic parts or not
*/
bool SymbolType::hasAnyGenericParts() const { // NOLINT(misc-no-recursion)
const SymbolType &baseType = getBaseType();
// Check if the type itself is generic
if (baseType.is(TY_GENERIC))
return true;
// Check if the type has generic template types
const auto templateTypes = baseType.getTemplateTypes();
if (std::ranges::any_of(templateTypes, [](const SymbolType &t) { return t.hasAnyGenericParts(); }))
return true;
// Check param and return types or functions/procedures
if (baseType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
const auto paramTypes = baseType.getFunctionParamAndReturnTypes();
if (std::ranges::any_of(paramTypes, [](const SymbolType &t) { return t.hasAnyGenericParts(); }))
return true;
}
return false; // Does not have generic parts
}
/**
* Set the list of templates types
*/
void SymbolType::setTemplateTypes(const std::vector<SymbolType> &templateTypes) {
assert(isOneOf({TY_STRUCT, TY_INTERFACE}));
typeChain.back().templateTypes = templateTypes;
}
/**
* Set the list of templates types of the base type
*/
void SymbolType::setBaseTemplateTypes(const std::vector<SymbolType> &templateTypes) {
assert(getBaseType().isOneOf({TY_STRUCT, TY_INTERFACE}));
typeChain.front().templateTypes = templateTypes;
}
/**
* Retrieve template types of the current type
*
* @return Vector of template types
*/
const std::vector<SymbolType> &SymbolType::getTemplateTypes() const { return typeChain.back().templateTypes; }
/**
* Check if the given generic type list has a substantiation for the current (generic) type
*
* @param genericTypeList Generic type list
* @return Has substantiation or not
*/
bool SymbolType::isCoveredByGenericTypeList(std::vector<GenericType> &genericTypeList) const {
const SymbolType baseType = getBaseType();
// Check if the symbol type itself is generic
if (baseType.is(TY_GENERIC)) {
return std::ranges::any_of(genericTypeList, [&](GenericType &t) {
if (baseType.matches(t, true, true, true)) {
t.used = true;
return true;
}
return false;
});
}
// If the type is non-generic check template types
bool covered = true;
// Check template types
const std::vector<SymbolType> &baseTemplateTypes = baseType.getTemplateTypes();
covered &= std::ranges::all_of(baseTemplateTypes, [&](const SymbolType &templateType) {
return templateType.isCoveredByGenericTypeList(genericTypeList);
});
// If function/procedure, check param and return types
if (baseType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
const std::vector<SymbolType> ¶mAndReturnTypes = baseType.getFunctionParamAndReturnTypes();
covered &= std::ranges::all_of(
paramAndReturnTypes, [&](const SymbolType ¶mType) { return paramType.isCoveredByGenericTypeList(genericTypeList); });
}
return covered;
}
/**
* Get the name of the symbol type as a string
*
* @param withSize Include the array size for sized types
* @param ignorePublic Ignore any potential public specifier
* @return Symbol type name
*/
std::string SymbolType::getName(bool withSize, bool ignorePublic) const { // NOLINT(misc-no-recursion)
std::stringstream name;
// Append the specifiers
const TypeSpecifiers defaultForSuperType = TypeSpecifiers::of(getBaseType().getSuperType());
if (!ignorePublic && specifiers.isPublic && !defaultForSuperType.isPublic)
name << "public ";
if (specifiers.isInline && !defaultForSuperType.isInline)
name << "inline ";
if (specifiers.isComposition && !defaultForSuperType.isComposition)
name << "compose ";
if (specifiers.isConst && !defaultForSuperType.isConst)
name << "const ";
if (specifiers.isHeap && !defaultForSuperType.isHeap)
name << "heap ";
if (specifiers.isSigned && !defaultForSuperType.isSigned)
name << "signed ";
if (!specifiers.isSigned && defaultForSuperType.isSigned)
name << "unsigned ";
// Loop through all chain elements
for (const TypeChainElement &chainElement : typeChain)
name << chainElement.getName(withSize);
return name.str();
}
/**
* Set the return type of a function type
*
* @param returnType Function return type
*/
void SymbolType::setFunctionReturnType(const SymbolType &returnType) {
assert(is(TY_FUNCTION));
std::vector<SymbolType> ¶mTypes = typeChain.back().paramTypes;
if (paramTypes.empty())
paramTypes.resize(1);
paramTypes.at(0) = returnType;
}
/**
* Get the return type of a function type
*
* @return Function return type
*/
const SymbolType &SymbolType::getFunctionReturnType() const {
assert(is(TY_FUNCTION));
assert(!typeChain.back().paramTypes.empty());
return typeChain.back().paramTypes.front();
}
/**
* Set the param types of a function or procedure type
*
* @param paramTypes Function param types
*/
void SymbolType::setFunctionParamTypes(const std::vector<SymbolType> &newParamTypes) {
assert(isOneOf({TY_FUNCTION, TY_PROCEDURE}));
std::vector<SymbolType> ¶mTypes = typeChain.back().paramTypes;
// Resize param types if required
if (paramTypes.size() < newParamTypes.size() + 1)
paramTypes.resize(newParamTypes.size() + 1, SymbolType(TY_DYN));
// Set the function param types
for (size_t i = 0; i < newParamTypes.size(); i++)
paramTypes.at(i + 1) = newParamTypes.at(i);
}
/**
* Get the param types of a function or procedure type
*
* @return Function param types
*/
std::vector<SymbolType> SymbolType::getFunctionParamTypes() const {
assert(isOneOf({TY_FUNCTION, TY_PROCEDURE}));
if (typeChain.back().paramTypes.empty())
return {};
return {typeChain.back().paramTypes.begin() + 1, typeChain.back().paramTypes.end()};
}
/**
* Set has captures of a function or procedure type
*
* @param hasCaptures Has captures
*/
void SymbolType::setHasLambdaCaptures(bool hasCaptures) {
assert(getBaseType().isOneOf({TY_FUNCTION, TY_PROCEDURE}));
typeChain.front().data.hasCaptures = hasCaptures;
}
/**
* Check if a function or procedure type has captures
*
* @return Has captures
*/
bool SymbolType::hasLambdaCaptures() const {
assert(getBaseType().isOneOf({TY_FUNCTION, TY_PROCEDURE}));
return typeChain.front().data.hasCaptures;
}
/**
* Set the param and return types of a function or procedure base type
*
* @param newParamAndReturnTypes Function param and return types (first is return type, rest are param types)
*/
void SymbolType::setFunctionParamAndReturnTypes(const std::vector<SymbolType> &newParamAndReturnTypes) {
assert(getBaseType().isOneOf({TY_FUNCTION, TY_PROCEDURE}));
typeChain.front().paramTypes = newParamAndReturnTypes;
}
/**
* Get the param and return types of a function or procedure base type
*
* @return Function param and return types (first is return type, rest are param types)
*/
const std::vector<SymbolType> &SymbolType::getFunctionParamAndReturnTypes() const {
assert(getBaseType().isOneOf({TY_FUNCTION, TY_PROCEDURE}));
return typeChain.front().paramTypes;
}
/**
* Get the struct instance for a struct type
*
* @param node Accessing AST node
* @return Struct instance
*/
Struct *SymbolType::getStruct(const ASTNode *node) const {
assert(is(TY_STRUCT));
Scope *structDefScope = getBodyScope()->parent;
const std::string structName = getSubType();
const std::vector<SymbolType> &templateTypes = getTemplateTypes();
return StructManager::matchStruct(structDefScope, structName, templateTypes, node);
}
/**
* Get the interface instance for an interface type
*
* @param node Accessing AST node
* @return Interface instance
*/
Interface *SymbolType::getInterface(const ASTNode *node) const {
assert(is(TY_INTERFACE));
Scope *interfaceDefScope = getBodyScope()->parent;
const std::string structName = getSubType();
const std::vector<SymbolType> &templateTypes = getTemplateTypes();
return InterfaceManager::matchInterface(interfaceDefScope, structName, templateTypes, node);
}
bool operator==(const SymbolType &lhs, const SymbolType &rhs) {
return lhs.typeChain == rhs.typeChain && lhs.specifiers == rhs.specifiers;
}
bool operator!=(const SymbolType &lhs, const SymbolType &rhs) { return !(lhs == rhs); }
/**
* Check for the matching compatibility of two types.
* Useful for struct and function matching as well as assignment type validation and function arg matching.
*
* @param otherType Type to compare against
* @param ignoreArraySize Ignore array sizes
* @param ignoreSpecifiers Ignore specifiers, except for pointer and reference types
* @param allowConstify Match when the types are the same, but the lhs type is more const restrictive than the rhs type
* @return Matching or not
*/
bool SymbolType::matches(const SymbolType &otherType, bool ignoreArraySize, bool ignoreSpecifiers, bool allowConstify) const {
// If the size does not match, it is not equal
if (typeChain.size() != otherType.typeChain.size())
return false;
// Compare the elements
for (size_t i = 0; i < typeChain.size(); i++) {
const SymbolType::TypeChainElement &lhsElement = typeChain.at(i);
const SymbolType::TypeChainElement &rhsElement = otherType.typeChain.at(i);
// Ignore differences in array size
if (ignoreArraySize && lhsElement.superType == TY_ARRAY && rhsElement.superType == TY_ARRAY)
continue;
// Not both types are arrays -> compare them as usual
if (lhsElement != rhsElement)
return false;
}
// Ignore or compare specifiers
return ignoreSpecifiers || specifiers.match(otherType.specifiers, allowConstify);
}
/**
* Check for the matching compatibility of two types in terms of interface implementation.
* Useful for function matching as well as assignment type validation and function arg matching.
*
* @param otherType Type to compare against
* @return Matching or not
*/
bool SymbolType::matchesInterfaceImplementedByStruct(const SymbolType &otherType) const {
if (!is(TY_INTERFACE) || !otherType.is(TY_STRUCT))
return false;
// Check if the rhs is a struct type that implements the lhs interface type
const Struct *spiceStruct = otherType.getStruct(nullptr);
assert(spiceStruct != nullptr);
for (const SymbolType &interfaceType : spiceStruct->interfaceTypes) {
assert(interfaceType.is(TY_INTERFACE));
if (matches(interfaceType, false, false, true))
return true;
}
return false;
}
/**
* Check if a certain input type can be bound (assigned) to the current type.
*
* @param inputType Type, which should be bound to the current type
* @param isTemporary Is the input type a temporary type
* @return Can be bound or not
*/
bool SymbolType::canBind(const SymbolType &inputType, bool isTemporary) const {
return !isTemporary || inputType.isRef() || !isRef() || isConstRef();
}
/**
* Remove pointers / arrays / references if both types have them as far as possible.
* Furthermore, remove reference wrappers if possible.
*
* @param typeA Candidate type
* @param typeB Requested type
*/
void SymbolType::unwrapBoth(SymbolType &typeA, SymbolType &typeB) {
// Remove reference wrapper of front type if required
if (typeA.isRef() && !typeB.isRef())
typeA = typeA.removeReferenceWrapper();
// Remove reference wrapper of requested type if required
if (!typeA.isRef() && typeB.isRef() && !typeA.getBaseType().is(TY_GENERIC))
typeB = typeB.removeReferenceWrapper();
// Unwrap both types as far as possible
while (typeA.isSameContainerTypeAs(typeB)) {
typeB = typeB.getContainedTy();
typeA = typeA.getContainedTy();
}
}
} // namespace spice::compiler