-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathTypes.cpp
More file actions
949 lines (811 loc) · 30.9 KB
/
Types.cpp
File metadata and controls
949 lines (811 loc) · 30.9 KB
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
#include "Types.h"
#include "ArrayType.h"
#include "ObjectPointerType.h"
#include "SimpleType.h"
#include "TypeVisitor.h"
#include "exceptions/UnImplementedException.h"
#include "utils/PrinterUtils.h"
#include "utils/SizeUtils.h"
#include "loguru.h"
#include <climits>
/*
* class Type
*/
types::Type::Type(clang::QualType qualType, TypeName usedTypeName, const clang::SourceManager &sourceManager): mUsedType(std::move(usedTypeName)) {
clang::QualType canonicalType = qualType.getCanonicalType();
auto pp = clang::PrintingPolicy(clang::LangOptions());
fs::path sourceFilePath = sourceManager.getFileEntryForID(sourceManager.getMainFileID())->tryGetRealPathName().str();
if (Paths::getSourceLanguage(sourceFilePath) == utbot::Language::CXX) {
pp.adjustForCPlusPlus();
}
mType = canonicalType.getNonReferenceType().getUnqualifiedType().getAsString(pp);
TypeVisitor visitor;
visitor.TraverseType(qualType);
mKinds = visitor.getKinds();
dimension = getDimension();
mBaseType = visitor.getTypes()[dimension];
mTypeId = getIdFromCanonicalType(canonicalType);
AbstractType *baseType = mKinds[dimension].get();
if (auto simpleType = dynamic_cast<SimpleType*>(baseType)) {
mBaseTypeId = simpleType->getId();
}
}
uint64_t types::Type::getIdFromCanonicalType(clang::QualType canonicalType) {
auto unqualifiedType = canonicalType.getNonReferenceType().getUnqualifiedType();
if (auto tagType = llvm::dyn_cast<clang::TagType>(unqualifiedType)) {
auto decl = tagType->getDecl();
clang::SourceManager const &sourceManager = decl->getASTContext().getSourceManager();
const clang::SourceLocation spellingLoc = sourceManager.getSpellingLoc(decl->getLocation());
fs::path filePath = sourceManager.getFilename(spellingLoc).str();
std::string name = unqualifiedType.getAsString();
uint64_t id = 0;
HashUtils::hashCombine(id, name, filePath);
LOG_S(DEBUG) << "Name: " << name << ", file: " << filePath << ", id: " << id;
return id;
} else {
return 0;
}
}
types::Type::Type(const types::TypeName& type, size_t pointersNum) {
if (pointersNum > 0) {
mType = type + " " + std::string(pointersNum, '*');
} else {
mType = type;
}
mUsedType = mType;
dimension = pointersNum;
mBaseType = type;
for (size_t i = 0; i < pointersNum; ++i) {
mKinds.push_back(std::make_shared<ObjectPointerType>(false));
}
mKinds.push_back(std::make_shared<SimpleType>(0, false, false, SimpleType::ReferenceType::NotReference));
}
types::Type types::Type::createSimpleTypeFromName(const types::TypeName& type, size_t pointersNum) {
return Type(type, pointersNum);
}
types::Type types::Type::createConstTypeFromName(const types::TypeName& type, size_t pointersNum) {
auto res = createSimpleTypeFromName(type, pointersNum);
res.mType = "const " + res.mType;
res.mUsedType = res.mType;
return res;
}
types::Type types::Type::createArray(const types::Type &type) {
Type res;
res.mType = type.typeName() + "*";
res.mUsedType = res.mType;
res.mBaseType = type.baseType();
res.mKinds = type.mKinds;
res.mKinds.insert(res.mKinds.begin(), std::shared_ptr<AbstractType>(new ArrayType(
TypesHandler::getElementsNumberInPointerOneDim(PointerUsage::PARAMETER), false)));
res.dimension = type.dimension + 1;
res.mTypeId = 0;
res.mBaseTypeId = type.mBaseTypeId;
res.maybeArray = true;
return res;
}
types::TypeName types::Type::typeName() const {
return mType;
}
types::TypeName types::Type::baseType() const {
return mBaseType;
}
types::TypeName types::Type::usedType() const {
return mUsedType;
}
types::Type types::Type::baseTypeObj(size_t depth) const {
auto type = *this;
type.mType = mBaseType;
type.mBaseType = type.mType;
type.mUsedType = type.mType;
type.mKinds.erase(type.mKinds.begin(), type.mKinds.begin() + depth);
type.dimension = type.getDimension();
type.mTypeId = mBaseTypeId;
type.mBaseTypeId = {};
return type;
}
types::Type types::Type::baseTypeObj() const {
return baseTypeObj(getDimension());
}
std::string types::Type::mTypeName() const {
return this->mType;
}
size_t types::Type::getDimension() const {
// usage doesn't matter here
return arraysSizes(PointerUsage::PARAMETER).size();
}
std::optional<uint64_t> types::Type::getBaseTypeId() const {
return mBaseTypeId;
}
bool types::Type::maybeJustPointer() const {
return types::TypesHandler::isObjectPointerType(*this) && !maybeArray &&
this->kinds().size() < 3;
}
bool types::Type::isFilePointer() const {
return typeName() == FILE_PTR_TYPE;
}
bool types::Type::maybeReturnArray() const {
bool condition1 = types::TypesHandler::isObjectPointerType(*this);
bool condition2 = maybeJustPointer();
bool condition3 = types::TypesHandler::isArrayOfPointersToFunction(*this);
bool condition4 = this->kinds().size() < 3;
bool condition5 = types::TypesHandler::isVoid(this->baseTypeObj());
return condition1 && !condition2 && !condition3 && condition4 && !condition5;
}
size_t types::Type::countReturnPointers(bool decrementIfArray) const {
size_t returnPointer = 0;
const std::vector<std::shared_ptr<AbstractType>> pointerArrayKinds = this->pointerArrayKinds();
for (const auto &pointerArrayKind: pointerArrayKinds) {
returnPointer += pointerArrayKind->getKind() == AbstractType::OBJECT_POINTER;
}
if (decrementIfArray && maybeReturnArray()) {
returnPointer--;
}
return returnPointer;
}
const std::vector<std::shared_ptr<AbstractType>> &types::Type::kinds() const {
return mKinds;
}
std::vector<size_t> types::Type::arraysSizes(PointerUsage usage) const {
if (!isArray() && !isObjectPointer() && !isPointerToFunction()) {
return {};
}
std::vector<size_t> sizes;
for (const auto &kind: pointerArrayKinds()) {
switch (kind->getKind()) {
case AbstractType::ARRAY:
sizes.push_back(kind->getSize());
break;
case AbstractType::OBJECT_POINTER:
case AbstractType::FUNCTION_POINTER:
if (kinds().size() <= 2) {
sizes.push_back(types::TypesHandler::getElementsNumberInPointerOneDim(usage));
} else {
sizes.push_back(types::TypesHandler::getElementsNumberInPointerMultiDim(usage));
}
if (usage == types::PointerUsage::LAZY) {
return sizes;
}
break;
default:
LOG_S(ERROR) << "INVARIANT ERROR: Class Type: " << kind->getKind();
}
}
return sizes;
}
std::vector<std::shared_ptr<AbstractType>> types::Type::pointerArrayKinds() const {
if (kinds().size() <= 1) {
return {};
}
std::vector<std::shared_ptr<AbstractType>> res;
res.reserve(kinds().size() - 1);
size_t i = 0;
while (i < kinds().size() - 1 &&
(mKinds[i]->getKind() == AbstractType::OBJECT_POINTER || mKinds[i]->getKind() == AbstractType::ARRAY)) {
res.push_back(mKinds[i]);
++i;
}
return res;
}
bool types::Type::isArrayCandidate() const {
return isObjectPointer() || isArray();
}
bool types::Type::isObjectPointer() const {
return mKinds.front()->getKind() == AbstractType::OBJECT_POINTER;
}
bool types::Type::isArray() const {
return mKinds.front()->getKind() == AbstractType::ARRAY;
}
bool types::Type::isPointerToFunction() const {
return mKinds.front()->getKind() == AbstractType::FUNCTION_POINTER;
}
bool types::Type::isArrayOfPointersToFunction() const {
return mKinds.size() > 1 &&
mKinds[0]->getKind() == AbstractType::OBJECT_POINTER &&
mKinds[1]->getKind() == AbstractType::FUNCTION_POINTER;
}
bool types::Type::isSimple() const {
return mKinds.front()->getKind() == AbstractType::SIMPLE;
}
bool types::Type::isUnnamed() const {
return isSimple() && dynamic_cast<SimpleType *>(mKinds.front().get())->isUnnamed();
}
bool types::Type::isLValueReference() const {
return isSimple() && dynamic_cast<SimpleType *>(mKinds.front().get())->isLValue();
}
bool types::Type::isConstQualified() const {
return isSimple() && dynamic_cast<SimpleType *>(mKinds.front().get())->isConstQualified();
}
static const types::TypeName MINIMAL_SCALAR_TYPE = "unsigned char";
types::Type types::Type::minimalScalarType() {
static types::Type minimalScalarTypeSingleton = createSimpleTypeFromName(MINIMAL_SCALAR_TYPE);
return minimalScalarTypeSingleton;
}
types::Type types::Type::minimalScalarPointerType(size_t pointersNum) {
return createSimpleTypeFromName(MINIMAL_SCALAR_TYPE, pointersNum);
}
types::Type types::Type::intType() {
static types::Type intTypeSingleton = createSimpleTypeFromName("int");
return intTypeSingleton;
}
types::Type types::Type::longlongType() {
static types::Type longlongTypeSingleton = createSimpleTypeFromName("long long");
return longlongTypeSingleton;
}
types::Type types::Type::CStringType() {
static types::Type cStringTypeSingleton = createConstTypeFromName("char", 1);
cStringTypeSingleton.maybeArray = true;
return cStringTypeSingleton;
}
const std::string &types::Type::getStdinParamName() {
static const std::string stdinParamName = "stdin_buf";
return stdinParamName;
}
bool types::Type::isPointerToPointer() const {
const std::vector<std::shared_ptr<AbstractType>> pointerArrayKinds = this->pointerArrayKinds();
return pointerArrayKinds.size() > 1 &&
pointerArrayKinds[0]->getKind() == AbstractType::OBJECT_POINTER &&
pointerArrayKinds[1]->getKind() == AbstractType::OBJECT_POINTER;
}
bool types::Type::isTwoDimensionalPointer() const {
return isPointerToPointer() && kinds().size() == 3;
}
bool types::Type::isPointerToArray() const {
const std::vector<std::shared_ptr<AbstractType>> pointerArrayKinds = this->pointerArrayKinds();
return pointerArrayKinds.size() > 1 &&
pointerArrayKinds[0]->getKind() == AbstractType::OBJECT_POINTER &&
pointerArrayKinds[1]->getKind() == AbstractType::ARRAY;
}
bool types::Type::isConstQualifiedValue() const {
for (const auto &kind: mKinds) {
if(kind->getKind() == AbstractType::SIMPLE) {
if(dynamic_cast<SimpleType*>(kind.get())->isConstQualified()) {
return true;
} else {
return false;
}
} else if (kind->getKind() != AbstractType::ARRAY &&
kind->getKind() != AbstractType::OBJECT_POINTER) {
return false;
}
}
return false;
}
bool types::Type::isTypeContainsPointer() const {
const std::vector<std::shared_ptr<AbstractType>> pointerArrayKinds = this->pointerArrayKinds();
return std::any_of(pointerArrayKinds.cbegin(), pointerArrayKinds.cend(),
[](auto const &kind){ return kind->getKind() == AbstractType::OBJECT_POINTER; });
}
bool types::Type::isTypeContainsFunctionPointer() const {
return std::any_of(mKinds.cbegin(), mKinds.cend(),
[](auto const &kind){ return kind->getKind() == AbstractType::FUNCTION_POINTER; });
}
int types::Type::indexOfFirstPointerInTypeKinds() const {
int index = 0;
for (const auto &kind : pointerArrayKinds()) {
if (kind->getKind() == AbstractType::OBJECT_POINTER) {
return index;
}
index++;
}
throw BaseException("Couldn't find pointer in type " + typeName());
}
bool types::Type::isOneDimensionPointer() const {
return isObjectPointer() && !isPointerToPointer() && !isPointerToArray();
}
types::Type types::Type::arrayClone(PointerUsage usage, size_t pointerSize) const {
Type t = *this;
t.mKinds[0] = std::make_shared<ArrayType>(TypesHandler::getElementsNumberInPointerOneDim(usage, pointerSize), true);
return t;
}
types::Type types::Type::arrayCloneMultiDim(PointerUsage usage, std::vector<size_t> pointerSizes) const {
Type t = *this;
for(size_t i = 0; i < pointerSizes.size(); ++i) {
if (t.mKinds[i]->getKind() == AbstractType::OBJECT_POINTER) {
t.mKinds[i] = std::make_shared<ArrayType>(
TypesHandler::getElementsNumberInPointerMultiDim(usage, pointerSizes[i]),
true);
}
}
return t;
}
types::Type types::Type::arrayCloneMultiDim(PointerUsage usage) const {
if (this->maybeJustPointer() && this->pointerArrayKinds().size() < 2) {
return this->baseTypeObj();
}
std::vector<size_t> pointerSizes = this->arraysSizes(usage);
if(pointerSizes.size() == 1) {
return arrayClone(usage);
}
return this->arrayCloneMultiDim(usage, pointerSizes);
}
uint64_t types::Type::getId() const {
return mTypeId.value_or(0);
}
void types::Type::replaceUsedType(const types::TypeName &newUsedType) {
mUsedType = newUsedType;
}
/*
* Integer types
*/
static const std::unordered_map<std::string, size_t> integerTypesToSizes = {
{"utbot_byte", SizeUtils::bytesToBits(sizeof(char))}, // we use different name to not trigger char processing
{"short", SizeUtils::bytesToBits(sizeof(short))},
{"int", SizeUtils::bytesToBits(sizeof(int))},
{"long", SizeUtils::bytesToBits(sizeof(long))},
{"long long", SizeUtils::bytesToBits(sizeof(long long))},
{"unsigned short", SizeUtils::bytesToBits(sizeof(unsigned short))},
{"unsigned int", SizeUtils::bytesToBits(sizeof(unsigned int))},
{"unsigned long", SizeUtils::bytesToBits(sizeof(unsigned long))},
{"unsigned long long", SizeUtils::bytesToBits(sizeof(unsigned long long))},
{"unsigned char", SizeUtils::bytesToBits(sizeof(unsigned char))} // we do not want to treat an unsigned char as character literal
};
bool types::TypesHandler::isIntegerType(const Type &type) {
return type.isSimple() && isIntegerType(type.baseType());
}
bool types::TypesHandler::isUnsignedType(const Type &type) {
return StringUtils::startsWith(type.baseType(), "unsigned");
}
bool types::TypesHandler::isIntegerType(const TypeName &typeName) {
return CollectionUtils::containsKey(integerTypesToSizes, typeName);
}
/*
* Floating point types
*/
static const std::unordered_map<std::string, size_t> floatingPointTypesToSizes = {
{"float", SizeUtils::bytesToBits(sizeof(float))},
{"double", SizeUtils::bytesToBits(sizeof(double))},
{"long double", SizeUtils::bytesToBits(sizeof(long double))}
};
bool types::TypesHandler::isFloatingPointType(const Type &type) {
return type.isSimple() && isFloatingPointType(type.baseType());
}
bool types::TypesHandler::isFloatingPointType(const TypeName &type) {
return CollectionUtils::containsKey(floatingPointTypesToSizes, type);
}
/*
* Character types
*/
static const std::unordered_map<std::string, size_t> characterTypesToSizes = {
{"char", SizeUtils::bytesToBits(sizeof(char))},
{"signed char", SizeUtils::bytesToBits(sizeof(signed char))},
};
bool types::TypesHandler::isCharacterType(const Type &type) {
return type.isSimple() && isCharacterType(type.baseType());
}
bool types::TypesHandler::isCharacterType(const TypeName &type) {
return CollectionUtils::containsKey(characterTypesToSizes, type);
}
/*
* Boolean types
*/
static const std::unordered_map<std::string, size_t> boolTypesToSizes = {
{"bool", SizeUtils::bytesToBits(sizeof(bool))},
{"_Bool", SizeUtils::bytesToBits(sizeof(bool))}
};
bool types::TypesHandler::isBoolType(const Type &type) {
return type.isSimple() && isBoolType(type.baseType());
}
bool types::TypesHandler::isBoolType(const TypeName &type) {
return CollectionUtils::containsKey(boolTypesToSizes, type);
}
/*
* All primitive types
*/
bool types::TypesHandler::isPrimitiveType(const Type &type) {
return isIntegerType(type)
|| isFloatingPointType(type)
|| isCharacterType(type)
|| isBoolType(type)
|| isVoid(type);
}
bool types::TypesHandler::isCStringType(const Type &type) {
return isOneDimensionPointer(type) && isCharacterType(type.baseType());
}
bool types::TypesHandler::isCppStringType(const Type &type) {
return type.typeName() == "string";
}
/*
* Struct types (structs and unions)
*/
bool types::TypesHandler::isStructLike(const Type &type) const {
return type.isSimple() && isStructLike(type.getId());
}
bool types::TypesHandler::isStructLike(uint64_t id) const {
return typeIsInMap(id, typeMaps.structs);
}
/*
* Enum types
*/
bool types::TypesHandler::isEnum(const types::Type &type) const {
return type.isSimple() && isEnum(type.getId());
}
bool types::TypesHandler::isEnum(uint64_t id) const {
return typeIsInMap(id, typeMaps.enums);
}
/*
* Void type
*/
bool types::TypesHandler::isVoid(const Type &type) {
return type.isSimple() && isVoid(type.baseType());
}
bool types::TypesHandler::baseTypeIsVoid(const Type &type) {
return isVoid(type.baseType());
}
bool types::TypesHandler::isVoid(const TypeName &type) {
return type == "void";
}
bool types::TypesHandler::isPointerToFunction(const Type& type) {
return type.isPointerToFunction();
}
bool types::TypesHandler::isArrayOfPointersToFunction(const Type& type) {
return type.isArrayOfPointersToFunction();
}
bool types::TypesHandler::omitMakeSymbolic(const Type& type) {
return isVoid(type) || type.isPointerToFunction() ||
type.isArrayOfPointersToFunction() || type.isObjectPointer();
}
bool types::TypesHandler::skipTypeInReturn(const Type& type) {
return isVoid(type) || isPointerToFunction(type) || isArrayOfPointersToFunction(type);
}
/*
* Get struct information
*/
types::StructInfo types::TypesHandler::getStructInfo(const Type &type) const {
return getStructInfo(type.getId());
}
types::StructInfo types::TypesHandler::getStructInfo(uint64_t id) const {
return typeFromMap<StructInfo>(id, typeMaps.structs);
}
/*
* Get enum information
*/
types::EnumInfo types::TypesHandler::getEnumInfo(const types::Type &type) const {
return getEnumInfo(type.getId());
}
types::EnumInfo types::TypesHandler::getEnumInfo(uint64_t id) const {
return typeFromMap<EnumInfo>(id, typeMaps.enums);
}
/**
* Checks whether type is a pointer
*/
bool types::TypesHandler::isObjectPointerType(const Type &type) {
return type.isObjectPointer();
}
/**
* Checks whether type is an array
*/
bool types::TypesHandler::isArrayType(const Type &type) {
return type.isArray();
}
bool types::TypesHandler::isIncompleteArrayType(const Type &type) {
return type.isArray() && !dynamic_cast<ArrayType *>(type.kinds().front().get())->isComplete();
}
bool types::TypesHandler::isOneDimensionPointer(const types::Type &type) {
return type.isOneDimensionPointer();
}
size_t types::TypesHandler::typeSize(const types::Type &type) const {
if (isIntegerType(type)) {
return integerTypesToSizes.at(type.baseType());
}
if (isBoolType(type)) {
return boolTypesToSizes.at(type.baseType());
}
if (isFloatingPointType(type)) {
return floatingPointTypesToSizes.at(type.baseType());
}
if (isCharacterType(type)) {
return characterTypesToSizes.at(type.baseType());
}
if (isStructLike(type)) {
return getStructInfo(type).size;
}
if (isEnum(type)) {
return getEnumInfo(type).size;
}
if (isArrayType(type)) {
size_t elementsNum = type.kinds().front()->getSize();
size_t elementSize = typeSize(type.baseTypeObj());
return elementSize * elementsNum;
}
if (isObjectPointerType(type)) {
return SizeUtils::bytesToBits(getPointerSize());
}
if (isPointerToFunction(type)) {
return SizeUtils::bytesToBits(sizeof(char *));
}
throw UnImplementedException("Type is unknown for: " + type.typeName());
}
std::string types::TypesHandler::removeConstPrefix(const TypeName &type) {
std::vector<std::string> tmp = StringUtils::split(type);
if (tmp[0] == CONST_QUALIFIER) {
std::string res;
for (size_t i = 1; i < tmp.size(); i++) {
res += tmp[i];
if (i + 1 < tmp.size()) {
res.push_back(' ');
}
}
return res;
}
return type;
}
bool types::TypesHandler::hasConstModifier(const types::TypeName &typeName) {
const auto &splitType = StringUtils::splitByWhitespaces(typeName);
return std::find(splitType.begin(), splitType.end(), CONST_QUALIFIER) != splitType.end();
}
std::string types::TypesHandler::removeArrayReference(TypeName type) {
if (type[0] == '*') {
type = type.substr(1, type.size());
} else if (type.back() == '*') {
type.pop_back();
}
if (type.size() > 2 && type.back() == ']' && type[type.size() - 2] == '[') {
type.pop_back();
type.pop_back();
}
StringUtils::trim(type);
return type;
}
std::string types::TypesHandler::removeArrayBrackets(TypeName type) {
if (type.back() == ']') {
while (!type.empty() && type.back() != '[') {
type.pop_back();
}
if (!type.empty()) {
type.pop_back();
}
}
StringUtils::trim(type);
return type;
}
testsgen::ValidationType types::TypesHandler::getIntegerValidationType(const Type &type) {
size_t size;
if (isIntegerType(type)) {
size = integerTypesToSizes.at(type.baseType());
} else {
ABORT_F("type is not an integerType: %s", type.baseType().c_str());
}
bool isUnsigned = isUnsignedType(type);
if (size == 8) {
return (isUnsigned) ? testsgen::UINT8_T : testsgen::INT8_T;
} else if (size == 16) {
return (isUnsigned) ? testsgen::UINT16_T : testsgen::INT16_T;
} else if (size == 32) {
return (isUnsigned) ? testsgen::UINT32_T : testsgen::INT32_T;
} else if (size == 64) {
return (isUnsigned) ? testsgen::UINT64_T : testsgen::INT64_T;
} else {
ABORT_F("Unknown integer size: %zu", size);
}
}
const std::unordered_map<types::TypeName, std::vector<std::string>> &types::TypesHandler::preferredConstraints() noexcept {
static const std::unordered_map<std::string, std::vector<std::string>> constraints = {
{"char", {">= 'a'", "<= 'z'", "!= '\\0'"}},
{"signed char", {">= 'a'", "<= 'z'", "!= '\\0'"}},
{"unsigned char", {">= 'a'", "<= 'z'", "!= '\\0'"}},
{"short", {">= -10", "<= 10"}},
{"int", {">= -10", "<= 10"}},
{"long", {">= -10", "<= 10"}},
{"long long", {">= -10", "<= 10"}},
{"unsigned short", {"<= 10"}},
{"unsigned int", {"<= 10"}},
{"unsigned long", {"<= 10"}},
{"unsigned long long", {"<= 10"}},
{"float", {">= -10", "<= 10"}},
{"double", {">= -10", "<= 10"}},
{"long double", {">= -10", "<= 10"}},
{"void", {"<= 10"}},
};
return constraints;
}
types::TypeKind types::TypesHandler::getTypeKind(const Type &type) const {
if (isPrimitiveType(type)) {
return TypeKind::PRIMITIVE;
}
if (isObjectPointerType(type)) {
return TypeKind::OBJECT_POINTER;
}
if (isArrayType(type)) {
return TypeKind::ARRAY;
}
if (isStructLike(type)) {
return TypeKind::STRUCT_LIKE;
}
if (isEnum(type)) {
return TypeKind::ENUM;
}
if (isPointerToFunction(type)) {
return TypeKind::FUNCTION_POINTER;
}
return TypeKind::UNKNOWN;
}
std::string types::TypesHandler::getDefaultValueForType(const types::Type &type,
utbot::Language language) const {
if (isIntegerType(type)) {
return "0";
}
if (isBoolType(type)) {
if (language == utbot::Language::CXX) {
return "false";
} else {
return "0";
}
}
if (isFloatingPointType(type)) {
return ".0";
}
if (isVoid(type)) {
return "";
}
if (isCharacterType(type)) {
return "'\\0'";
}
if (isArrayType(type)) {
return "{}";
}
if (isCStringType(type)) {
return "\"\"";
}
if (isObjectPointerType(type)) {
return PrinterUtils::C_NULL;
}
TypeName name = type.typeName();
std::string cDefaultValue = StringUtils::stringFormat("(%s){0}", name);
std::string cppDefaultValue = "{}";
switch (language) {
case utbot::Language::C: {
LOG_S(WARNING) << "Couldn't determine kind of type while generating default value. Using "
"\"(%s){0}\" instead.";
return cDefaultValue;
}
case utbot::Language::CXX: {
LOG_S(WARNING) << "Couldn't determine kind of type while generating default value. Using "
"\"{}\" instead.";
return cppDefaultValue;
}
default: {
LOG_S(WARNING) << "Unknown language for getDefaultValueForType. Using ifdef macro";
return StringUtils::stringFormat("\n#ifdef __cplusplus\n %s\n #else\n %s\n #endif\n",
cppDefaultValue, cDefaultValue);
}
}
}
std::string types::TypesHandler::cBoolToCpp(const types::TypeName &type) {
return isBoolType(type) ? "bool" : type;
}
types::TypeSupport
types::TypesHandler::isSupportedType(const Type &type, TypeUsage usage, int depth) const {
auto hashArgument = IsSupportedTypeArguments(type.typeName(), usage);
auto writtenValue = isSupportedTypeHash.find(hashArgument);
if (writtenValue != isSupportedTypeHash.end()) {
return writtenValue->second;
}
recursiveCheckStarted.insert(type.typeName());
using PredicateWithReason = std::pair<std::string, std::function<bool(Type, TypeUsage)>>;
std::vector<PredicateWithReason> unsupportedPredicates = {
{
"Type is unknown",
[&](const Type &type, TypeUsage usage) {
return getTypeKind(type) == TypeKind::UNKNOWN;
}
},
{
"Type has flexible array member",
[&](const Type &type, TypeUsage usage) {
if (isStructLike(type)) {
auto structInfo = getStructInfo(type);
if (structInfo.fields.empty()) {
return false;
}
return isIncompleteArrayType(structInfo.fields.back().type);
}
return false;
} },
{
"Dimension of pointer is too big",
[&](const Type &type, TypeUsage usage) {
if (usage == TypeUsage::RETURN) {
return false;
}
size_t counter = 0;
for (const auto& kind: type.kinds()) {
counter += kind->getKind() == AbstractType::OBJECT_POINTER ? 1: 0;
}
return counter > 2;
}
},
{ "Two dimensional pointer has extra const qualifiers",
[&](const Type &type, TypeUsage usage) {
if (usage == TypeUsage::RETURN) {
return false;
}
if (type.isPointerToPointer()) {
if (auto firstPointer =
dynamic_cast<ObjectPointerType *>(type.kinds()[0].get())) {
if (firstPointer->isConstQualified()) {
return true;
}
if (auto secondPointer =
dynamic_cast<ObjectPointerType *>(type.kinds()[1].get())) {
if (secondPointer->isConstQualified()) {
return true;
}
}
}
}
return false;
} },
{
"Unsupported types in structs/unions",
[&](const Type &type, TypeUsage usage) {
auto unsupportedFields = [&](const std::vector<types::Field> &fields) {
return std::any_of(fields.begin(), fields.end(), [&](const types::Field &field) {
if (!CollectionUtils::contains(recursiveCheckStarted, field.type.typeName())) {
if (field.type.isObjectPointer()) {
return false;
}
auto support = isSupportedType(field.type, usage, depth + 1);
bool fieldSupported = support.isSupported;
return !fieldSupported;
}
return false;
});
};
if (isStructLike(type)) {
auto structInfo = getStructInfo(type);
return unsupportedFields(structInfo.fields);
}
return false;
}
},
{ "Base type of array or pointer",
[&](const Type &type, TypeUsage usage) {
if (type.isArray() || type.isObjectPointer()) {
if (type.isObjectPointer() && depth > 0) {
return false;
}
auto support = isSupportedType(type.baseTypeObj(), usage, depth + 1);
bool supported = support.isSupported;
return !supported;
}
return false;
} }
};
for (const auto &[reason, predicate]: unsupportedPredicates) {
if (predicate(type, usage)) {
recursiveCheckStarted.erase(type.typeName());
return {false, reason};
}
}
recursiveCheckStarted.erase(type.typeName());
types::TypeSupport result = {true, ""};
isSupportedTypeHash[IsSupportedTypeArguments(hashArgument)] = result;
return result;
}
types::TypesHandler::IsSupportedTypeArguments::IsSupportedTypeArguments(types::TypeName typeName,
types::TypeUsage usage)
: typeName(std::move(typeName)), usage(usage) {
}
bool types::TypesHandler::IsSupportedTypeArguments::operator==(const types::TypesHandler::IsSupportedTypeArguments &other) const {
return typeName == other.typeName && usage == other.usage;
}
std::size_t types::TypesHandler::IsSupportedTypeArgumentsHash::operator()(const types::TypesHandler::IsSupportedTypeArguments &args) const {
return fs::hash_value(args.typeName + std::to_string((int)args.usage));
}
types::Type types::TypesHandler::getReturnTypeToCheck(const types::Type &returnType) const {
types::Type baseReturnType = returnType.baseTypeObj();
if (types::TypesHandler::isObjectPointerType(returnType)) {
if (types::TypesHandler::skipTypeInReturn(baseReturnType)) {
return types::Type::minimalScalarType();
}
return baseReturnType;
}
return returnType;
}
std::string types::EnumInfo::getEntryName(const std::string &value, utbot::Language language) const {
const EnumEntry &entry = valuesToEntries.at(value);
if (language == utbot::Language::CXX && access.has_value()) {
return access.value() + "::" + entry.name;
}
return entry.name;
}