-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass_gen_vfh.cpp
901 lines (759 loc) · 33.1 KB
/
class_gen_vfh.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
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
#include "kslicer.h"
#include "class_gen.h"
#include "ast_matchers.h"
#include "extractor.h"
#include "initial_pass.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/Parse/ParseAST.h"
#include <sstream>
#include <algorithm>
/**
\brief processing of C++ member function for virtual functions
*/
class MemberRewriter : public kslicer::GLSLFunctionRewriter
{
public:
MemberRewriter(clang::Rewriter &R, const clang::CompilerInstance& a_compiler, kslicer::MainClassInfo* a_codeInfo, kslicer::MainClassInfo::DImplClass& dImpl, int a_level) :
kslicer::GLSLFunctionRewriter(R, a_compiler, a_codeInfo, kslicer::ShittyFunction()),
m_processed(dImpl.memberFunctions), m_fields(dImpl.fields), m_className(dImpl.name), m_objBufferName(dImpl.objBufferName), m_interfaceName(dImpl.interfaceName),
m_mainClassName(a_codeInfo->mainClassName), dataClassNames(a_codeInfo->dataClassNames), m_vfhLevel(a_level)
{
}
bool VisitMemberExpr_Impl(clang::MemberExpr* expr) override
{
clang::ValueDecl* pValueDecl = expr->getMemberDecl();
if(!clang::isa<clang::FieldDecl>(pValueDecl))
return true;
clang::FieldDecl* pFieldDecl = clang::dyn_cast<clang::FieldDecl>(pValueDecl);
clang::RecordDecl* pRecodDecl = pFieldDecl->getParent();
const std::string thisTypeName = pRecodDecl->getNameAsString();
const std::string debugText = kslicer::GetRangeSourceCode(expr->getSourceRange(), m_compiler);
const std::string fieldName = pFieldDecl->getNameAsString();
const clang::QualType qt = pFieldDecl->getType();
const std::string fieldTypeName = qt.getAsString();
const auto typeDecl = qt->getAsRecordDecl();
const bool isContainer = (typeDecl != nullptr) && clang::isa<clang::ClassTemplateSpecializationDecl>(typeDecl);
if((thisTypeName == m_className || thisTypeName == m_interfaceName) && WasNotRewrittenYet(expr))
{
const clang::Expr* baseExpr = expr->getBase();
std::string exprContent = RecursiveRewrite(baseExpr);
std::string exprReplaced = m_objBufferName + "[selfId]." + exprContent;
if(m_vfhLevel >= 2)
exprReplaced = "all_references." + m_className + "_buffer." + exprReplaced;
//ReplaceTextOrWorkAround(expr->getSourceRange(), exprReplaced);
m_rewriter.ReplaceText(expr->getSourceRange(), exprReplaced);
MarkRewritten(expr);
}
else if(dataClassNames.find(thisTypeName) != dataClassNames.end() && WasNotRewrittenYet(expr))
{
std::string prefix = "ubo.";
if(isContainer)
{
prefix = "";
auto pComposPrefix = m_codeInfo->composPrefix.find(thisTypeName);
if(pComposPrefix != m_codeInfo->composPrefix.end())
prefix = pComposPrefix->second + "_";
}
//std::cout << " [MemberRewriter]: fieldName = " << fieldName.c_str() << std::endl;
const std::string filedNameToFind = (isContainer && prefix != "") ? prefix + fieldName : fieldName;
auto p = m_codeInfo->allDataMembers.find(filedNameToFind);
if(p != m_codeInfo->allDataMembers.end())
{
kslicer::UsedContainerInfo container;
container.name = p->first;
container.type = p->second.type;
container.kind = p->second.kind;
container.isConst = qt.isConstQualified();
if(isContainer)
{
kslicer::ProbablyUsed pcontainer;
pcontainer.astNode = pFieldDecl;
pcontainer.isContainer = true;
pcontainer.info = container;
pcontainer.interfaceName = m_interfaceName;
pcontainer.className = m_className;
pcontainer.objBufferName = m_objBufferName;
auto specDecl = clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(typeDecl);
kslicer::SplitContainerTypes(specDecl, pcontainer.containerType, pcontainer.containerDataType);
m_codeInfo->usedProbably[container.name] = pcontainer;
}
else
{
kslicer::ProbablyUsed pvar;
pvar.astNode = pFieldDecl;
pvar.isContainer = false;
pvar.info = container;
pvar.interfaceName = m_interfaceName;
pvar.className = m_className;
pvar.objBufferName = m_objBufferName;
m_codeInfo->usedProbably[container.name] = pvar;
p->second.usedInKernel = true;
}
}
//ReplaceTextOrWorkAround(expr->getSourceRange(), prefix + fieldName);
m_rewriter.ReplaceText(expr->getSourceRange(), prefix + fieldName);
MarkRewritten(expr);
}
else if(expr->isArrow() && WasNotRewrittenYet(expr))
{
// a->b ==> a.b
clang::Expr* base = expr->getBase(); // Получаем указатель на ноду, соответствующую a
auto memberNameInfo = expr->getMemberNameInfo();
auto memberName = memberNameInfo.getName().getAsString();
//std::cout << " [MemberRewriter]: process with '.' for " << thisTypeName.c_str() << "::" << fieldName.c_str() << std::endl;
//ReplaceTextOrWorkAround(expr->getSourceRange(), kslicer::GetRangeSourceCode(base->getSourceRange(), m_compiler) + "." + memberName);
m_rewriter.ReplaceText(expr->getSourceRange(), kslicer::GetRangeSourceCode(base->getSourceRange(), m_compiler) + "." + memberName);
MarkRewritten(expr);
}
return true;
}
bool VisitUnaryOperator_Impl(clang::UnaryOperator* expr) override
{
const auto op = expr->getOpcodeStr(expr->getOpcode());
clang::Expr* subExpr = expr->getSubExpr();
if(subExpr == nullptr)
return true;
if((op == "*" || op == "&") && WasNotRewrittenYet(expr->getSubExpr()) )
{
std::string text = RecursiveRewrite(expr->getSubExpr());
ReplaceTextOrWorkAround(expr->getSourceRange(), text);
MarkRewritten(expr->getSubExpr());
}
return true;
}
/// \return whether \p Ty points to a const type, or is a const reference.
//
static bool isPointerToConst(clang::QualType Ty)
{
return !Ty->getPointeeType().isNull() && Ty->getPointeeType().getCanonicalType().isConstQualified();
}
using ArgList = std::vector< std::pair<std::string, std::string> >;
std::string RewriteMemberDecl(clang::CXXMethodDecl* fDecl, const std::string& classTypeName, ArgList* pList = nullptr)
{
std::string fname = fDecl->getNameInfo().getName().getAsString();
std::string result = m_codeInfo->pShaderFuncRewriter->RewriteStdVectorTypeStr(fDecl->getReturnType().getAsString()) + " " + classTypeName + "_" + fname + "_" + m_objBufferName + "(uint selfId" ;
if(fDecl->getNumParams() != 0)
result += ", ";
bool isKernel = m_codeInfo->IsKernel(fname);
for(uint32_t i=0; i < fDecl->getNumParams(); i++)
{
const clang::ParmVarDecl* pParam = fDecl->getParamDecl(i);
const clang::QualType typeOfParam = pParam->getType();
const clang::IdentifierInfo* identifier = pParam->getIdentifier();
if(identifier == nullptr)
continue;
std::string typeNameRewritten = m_codeInfo->pShaderFuncRewriter->RewriteStdVectorTypeStr(typeOfParam.getAsString());
if(dataClassNames.find(typeNameRewritten) != dataClassNames.end())
{
if(i==fDecl->getNumParams()-1)
result[result.rfind(",")] = ' ';
continue;
}
if(typeOfParam->isPointerType() && !typeOfParam->getPointeeType().isConstQualified())
typeNameRewritten = std::string("inout ") + typeNameRewritten;
result += typeNameRewritten + " " + std::string(identifier->getName());
if(pList != nullptr)
pList->push_back(std::make_pair(typeNameRewritten, std::string(identifier->getName())));
if(i!=fDecl->getNumParams()-1)
result += ", ";
}
return result + ") ";
}
bool VisitCXXMethodDecl_Impl(clang::CXXMethodDecl* fDecl) override
{
if(isCopy)
return true;
std::string fname = fDecl->getNameInfo().getName().getAsString();
auto thisType = fDecl->getThisType();
auto qtOfClass = thisType->getPointeeType();
std::string classTypeName = kslicer::CutOffStructClass(qtOfClass.getAsString());
if(classTypeName.find(fname) != std::string::npos || classTypeName.find(fname.substr(1)) != std::string::npos || fname == "GetTag" || fname == "GetSizeOf")
return true; // exclude constructor, destructor and special functions
if(WasNotRewrittenYet(fDecl->getBody()))
{
std::string declSource = RewriteMemberDecl(fDecl, classTypeName);
std::string bodySource = RecursiveRewrite(fDecl->getBody());
auto p = declByName.find(fname);
if(p == declByName.end()) {
ArgList args;
declByName[fname] = RewriteMemberDecl(fDecl, m_interfaceName, &args); // make text decls for IMaterial_sample_materials(...)
argsByName[fname] = args;
}
auto beginPos = declSource.find(" ");
auto endPos = declSource.find("(");
kslicer::MainClassInfo::DImplFunc funcData;
funcData.decl = fDecl;
funcData.name = fname;
funcData.nameRewritten = declSource.substr(beginPos+1, endPos - beginPos - 1);
funcData.srcRewritten = declSource + bodySource;
funcData.isEmpty = false;
funcData.isConstMember = fDecl->isConst();
//funcData.mainClassPass = mainClassDataPass;
if(clang::isa<clang::CompoundStmt>(fDecl->getBody()))
{
clang::CompoundStmt* pBody = clang::dyn_cast<clang::CompoundStmt>(fDecl->getBody());
funcData.isEmpty = pBody->body_empty();
}
m_processed.push_back(funcData);
MarkRewritten(fDecl->getBody());
}
return true;
}
bool VisitCXXMemberCallExpr_Impl(clang::CXXMemberCallExpr* call) override
{
const clang::FunctionDecl* fDecl = call->getDirectCallee();
const std::string fname = fDecl->getNameInfo().getName().getAsString();
const clang::QualType qt = call->getObjectType();
std::string classTypeName = kslicer::CutOffStructClass(qt.getAsString());
if(WasNotRewrittenYet(call))
{
std::string textRes = classTypeName + "_" + fname + "_" + m_objBufferName + "(selfId";
{
const auto pPrefix = m_codeInfo->composPrefix.find(classTypeName);
if(pPrefix != m_codeInfo->composPrefix.end())
textRes = pPrefix->second + "_" + fname + "(";
else if(call->getNumArgs() > 0)
textRes += ",";
}
for(unsigned i=0;i<call->getNumArgs();i++)
{
const auto pParam = call->getArg(i);
const clang::QualType typeOfParam = pParam->getType();
const std::string typeNameRewritten = typeOfParam.getAsString();
if(dataClassNames.find(typeNameRewritten) != dataClassNames.end())
{
if(i==call->getNumArgs()-1)
textRes[textRes.rfind(",")] = ' ';
continue;
}
textRes += RecursiveRewrite(call->getArg(i));
if(i < call->getNumArgs()-1)
textRes += ",";
}
textRes += ")";
ReplaceTextOrWorkAround(call->getSourceRange(), textRes);
MarkRewritten(call);
}
return true;
}
bool VisitFieldDecl_Impl(clang::FieldDecl* pFieldDecl) override
{
clang::RecordDecl* pRecodDecl = pFieldDecl->getParent();
const std::string thisTypeName = pRecodDecl->getNameAsString();
if(thisTypeName == m_className)
m_fields.push_back(kslicer::GetRangeSourceCode(pFieldDecl->getSourceRange(), m_compiler));
return true;
}
std::unordered_map<std::string, ArgList> argsByName;
std::unordered_map<std::string, std::string> declByName;
const std::unordered_set<std::string>& dataClassNames;
private:
std::vector<kslicer::MainClassInfo::DImplFunc>& m_processed;
std::vector<std::string>& m_fields;
const std::string& m_className;
const std::string& m_mainClassName;
const std::string& m_objBufferName;
const std::string& m_interfaceName;
bool isCopy = false;
int m_vfhLevel = 0;
///////////////////////////////////////////////////////////////////////////////////////////////////
inline void MarkRewritten(const clang::Stmt* expr) { FunctionRewriter::MarkRewritten(expr); }
inline bool WasNotRewrittenYet(const clang::Stmt* expr) { return FunctionRewriter::WasNotRewrittenYet(expr); }
std::string RecursiveRewrite(const clang::Stmt* expr) override
{
if(expr == nullptr)
return "";
MemberRewriter rvCopy = *this;
rvCopy.isCopy = true;
rvCopy.TraverseStmt(const_cast<clang::Stmt*>(expr));
std::string text = m_rewriter.getRewrittenText(expr->getSourceRange());
return (text != "") ? text : kslicer::GetRangeSourceCode(expr->getSourceRange(), m_compiler);
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void kslicer::MainClassInfo::ProcessVFH(const std::vector<const clang::CXXRecordDecl*>& a_decls, const clang::CompilerInstance& a_compiler)
{
//
//
for(auto& p : m_vhierarchy)
{
const clang::CXXRecordDecl* pBaseClass = nullptr;
std::string className = kslicer::CutOffStructClass(p.first);
// find target base class
//
for(const auto& decl : a_decls)
{
const std::string testName = decl->getNameAsString();
if(testName == className)
{
pBaseClass = decl;
break;
}
//std::cout << " found class: " << testName.c_str() << std::endl;
}
if(pBaseClass == nullptr)
return;
p.second.interfaceDecl = pBaseClass;
// find all derived classes for target base class
//
std::unordered_map<std::string, std::string> declByName;
std::unordered_map<std::string, MemberRewriter::ArgList> argsByName;
clang::Rewriter rewrite2;
rewrite2.setSourceMgr(a_compiler.getSourceManager(), a_compiler.getLangOpts());
for(const auto& decl : a_decls)
{
if(decl->isDerivedFrom(pBaseClass))
{
DImplClass dImpl;
dImpl.decl = decl;
dImpl.name = decl->getNameAsString();
dImpl.objBufferName = p.second.objBufferName;
dImpl.interfaceName = p.second.interfaceName;
if(intersectionBlackList.find(dImpl.name) != intersectionBlackList.end())
continue;
else if(!intersectionWhiteList.empty())
{
if(intersectionWhiteList.find(dImpl.name) == intersectionWhiteList.end())
continue;
}
MemberRewriter rv(rewrite2, a_compiler, this, dImpl, int(p.second.level));
rv.TraverseDecl(const_cast<clang::CXXRecordDecl*>(dImpl.decl));
for (const auto& kv : rv.declByName)
declByName[kv.first] = kv.second;
for (const auto& kv : rv.argsByName)
argsByName[kv.first] = kv.second;
dImpl.isEmpty = (dImpl.name.find("Empty") != std::string::npos);
for(auto& k : kernels)
{
if(k.second.className == dImpl.name)
k.second.interfaceName = className;
}
bool alreadyHasSuchImpl = false;
for(const auto& impl : p.second.implementations) {
if(impl.name == dImpl.name) {
alreadyHasSuchImpl = true;
break;
}
}
if(!alreadyHasSuchImpl)
p.second.implementations.push_back(dImpl);
}
}
for(auto& f : p.second.virtualFunctions) {
auto pFound = declByName.find(f.second.name);
if(pFound != declByName.end())
f.second.declRewritten = pFound->second;
auto pFound2 = argsByName.find(f.second.name);
if(pFound2 != argsByName.end())
f.second.args = pFound2->second;
}
}
// debug output
//
for(const auto& p : m_vhierarchy)
{
for(const auto& impl : p.second.implementations)
std::cout << " found " << p.first.c_str() << " --> " << impl.name.c_str() << std::endl;
}
}
class TagSeeker : public clang::RecursiveASTVisitor<TagSeeker>
{
public:
TagSeeker(const clang::CompilerInstance& a_compiler, std::vector<kslicer::DeclInClass>& a_constants, std::unordered_map<std::string, kslicer::MainClassInfo::VFHTagInfo>& a_tagByName) :
m_compiler(a_compiler), m_sm(a_compiler.getSourceManager()), m_knownConstants(a_constants), m_tagByClassName(a_tagByName) { m_tagByClassName.clear(); }
bool VisitCXXMethodDecl(const clang::CXXMethodDecl* f)
{
if(!f->hasBody())
return true;
// Get name of function
const std::string fname = f->getNameInfo().getName().getAsString();
if(fname == "GetTag" || fname == "GetTypeId")
{
const clang::QualType qThisType = f->getThisType();
const clang::QualType classType = qThisType->getPointeeType();
const std::string thisTypeName = kslicer::CutOffStructClass(classType.getAsString());
auto funcBody = f->getBody();
if(clang::isa<clang::CompoundStmt>(funcBody))
{
clang::CompoundStmt* s2 = clang::dyn_cast<clang::CompoundStmt>(funcBody);
for(auto iter = s2->body_begin(); iter != s2->body_end(); ++iter)
{
if(clang::isa<clang::ReturnStmt>(*iter))
{
funcBody = *iter;
break;
}
}
}
if(!clang::isa<clang::ReturnStmt>(funcBody))
{
std::cout << " [TagSeeker::Error]: " << "Can't find returt statement in 'GetTag/GetTypeId' fuction body for '" << thisTypeName.c_str() << "' class." << std::endl;
return true;
}
clang::ReturnStmt* retStmt = clang::dyn_cast< clang::ReturnStmt>(funcBody);
clang::Expr* retVal = retStmt->getRetValue();
const std::string tagName = kslicer::GetRangeSourceCode(retVal->getSourceRange(), m_compiler);
for(const auto& decl : m_knownConstants)
{
//auto pos = funcBody.find(decl.name);
if(decl.name == tagName)
{
kslicer::MainClassInfo::VFHTagInfo tagInfo;
tagInfo.name = decl.name;
tagInfo.id = decl.constVal;
m_tagByClassName[thisTypeName] = tagInfo;
break;
}
}
}
return true;
}
private:
const clang::CompilerInstance& m_compiler;
const clang::SourceManager& m_sm;
std::vector<kslicer::DeclInClass>& m_knownConstants;
std::unordered_map<std::string, kslicer::MainClassInfo::VFHTagInfo>& m_tagByClassName;
};
void kslicer::MainClassInfo::ExtractVFHConstants(const clang::CompilerInstance& compiler, clang::tooling::ClangTool& Tool)
{
for(auto& p : m_vhierarchy)
{
//// (1) get all constants inside interface
//
std::cout << " process " << p.second.interfaceName.c_str() << std::endl;
p.second.usedDecls = kslicer::ExtractTCFromClass(p.second.interfaceName, p.second.interfaceDecl, compiler, Tool);
//// (2) juxtapose constant TAG and class implementation by analyzing GetTag() function
//
TagSeeker visitor(compiler, p.second.usedDecls, p.second.tagByClassName);
for(auto impl : p.second.implementations)
visitor.TraverseDecl(const_cast<clang::CXXRecordDecl*>(impl.decl));
}
// fill tag name and id for each implementation
//
for(auto& p : m_vhierarchy) {
for(auto& impl : p.second.implementations) {
auto pTagInfo = p.second.tagByClassName.find(impl.name);
if(pTagInfo != p.second.tagByClassName.end()) {
impl.tagName = pTagInfo->second.name;
impl.tagId = pTagInfo->second.id;
}
}
std::sort(p.second.implementations.begin(), p.second.implementations.end(), [](auto& a, auto& b){ return a.tagId < b.tagId; });
}
// debug output
//
std::cout << " reordered vfh: " << std::endl;
for(const auto& p : m_vhierarchy)
{
for(const auto& impl : p.second.implementations)
std::cout << " " << p.first.c_str() << " --> " << impl.name.c_str() << "; tag(" << impl.tagName.c_str() << ") = " << impl.tagId << std::endl;
}
}
std::vector< std::pair<std::string, std::string> > kslicer::MainClassInfo::GetFieldsFromStruct(const clang::CXXRecordDecl* recordDecl, size_t* pSummOfFiledsSize) const
{
std::vector< std::pair<std::string, std::string> > fieldInfo;
size_t summOfSize = 0;
const auto& context = recordDecl->getASTContext();
// Проходим по всем полям структуры
for (auto it = recordDecl->field_begin(); it != recordDecl->field_end(); ++it)
{
const clang::FieldDecl* field = *it;
const clang::QualType fieldType = field->getType();
const clang::Type* baseType = fieldType.getTypePtrOrNull();
// Получаем имена типов и полей и добавляем их в массив пар
if (baseType)
{
std::string typeName = baseType->getCanonicalTypeInternal().getAsString();
std::string fieldName = field->getNameAsString();
std::string typeNameR = pShaderFuncRewriter->RewriteStdVectorTypeStr(typeName, fieldName);
fieldInfo.push_back(std::make_pair(typeNameR, fieldName));
const clang::Type* fieldType = field->getType().getTypePtr();
const clang::TypeInfo& typeInfo = context.getTypeInfo(fieldType);
const uint64_t sizeInBits = typeInfo.Width;
const uint64_t sizeInBytes = llvm::divideCeil(sizeInBits, 8);
summOfSize += size_t(sizeInBytes);
}
}
if(pSummOfFiledsSize != nullptr)
(*pSummOfFiledsSize) = summOfSize;
return fieldInfo;
}
bool kslicer::IsCalledWithArrowAndVirtual(const clang::CXXMemberCallExpr* f)
{
if (!f) return false;
// Получаем выражение, на котором вызывается метод
const auto* calleeExpr = f->getCallee();
if (!calleeExpr) return false;
// Проверяем, является ли выражение MemberExpr
const clang::MemberExpr* memberExpr = clang::dyn_cast<clang::MemberExpr>(calleeExpr);
if (!memberExpr) return false;
// Получаем выражение объекта
const clang::Expr* baseExpr = memberExpr->getBase();
if (!baseExpr) return false;
// Проверяем тип базового выражения
if (const auto* baseType = baseExpr->getType()->getPointeeType().getTypePtrOrNull()) {
if (baseType->isRecordType()) {
// Использован оператор "->"
const clang::CXXMethodDecl* methodDecl = clang::dyn_cast<clang::CXXMethodDecl>(memberExpr->getMemberDecl());
if (methodDecl && methodDecl->isVirtual()) {
// Метод является виртуальным
return true;
}
}
}
// Если условие не выполнено, возвращаем false
return false;
}
kslicer::VFHAccessNodes kslicer::GetVFHAccessNodes(const clang::CXXMemberCallExpr* f, const clang::CompilerInstance& a_compiler)
{
VFHAccessNodes result = {};
if (!f) return result;
// Get the MemberExpr for the method call
const clang::MemberExpr* memberExpr = clang::dyn_cast<clang::MemberExpr>(f->getCallee());
if (!memberExpr) return result;
// Get the base of the MemberExpr
const clang::Expr* baseExpr = memberExpr->getBase();
if (!baseExpr) return result;
// Expecting baseExpr to be an ImplicitCastExpr
const clang::ImplicitCastExpr* castExpr = clang::dyn_cast<clang::ImplicitCastExpr>(baseExpr);
if (!castExpr) return result;
// Get the subexpression of the cast
const clang::Expr* subExpr = kslicer::RemoveImplicitCast(castExpr->getSubExpr());
if (!subExpr)
return result;
// Проверка на первый тип/уровень вызова, соотвествующий VFH_LEVEL_1: "(m_materials.data() + matId)->GetColor()"
//
if (const clang::ParenExpr* parenExpr = clang::dyn_cast<clang::ParenExpr>(subExpr))
{
// Get the subexpression of the paren
const clang::Expr* innerExpr = parenExpr->getSubExpr();
if (!innerExpr) return result;
// Expecting innerExpr to be a BinaryOperator
const clang::BinaryOperator* binOp = clang::dyn_cast<clang::BinaryOperator>(innerExpr);
if (!binOp || binOp->getOpcode() != clang::BO_Add) return result;
// Get the left and right hand sides of the binary operator
const clang::Expr* lhs = binOp->getLHS();
const clang::Expr* rhs = binOp->getRHS();
if (!lhs || !rhs)
return result;
std::string buffText = GetRangeSourceCode(lhs->getSourceRange(), a_compiler);
result.buffName = buffText.substr(0, buffText.find(".data()")); // The left hand side should be a CXXMemberCallExpr (m_materials.data())
result.offsetName = GetRangeSourceCode(rhs->getSourceRange(), a_compiler); // The right hand side should be a DeclRefExpr (mid)
}
// Проверка на второй тип/уровень вызова, соотвествующий VFH_LEVEL_2: "m_materials[matId]->GetColor()"
else if(const clang::CXXOperatorCallExpr* arrayExpr = clang::dyn_cast<clang::CXXOperatorCallExpr>(subExpr))
{
if(arrayExpr->getOperator() != clang::OO_Subscript) // operator[]
return result;
const auto arg0 = kslicer::RemoveImplicitCast(arrayExpr->getArg(0));
const auto arg1 = kslicer::RemoveImplicitCast(arrayExpr->getArg(1));
result.buffName = GetRangeSourceCode(arg0->getSourceRange(), a_compiler); // The left hand side should be a 'm_materials'
result.offsetName = GetRangeSourceCode(arg1->getSourceRange(), a_compiler); // The right hand side should be a 'mid'
}
const clang::Expr* baseCallExpr = f->getImplicitObjectArgument();
if (baseCallExpr) {
clang::QualType baseType = baseCallExpr->getType();
if (const clang::CXXRecordDecl* recordDecl = baseType->getPointeeCXXRecordDecl())
result.interfaceName = recordDecl->getNameAsString();
result.interfaceTypeName = kslicer::ClearTypeName(baseType.getAsString());
ReplaceFirst(result.interfaceTypeName, "*", "");
while(ReplaceFirst(result.interfaceTypeName, " ", ""));
}
return result;
}
bool kslicer::MainClassInfo::IsVFHBuffer(const std::string& a_name, VFH_LEVEL* pOutLevel, VFHHierarchy* pHierarchy) const
{
bool isVFHBuffer = false;
for(const auto& vfh : this->m_vhierarchy) {
if(vfh.second.objBufferName == a_name) {
isVFHBuffer = true;
if(pOutLevel != nullptr)
(*pOutLevel) = vfh.second.level;
if(pHierarchy != nullptr)
(*pHierarchy) = vfh.second;
break;
}
}
return isVFHBuffer;
}
void kslicer::MainClassInfo::AppendAllRefsBufferIfNeeded(std::vector<DataMemberInfo>& a_vector)
{
bool exitFromThisFunction = true;
bool usedWithVBR = false; // used with vector buffer reference
for(const auto& h : m_vhierarchy)
if(int(h.second.level) >= 2)
exitFromThisFunction = false;
for(const auto& v : dataMembers)
{
auto pFound = allDataMembers.find(v.name);
if(pFound != allDataMembers.end())
if(pFound->second.bindWithRef) {
exitFromThisFunction = false;
usedWithVBR = true;
break;
}
}
if(exitFromThisFunction)
return;
const std::string nameOfBuffer = "all_references";
auto pMember = std::find_if(a_vector.begin(), a_vector.end(), [&nameOfBuffer](const DataMemberInfo & m) { return m.name == nameOfBuffer; });
if(pMember == a_vector.end())
{
// add vector itself
//
DataMemberInfo memberVFHTable;
memberVFHTable.name = nameOfBuffer;
memberVFHTable.type = "std::vector<AllBufferReferences>";
memberVFHTable.containerDataType = "AllBufferReferences";
memberVFHTable.containerType = "std::vector";
memberVFHTable.isContainer = true;
memberVFHTable.isSingle = true;
memberVFHTable.kind = DATA_KIND::KIND_VECTOR;
memberVFHTable.bindWithRef = false;
a_vector.push_back(memberVFHTable);
// add vector size and capacity for this vector
//
kslicer::DataMemberInfo size;
size.type = "uint";
size.sizeInBytes = sizeof(unsigned int);
size.name = memberVFHTable.name + "_size";
size.usedInKernel = true;
size.isContainerInfo = true;
size.kind = kslicer::DATA_KIND::KIND_POD;
kslicer::DataMemberInfo capacity = size;
capacity.name = memberVFHTable.name + "_capacity";
a_vector.push_back(size);
a_vector.push_back(capacity);
pMember = std::find_if(a_vector.begin(), a_vector.end(), [&nameOfBuffer](const DataMemberInfo & m) { return m.name == nameOfBuffer; });
}
if(pMember == a_vector.end())
{
std::cout << "[AppendAllRefsBufferIfNeeded]: ERROR, can't find data member '" << nameOfBuffer.c_str() << "'" << std::endl;
return;
}
// add to kernel.usedContainers to bind it to shaders further (because we must readreferences from this buffer)
//
for(auto& k : this->kernels) // TODO: check if kernel actually needs this buffer in some way
{
bool usedWithAtLeastOneVFH = false;
for(const auto& h : m_vhierarchy)
{
auto p = k.second.usedContainers.find(h.second.objBufferName);
if(p != k.second.usedContainers.end() && int(h.second.level) >= 2) {
usedWithAtLeastOneVFH = true;
break;
}
}
if(usedWithAtLeastOneVFH || usedWithVBR)
{
kslicer::UsedContainerInfo info;
info.type = pMember->type;
info.name = pMember->name;
info.kind = pMember->kind;
info.isConst = true;
k.second.usedContainers[info.name] = info;
}
}
for(const auto& h : m_vhierarchy) {
if(int(h.second.level) >= 2) {
for(auto impl : h.second.implementations) {
if(impl.isEmpty)
continue;
BufferReference ref;
ref.name = impl.name;
ref.typeOfElem = impl.name;
this->m_allRefsFromVFH.push_back(ref);
}
}
}
}
void kslicer::MainClassInfo::AppendAccelStructForIntersectionShadersIfNeeded(std::vector<DataMemberInfo>& a_vector, std::string composImplName)
{
struct IntersectionShader
{
std::string interfaceName;
std::string functionName;
VFHHierarchy* hierarchy = nullptr;
DataMemberInfo memberInfo;
};
std::vector<IntersectionShader> shaders;
for(auto& h : m_vhierarchy) {
for(auto is : intersectionShaders) {
if(is.first == h.second.interfaceName) {
IntersectionShader entity;
entity.interfaceName = is.first;
entity.functionName = is.second;
entity.hierarchy = &h.second;
for(auto& impl : h.second.implementations) { // mark all intersection shaders
for(auto& func : impl.memberFunctions) {
if(func.name == entity.functionName) {
func.isIntersection = true;
h.second.hasIntersection = true;
break; // allow single intersection shader per implementation, i.e. in 'impl.memberFunctions'
}
}
}
shaders.push_back(entity);
}
}
}
if(shaders.size() == 0) // exit if we dont have intersection shaders indication and didn't found appropriate VFH
return;
for(auto& group : shaders)
{
std::string memberName = group.hierarchy->objBufferName;
for(auto prefix : composPrefix) {
auto pos = memberName.find(prefix.second);
if(pos != std::string::npos) {
memberName = prefix.second;
group.hierarchy->accStructName = prefix.second;
break;
}
}
for(auto& member : a_vector) {
if(member.kind == DATA_KIND::KIND_ACCEL_STRUCT && member.name == memberName) {
member.hasIntersectionShader = true;
member.intersectionClassName = composImplName;
group.memberInfo = member;
}
}
}
// add to kernel.usedContainers to bind it to shaders further (because we must readreferences from this buffer)
//
for(auto& k : this->kernels)
{
for(const auto group : shaders)
{
auto& h = *group.hierarchy;
auto p = k.second.usedContainers.find(h.objBufferName);
if(p != k.second.usedContainers.end() && int(h.level) >= 2)
{
kslicer::UsedContainerInfo info;
info.type = "std::shared_ptr<struct ISceneObject>";
info.name = group.memberInfo.name;
info.kind = DATA_KIND::KIND_ACCEL_STRUCT;
info.isConst = true;
k.second.usedContainers[info.name] = info;
}
}
}
}
std::unordered_map<std::string, kslicer::MainClassInfo::VFHHierarchy> kslicer::MainClassInfo::SelectVFHOnlyUsedByKernel(const std::unordered_map<std::string, VFHHierarchy>& a_hierarhices, const KernelInfo& k) const
{
auto copy = a_hierarhices;
{
copy.clear();
for(const auto& h : a_hierarhices) {
auto p = k.usedContainers.find(h.second.objBufferName);
if(p != k.usedContainers.end())
copy.insert(h);
}
}
return copy;
}