[r302]: cmajor++ / Cm / Cm.Core / ClassFun.cpp Maximize Restore History

Download this file

ClassFun.cpp    977 lines (939 with data), 43.0 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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*========================================================================
Copyright (c) 2012-2013 Seppo Laakko
http://sourceforge.net/projects/cmajor/
Distributed under the GNU General Public License, version 3 (GPLv3).
(See accompanying LICENSE.txt or http://www.gnu.org/licenses/gpl.html)
========================================================================*/
#include <Cm.Core/ClassFun.hpp>
#include <Cm.Core/Class.hpp>
#include <Cm.Core/Statement.hpp>
#include <Cm.Core/Assert.hpp>
#include <Cm.Core/Util.hpp>
#include <Cm.Core/Error.hpp>
#include <Cm.Core/Pointer.hpp>
#include <Cm.Core/Gen.hpp>
#include <Llvm.Ir/RegVar.hpp>
#include <Llvm.Ir/MemberVar.hpp>
#include <Llvm.Ir/Util.hpp>
namespace Cm { namespace Core {
void SetVtblPtr(Ir& ir, ClassTypePtr classType, bool useThisVariable)
{
if (!classType->IsVirtual())
{
return;
}
Llvm::Ir::TypePtr i8PtrPtr(Llvm::Ir::Pointer(Llvm::Ir::Pointer(Llvm::Ir::I8())));
Llvm::Ir::ObjectPtr thisPtr;
if (useThisVariable)
{
LocalVariablePtr thisVar(new ThisVariable(classType->Pos(), classType->EnclosingScope(), classType->FileScope(), classType));
TypeCheckContext body(TypeCheckExtent::body);
thisVar->TypeCheck(body);
GenResult thisResult = thisVar->Gen(ir, GenFlags::none);
thisPtr = thisResult.MainObject();
}
else
{
thisPtr = Llvm::Ir::ObjectPtr(new Llvm::Ir::Parameter("this", Llvm::Ir::Pointer(classType->IrType())));
}
int vtblPtrIndex = classType->VirtualFunctionTablePtrIndex();
if (vtblPtrIndex == -1)
{
ClassTypePtr vtblContainingType = classType->VtblPtrContainerClass();
vtblPtrIndex = vtblContainingType->VirtualFunctionTablePtrIndex();
Llvm::Ir::RegVarPtr containerPtr = Llvm::Ir::CreateTemporaryRegVar(Llvm::Ir::Pointer(vtblContainingType->IrType()));
ir.Emit(Llvm::Ir::Bitcast(Llvm::Ir::Pointer(classType->IrType()), containerPtr, thisPtr, Llvm::Ir::Pointer(vtblContainingType->IrType())));
thisPtr = containerPtr;
}
Llvm::Ir::MemberVarPtr vtblPtr(new Llvm::Ir::MemberVar("$vtblptr", thisPtr, vtblPtrIndex, i8PtrPtr));
Llvm::Ir::RegVarPtr vtblAsI8PtrPtr = Llvm::Ir::CreateTemporaryRegVar(i8PtrPtr);
Llvm::Ir::ObjectPtr vtbl = classType->GetVtblIrObject();
ir.Emit(Llvm::Ir::Bitcast(vtbl->GetType(), vtblAsI8PtrPtr, vtbl, i8PtrPtr));
Llvm::Ir::Assign(ir, i8PtrPtr, vtblAsI8PtrPtr, vtblPtr);
}
class SetVtblPtrStatement: public Statement
{
public:
SetVtblPtrStatement(const ClassTypePtr& classType_, bool useThisVariable_):
Statement("setVtblPtr", classType_->Pos(), classType_->GetScope(), classType_->FileScope(), ""), classType(classType_), useThisVariable(useThisVariable_)
{
}
virtual std::string ToString() const
{
return "set vtbl ptr statement";
}
virtual ObjectPtr Clone(CloneContext& context) const { CM_CORE_ASSERT(false); return ObjectPtr(); }
virtual GenResult Gen(Ir& ir, GenFlags flags)
{
GenResult result(ir, flags);
if (classType.lock()->IsVirtual())
{
ir.SetNextInstructionComment("set vtbl ptr for " + classType.lock()->FullName());
SetVtblPtr(ir, classType.lock(), useThisVariable);
}
return result;
}
private:
WeakClassTypePtr classType;
bool useThisVariable;
};
class ClassDefaultConstructionStatement: public Statement
{
public:
ClassDefaultConstructionStatement(const ClassTypePtr& classType_, Function* ownerFunction_):
Statement("classDefaultConstruction", classType_->Pos(), ownerFunction_->Body()->GetScope(), classType_->FileScope(), ""), classType(classType_), ownerFunction(ownerFunction_)
{
}
virtual void Dispose()
{
if (!Disposed())
{
Statement::Dispose();
if (baseConstructorCall)
{
baseConstructorCall->Dispose();
baseConstructorCall.reset();
}
for (FunctionCallPtr memberCtorCall : memberConstructorCalls)
{
memberCtorCall->Dispose();
}
memberConstructorCalls.clear();
for (MemberVariablePtr memberVar : memberVars)
{
memberVar->Dispose();
}
memberVars.clear();
}
}
virtual std::string ToString() const
{
return "class default construction statement";
}
virtual ObjectPtr Clone(CloneContext& context) const { CM_CORE_ASSERT(false); return ObjectPtr(); }
virtual void TypeCheckSignature(TypeCheckContext& context)
{
Statement::TypeCheckSignature(context);
bool isNothrow = true;
if (classType.lock()->HasThrowingStaticConstructor())
{
isNothrow = false;
}
FunctionSetPtr ctor = FunctionSetTable::Instance()->GetFunctionSet(GetConstructorFunctionSetName());
CM_CORE_ASSERT(ctor);
ClassTypePtr baseClass = classType.lock()->BaseClass();
if (baseClass)
{
if (!baseClass->DefaultConstructor())
{
throw TypeCheckException(NO_DEFAULT_CTOR, "base class '" + baseClass->Name() + "' has no default constructor to call", Pos());
}
else if (baseClass->DefaultConstructor()->IsSuppressed())
{
throw TypeCheckException(CANNOT_CALL_SUPPRESSED_MEMBER_FUNCTION, "cannot call suppressed member function '" + baseConstructorCall->GetFunction()->FullName() + "'", Pos());
}
baseConstructorCall = FunctionCallPtr(new FunctionCall(baseClass->DefaultConstructor(), Pos(), ownerFunction));
baseConstructorCall->TypeCheck(context);
if (!baseConstructorCall->IsNothrow())
{
isNothrow = false;
}
baseClassType = baseClass;
}
memberVars = classType.lock()->MemberVariables();
int n = memberVars.size();
for (int i = 0; i < n; ++i)
{
MemberVariablePtr memberVar = memberVars[i];
Llvm::Ir::MemberVarPtr irMemberVar = Llvm::Ir::MemberVarPtr(new Llvm::Ir::MemberVar(memberVar->Name(), Llvm::Ir::CreateParameter("this", Llvm::Ir::Pointer(classType.lock()->IrType())),
memberVar->GetIndex(), memberVar->GetType()->IrType()));
irMemberVars.push_back(irMemberVar);
ArgumentVector args;
args.push_back(Argument(MakePointerType(memberVar->GetType(), TypeCheckExtent::signature), ArgumentCategory::lvalue));
ScopeLookupSet scopes;
scopes.insert(ScopeLookup(memberVar->GetType()->GetScope(), Lookup::this_scope));
FunctionCallPtr memberCtor = ctor->ResolveOverload(args, scopes, ResolutionOption::deny_conversions, Pos(), ResolutionFlags::throw_, ownerFunction);
memberConstructorCalls.push_back(memberCtor);
if (!memberCtor->IsNothrow())
{
isNothrow = false;
}
}
if (isNothrow)
{
GetFunction()->SetNothrow();
}
}
virtual void CollectExternalObjects(CompilationUnitPtr compilationUnit, ObjectSet& externalObjects, bool deep)
{
if (ExternalObjectsCollected(compilationUnit)) return;
Statement::CollectExternalObjects(compilationUnit, externalObjects, deep);
if (baseConstructorCall)
{
baseConstructorCall->CollectExternalObjects(compilationUnit, externalObjects, deep);
}
int n = memberConstructorCalls.size();
for (int i = 0; i < n; ++i)
{
FunctionCallPtr memberConstructorCall = memberConstructorCalls[i];
memberConstructorCall->CollectExternalObjects(compilationUnit, externalObjects, deep);
}
}
virtual void GenerateDestructionStatements(DestructionStackPtr destructionStack)
{
Statement::GenerateDestructionStatements(destructionStack);
if (baseConstructorCall)
{
baseConstructorCall->GenerateDestructionStatements(destructionStack);
}
int n = memberConstructorCalls.size();
for (int i = 0; i < n; ++i)
{
FunctionCallPtr memberConstructorCall = memberConstructorCalls[i];
memberConstructorCall->GenerateDestructionStatements(destructionStack);
}
}
virtual GenResult Gen(Ir& ir, GenFlags flags)
{
GenResult result(ir, flags);
if (baseConstructorCall)
{
ir.SetNextInstructionComment(baseConstructorCall->ToString());
GenResult baseResult(ir, flags);
Llvm::Ir::TypePtr baseClassPtr = Llvm::Ir::Pointer(baseClassType.lock()->IrType());
Llvm::Ir::ObjectPtr baseClassObject = Llvm::Ir::CreateTemporaryRegVar(baseClassPtr);
result.AddObject(baseClassObject);
TypePtr classPtrType = MakePointerType(classType.lock(), TypeCheckExtent::signature);
baseResult.AddObject(Llvm::Ir::CreateParameter("this", classPtrType->IrType()));
ir.Emit(Llvm::Ir::Bitcast(Llvm::Ir::Pointer(classType.lock()->IrType()), baseClassObject, baseResult.MainObject(), baseClassPtr));
baseConstructorCall->Generate(ir, result);
result.Merge(baseResult);
}
if (classType.lock()->IsVirtual())
{
SetVtblPtr(ir, classType.lock(), false);
}
int i = 0;
for (FunctionCallPtr memberCall : memberConstructorCalls)
{
ir.SetNextInstructionComment(memberVars[i]->FullName());
GenResult memberResult(ir, flags);
bool isClassType = memberVars[i]->GetType()->IsClassType();
memberResult.AddObject(isClassType ? irMemberVars[i]->CreateAddr(ir, memberVars[i]->GetType()->IrType()) : irMemberVars[i]);
ir.SetNextInstructionComment(memberCall->ToString());
memberCall->Generate(ir, memberResult);
result.Merge(memberResult);
++i;
}
return result;
}
virtual void CheckIfCanThrow(bool& canThrow, TypeCheckContext& context)
{
FunctionPtr staticCtor = classType.lock()->StaticConstructor();
if (staticCtor)
{
staticCtor->CheckIfCanThrow(canThrow, context);
if (canThrow) return;
}
if (baseConstructorCall)
{
baseConstructorCall->CheckIfCanThrow(canThrow, context);
if (canThrow) return;
}
for (FunctionCallPtr memberCall : memberConstructorCalls)
{
memberCall->CheckIfCanThrow(canThrow, context);
if (canThrow) return;
}
}
private:
WeakClassTypePtr classType;
WeakClassTypePtr baseClassType;
FunctionCallPtr baseConstructorCall;
FunctionCallList memberConstructorCalls;
MemberVariableList memberVars;
std::vector<Llvm::Ir::MemberVarPtr> irMemberVars;
Function* ownerFunction;
};
typedef std::shared_ptr<ClassDefaultConstructionStatement> ClassDefaultConstructionStatementPtr;
CompoundStatementPtr MakeEmptyBody(ClassTypePtr classType, ScopePtr scope)
{
return CompoundStatementPtr(new CompoundStatement("body", classType->Pos(), scope, classType->FileScope(), "", true));
}
ClassDefaultCtor::ClassDefaultCtor(const ClassTypePtr& classType_): Function(MakeOverloadName(GetConstructorFunctionSetName(), MakeParameterList1(MakePointerType(classType_, TypeCheckExtent::name), classType_->GetScope())),
classType_->Pos(), classType_->GetScope(), classType_->FileScope(), GetConstructorFunctionSetName(), Specifiers::public_), classType(classType_)
{
SetContent(ObjectPtr(), MakeParameterList1(MakePointerType(classType_, TypeCheckExtent::name), classType_->GetScope()), TemplateParameterList(), MakeEmptyBody(classType_, GetScope()));
Parameters()[0]->SetName("this");
Body()->AddStatement(ClassDefaultConstructionStatementPtr(new ClassDefaultConstructionStatement(classType.lock(), this)));
}
void ClassDefaultCtor::TypeCheckBody(TypeCheckContext& context)
{
FunctionPtr staticCtor = classType.lock()->StaticConstructor();
if (staticCtor)
{
staticCtorCall = FunctionCallPtr(new FunctionCall(staticCtor, staticCtor->Pos(), this));
staticCtorCall->TypeCheck(context);
}
Function::TypeCheckBody(context);
}
void ClassDefaultCtor::Generate(Ir& ir, GenResult& result)
{
GenResult ctorResult(ir, result.Flags());
ctorResult.AddMainObject(VoidType::Instance());
ctorResult.AddObject(result.MainObject()->CreateAddr(ir, classType.lock()->IrType()));
for (Llvm::Ir::ObjectPtr arg : result.Args())
{
ctorResult.AddObject(arg);
}
Function::Generate(ir, ctorResult);
result.Merge(ctorResult);
}
GenResult ClassDefaultCtor::GenerateStaticConstructorCall(Ir& ir, GenFlags flags)
{
GenResult result(ir, flags);
if (staticCtorCall)
{
staticCtorCall->Generate(ir, result);
}
return result;
}
std::string ClassDefaultCtor::GetParsingName() const
{
return classType.lock()->Name();
}
FunctionPtr CreateClassDefaultCtor(const ClassTypePtr& classType)
{
return FunctionPtr(new ClassDefaultCtor(classType));
}
class ClassCopyConstructionStatement: public Statement
{
public:
ClassCopyConstructionStatement(const ClassTypePtr& classType_, Function* ownerFunction_):
Statement("classCopyConstruction", classType_->Pos(), ownerFunction_->Body()->GetScope(), classType_->FileScope(), ""), classType(classType_), ownerFunction(ownerFunction_)
{
}
virtual void Dispose()
{
if (!Disposed())
{
Statement::Dispose();
if (baseCopyConstructorCall)
{
baseCopyConstructorCall->Dispose();
baseCopyConstructorCall.reset();
}
int n = memberConstructorCalls.size();
for (int i = 0; i < n; ++i)
{
memberConstructorCalls[i]->Dispose();
}
memberConstructorCalls.clear();
for (MemberVariablePtr memberVar : memberVars)
{
memberVar->Dispose();
}
memberVars.clear();
}
}
virtual ObjectPtr Clone(CloneContext& context) const { CM_CORE_ASSERT(false); return ObjectPtr(); }
virtual std::string ToString() const
{
return "class copy construction statement";
}
virtual void TypeCheckSignature(TypeCheckContext& context)
{
Statement::TypeCheckSignature(context);
bool isNothrow = true;
if (classType.lock()->HasThrowingStaticConstructor())
{
isNothrow = false;
}
FunctionSetPtr ctor = FunctionSetTable::Instance()->GetFunctionSet(GetConstructorFunctionSetName());
CM_CORE_ASSERT(ctor);
ClassTypePtr baseClass = classType.lock()->BaseClass();
if (baseClass && baseClass->CopyConstructor())
{
if (baseClass->CopyConstructor()->IsSuppressed())
{
throw TypeCheckException(CANNOT_CALL_SUPPRESSED_MEMBER_FUNCTION, "cannot call suppressed member function '" + baseClass->CopyConstructor()->FullName() + "'", Pos());
}
baseCopyConstructorCall = FunctionCallPtr(new FunctionCall(baseClass->CopyConstructor(), Pos(), ownerFunction));
baseCopyConstructorCall->TypeCheck(context);
if (!baseCopyConstructorCall->IsNothrow())
{
isNothrow = false;
}
baseClassType = baseClass;
}
memberVars = classType.lock()->MemberVariables();
int n = memberVars.size();
for (int i = 0; i < n; ++i)
{
MemberVariablePtr memberVar = memberVars[i];
ArgumentVector args;
args.push_back(Argument(MakePointerType(memberVar->GetType(), TypeCheckExtent::signature), ArgumentCategory::lvalue));
args.push_back(Argument(MakeConstReferenceType(memberVar->GetType(), TypeCheckExtent::signature), ArgumentCategory::lvalue));
ScopeLookupSet scopes;
scopes.insert(ScopeLookup(memberVar->GetType()->GetScope(), Lookup::this_scope));
FunctionCallPtr memberCtorCall = ctor->ResolveOverload(args, scopes, ResolutionOption::allow_conversions, Pos(), ResolutionFlags::throw_, ownerFunction);
if (!memberCtorCall->IsNothrow())
{
isNothrow = false;
}
memberConstructorCalls.push_back(memberCtorCall);
}
if (isNothrow)
{
GetFunction()->SetNothrow();
}
}
virtual void CollectExternalObjects(CompilationUnitPtr compilationUnit, ObjectSet& externalObjects, bool deep)
{
if (ExternalObjectsCollected(compilationUnit)) return;
Statement::CollectExternalObjects(compilationUnit, externalObjects, deep);
if (baseCopyConstructorCall)
{
baseCopyConstructorCall->CollectExternalObjects(compilationUnit, externalObjects, deep);
}
int n = memberConstructorCalls.size();
for (int i = 0; i < n; ++i)
{
FunctionCallPtr memberConstructorCall = memberConstructorCalls[i];
memberConstructorCall->CollectExternalObjects(compilationUnit, externalObjects, deep);
}
}
virtual GenResult Gen(Ir& ir, GenFlags flags)
{
TypePtr classPtrType = MakePointerType(classType.lock(), TypeCheckExtent::signature);
GenResult result(ir, flags);
if (baseCopyConstructorCall)
{
ir.SetNextInstructionComment(baseCopyConstructorCall->ToString());
GenResult baseResult(ir, flags);
Llvm::Ir::TypePtr baseClassPtr = Llvm::Ir::Pointer(baseClassType.lock()->IrType());
Llvm::Ir::ObjectPtr thisBaseClassObject = Llvm::Ir::CreateTemporaryRegVar(baseClassPtr);
result.AddObject(thisBaseClassObject);
TypePtr classPtrType = MakePointerType(classType.lock(), TypeCheckExtent::signature);
baseResult.AddObject(Llvm::Ir::CreateParameter("this", classPtrType->IrType()));
ir.Emit(Llvm::Ir::Bitcast(Llvm::Ir::Pointer(classType.lock()->IrType()), thisBaseClassObject, baseResult.MainObject(), baseClassPtr));
Llvm::Ir::ObjectPtr thatBaseClassObject = Llvm::Ir::CreateTemporaryRegVar(baseClassPtr);
result.AddObject(thatBaseClassObject);
baseResult.AddObject(Llvm::Ir::CreateParameter("that", classPtrType->IrType()));
ir.Emit(Llvm::Ir::Bitcast(Llvm::Ir::Pointer(classType.lock()->IrType()), thatBaseClassObject, baseResult.Arg1(), baseClassPtr));
baseCopyConstructorCall->Generate(ir, result);
result.Merge(baseResult);
}
if (classType.lock()->IsVirtual())
{
SetVtblPtr(ir, classType.lock(), false);
}
int n = memberVars.size();
for (int i = 0; i < n; ++i)
{
GenResult memberResult(ir, flags);
MemberVariablePtr memberVar = memberVars[i];
bool isClassType = memberVar->GetType()->IsClassType();
Llvm::Ir::MemberVarPtr left(new Llvm::Ir::MemberVar(memberVar->Name(), Llvm::Ir::CreateParameter("this", classPtrType->IrType()), memberVar->GetIndex(), memberVar->GetType()->IrType()));
ir.SetNextInstructionComment(memberVars[i]->FullName());
memberResult.AddObject(isClassType ? left->CreateAddr(ir, memberVar->GetType()->IrType()): left);
Llvm::Ir::MemberVarPtr right(new Llvm::Ir::MemberVar(memberVar->Name(), Llvm::Ir::CreateParameter("that", classPtrType->IrType()), memberVar->GetIndex(), memberVar->GetType()->IrType()));
memberResult.AddObject(isClassType ? right->CreateAddr(ir, memberVar->GetType()->IrType()) : right);
ir.SetNextInstructionComment(memberConstructorCalls[i]->ToString());
memberConstructorCalls[i]->Generate(ir, memberResult);
result.Merge(memberResult);
}
return result;
}
virtual void GenerateDestructionStatements(DestructionStackPtr destructionStack)
{
Statement::GenerateDestructionStatements(destructionStack);
if (baseCopyConstructorCall)
{
baseCopyConstructorCall->GenerateDestructionStatements(destructionStack);
}
int n = memberConstructorCalls.size();
for (int i = 0; i < n; ++i)
{
FunctionCallPtr memberConstructorCall = memberConstructorCalls[i];
memberConstructorCall->GenerateDestructionStatements(destructionStack);
}
}
virtual void CheckIfCanThrow(bool& canThrow, TypeCheckContext& context)
{
FunctionPtr staticCtor = classType.lock()->StaticConstructor();
if (staticCtor)
{
staticCtor->CheckIfCanThrow(canThrow, context);
if (canThrow) return;
}
if (baseCopyConstructorCall)
{
baseCopyConstructorCall->CheckIfCanThrow(canThrow, context);
if (canThrow) return;
}
for (FunctionCallPtr memberConstructorCall : memberConstructorCalls)
{
memberConstructorCall->CheckIfCanThrow(canThrow, context);
if (canThrow) return;
}
}
private:
WeakClassTypePtr classType;
WeakClassTypePtr baseClassType;
FunctionCallPtr baseCopyConstructorCall;
FunctionCallList memberConstructorCalls;
MemberVariableList memberVars;
Function* ownerFunction;
};
typedef std::shared_ptr<ClassCopyConstructionStatement> ClassCopyConstructionStatementPtr;
ClassCopyCtor::ClassCopyCtor(const ClassTypePtr& classType_): Function(MakeOverloadName(GetConstructorFunctionSetName(), MakeParameterList2(MakePointerType(classType_, TypeCheckExtent::name),
MakeConstReferenceType(classType_, TypeCheckExtent::name),
classType_->GetScope())), classType_->Pos(), classType_->GetScope(), classType_->FileScope(), GetConstructorFunctionSetName(), Specifiers::public_), classType(classType_)
{
SetContent(ObjectPtr(), MakeParameterList2(MakePointerType(classType_, TypeCheckExtent::name), MakeConstReferenceType(classType_, TypeCheckExtent::name), classType_->GetScope()), TemplateParameterList(), MakeEmptyBody(classType_, GetScope()));
Parameters()[0]->SetName("this");
Parameters()[1]->SetName("that");
Body()->AddStatement(ClassCopyConstructionStatementPtr(new ClassCopyConstructionStatement(classType.lock(), this)));
}
void ClassCopyCtor::TypeCheckBody(TypeCheckContext& context)
{
FunctionPtr staticCtor = classType.lock()->StaticConstructor();
if (staticCtor)
{
staticCtorCall = FunctionCallPtr(new FunctionCall(staticCtor, staticCtor->Pos(), this));
staticCtorCall->TypeCheck(context);
}
Function::TypeCheckBody(context);
}
void ClassCopyCtor::Generate(Ir& ir, GenResult& result)
{
GenResult ctorResult(ir, result.Flags());
ctorResult.AddMainObject(VoidType::Instance());
ctorResult.AddObject(result.MainObject()->CreateAddr(ir, classType.lock()->IrType()));
ctorResult.AddObject(result.Arg1()->CreateAddr(ir, classType.lock()->IrType()));
bool first = true;
for (Llvm::Ir::ObjectPtr arg : result.Args())
{
if (first)
{
first = false;
}
else
{
ctorResult.AddObject(arg);
}
}
Function::Generate(ir, ctorResult);
result.Merge(ctorResult);
}
GenResult ClassCopyCtor::GenerateStaticConstructorCall(Ir& ir, GenFlags flags)
{
GenResult result(ir, GenFlags::none);
if (staticCtorCall)
{
staticCtorCall->Generate(ir, result);
}
return result;
}
std::string ClassCopyCtor::GetParsingName() const
{
return classType.lock()->Name();
}
FunctionPtr CreateClassCopyCtor(const ClassTypePtr& classType)
{
return FunctionPtr(new ClassCopyCtor(classType));
}
class ClassAssignmentStatement: public Statement
{
public:
ClassAssignmentStatement(const ClassTypePtr& classType_, Function* ownerFunction_):
Statement("classAssignment", classType_->Pos(), ownerFunction_->Body()->GetScope(), classType_->FileScope(), ""), classType(classType_), ownerFunction(ownerFunction_)
{
}
virtual void Dispose()
{
if (!Disposed())
{
Statement::Dispose();
if (baseAssignmentCall)
{
baseAssignmentCall->Dispose();
baseAssignmentCall.reset();
}
int n = memberAssignmentCalls.size();
for (int i = 0; i < n; ++i)
{
memberAssignmentCalls[i]->Dispose();
}
memberAssignmentCalls.clear();
}
}
virtual ObjectPtr Clone(CloneContext& context) const { CM_CORE_ASSERT(false); return ObjectPtr(); }
virtual std::string ToString() const
{
return "class assignment statement";
}
virtual void TypeCheckSignature(TypeCheckContext& context)
{
Statement::TypeCheckSignature(context);
bool isNothrow = true;
FunctionSetPtr assignment = FunctionSetTable::Instance()->GetFunctionSet("operator=");
CM_CORE_ASSERT(assignment);
ClassTypePtr baseClass = classType.lock()->BaseClass();
if (baseClass && baseClass->Assignment())
{
if (baseClass->Assignment()->IsSuppressed())
{
throw TypeCheckException(CANNOT_CALL_SUPPRESSED_MEMBER_FUNCTION, "cannot call suppressed member function '" + baseClass->Assignment()->FullName() + "'", Pos());
}
baseAssignmentCall = FunctionCallPtr(new FunctionCall(baseClass->Assignment(), Pos(), ownerFunction));
baseAssignmentCall->TypeCheck(context);
if (!baseAssignmentCall->IsNothrow())
{
isNothrow = false;
}
baseClassType = baseClass;
}
memberVars = classType.lock()->MemberVariables();
int n = memberVars.size();
for (int i = 0; i < n; ++i)
{
MemberVariablePtr memberVar = memberVars[i];
ArgumentVector args;
args.push_back(Argument(MakePointerType(memberVar->GetType(), TypeCheckExtent::signature), ArgumentCategory::lvalue));
args.push_back(Argument(MakeConstReferenceType(memberVar->GetType(), TypeCheckExtent::signature), ArgumentCategory::lvalue));
ScopeLookupSet scopes;
scopes.insert(ScopeLookup(memberVar->GetType()->GetScope(), Lookup::this_scope));
FunctionCallPtr memberAssignmentCall = assignment->ResolveOverload(args, scopes, ResolutionOption::allow_conversions, Pos(), ResolutionFlags::throw_, ownerFunction);
if (!memberAssignmentCall->IsNothrow())
{
isNothrow = false;
}
memberAssignmentCalls.push_back(memberAssignmentCall);
}
if (isNothrow)
{
GetFunction()->SetNothrow();
}
}
virtual void CollectExternalObjects(CompilationUnitPtr compilationUnit, ObjectSet& externalObjects, bool deep)
{
if (ExternalObjectsCollected(compilationUnit)) return;
Statement::CollectExternalObjects(compilationUnit, externalObjects, deep);
if (baseAssignmentCall)
{
baseAssignmentCall->CollectExternalObjects(compilationUnit, externalObjects, deep);
}
int n = memberAssignmentCalls.size();
for (int i = 0; i < n; ++i)
{
FunctionCallPtr memberAssignmentCall = memberAssignmentCalls[i];
memberAssignmentCall->CollectExternalObjects(compilationUnit, externalObjects, deep);
}
}
virtual GenResult Gen(Ir& ir, GenFlags flags)
{
TypePtr classPtrType = MakePointerType(classType.lock(), TypeCheckExtent::signature);
GenResult result(ir, flags);
if (baseAssignmentCall)
{
ir.SetNextInstructionComment(baseAssignmentCall->ToString());
GenResult baseResult(ir, flags);
Llvm::Ir::TypePtr baseClassPtr = Llvm::Ir::Pointer(baseClassType.lock()->IrType());
Llvm::Ir::ObjectPtr thisBaseClassObject = Llvm::Ir::CreateTemporaryRegVar(baseClassPtr);
result.AddObject(thisBaseClassObject);
TypePtr classPtrType = MakePointerType(classType.lock(), TypeCheckExtent::signature);
baseResult.AddObject(Llvm::Ir::CreateParameter("this", classPtrType->IrType()));
ir.Emit(Llvm::Ir::Bitcast(Llvm::Ir::Pointer(classType.lock()->IrType()), thisBaseClassObject, baseResult.MainObject(), baseClassPtr));
Llvm::Ir::ObjectPtr thatBaseClassObject = Llvm::Ir::CreateTemporaryRegVar(baseClassPtr);
result.AddObject(thatBaseClassObject);
baseResult.AddObject(Llvm::Ir::CreateParameter("that", classPtrType->IrType()));
ir.Emit(Llvm::Ir::Bitcast(Llvm::Ir::Pointer(classType.lock()->IrType()), thatBaseClassObject, baseResult.Arg1(), baseClassPtr));
baseAssignmentCall->Generate(ir, result);
result.Merge(baseResult);
}
int n = memberVars.size();
for (int i = 0; i < n; ++i)
{
GenResult memberResult(ir, flags);
MemberVariablePtr memberVar = memberVars[i];
bool isClassType = memberVar->GetType()->IsClassType();
ir.SetNextInstructionComment(memberVars[i]->FullName());
Llvm::Ir::MemberVarPtr left(new Llvm::Ir::MemberVar(memberVar->Name(), Llvm::Ir::CreateParameter("this", classPtrType->IrType()), memberVar->GetIndex(), memberVar->GetType()->IrType()));
memberResult.AddObject(isClassType ? left->CreateAddr(ir, memberVar->GetType()->IrType()): left);
Llvm::Ir::MemberVarPtr right(new Llvm::Ir::MemberVar(memberVar->Name(), Llvm::Ir::CreateParameter("that", classPtrType->IrType()), memberVar->GetIndex(), memberVar->GetType()->IrType()));
memberResult.AddObject(isClassType ? right->CreateAddr(ir, memberVar->GetType()->IrType()) : right);
ir.SetNextInstructionComment(memberAssignmentCalls[i]->ToString());
memberAssignmentCalls[i]->Generate(ir, memberResult);
result.Merge(memberResult);
}
return result;
}
virtual void CheckIfCanThrow(bool& canThrow, TypeCheckContext& context)
{
if (baseAssignmentCall)
{
baseAssignmentCall->CheckIfCanThrow(canThrow, context);
if (canThrow) return;
}
int n = memberAssignmentCalls.size();
for (int i = 0; i < n; ++i)
{
FunctionCallPtr memberAssignmentCall = memberAssignmentCalls[i];
memberAssignmentCall->CheckIfCanThrow(canThrow, context);
if (canThrow) return;
}
}
private:
WeakClassTypePtr classType;
WeakClassTypePtr baseClassType;
FunctionCallPtr baseAssignmentCall;
FunctionCallList memberAssignmentCalls;
MemberVariableList memberVars;
Function* ownerFunction;
};
typedef std::shared_ptr<ClassAssignmentStatement> ClassAssignmentStatementPtr;
ClassAssignment::ClassAssignment(const ClassTypePtr& classType_): Function(MakeOverloadName("operator=", MakeParameterList2(MakePointerType(classType_, TypeCheckExtent::name),
MakeConstReferenceType(classType_, TypeCheckExtent::name),
classType_->GetScope())), classType_->Pos(), classType_->GetScope(), classType_->FileScope(), "operator=", Specifiers::public_), classType(classType_)
{
SetContent(VoidType::Instance(), MakeParameterList2(MakePointerType(classType_, TypeCheckExtent::name), MakeConstReferenceType(classType_, TypeCheckExtent::name), classType_->GetScope()), TemplateParameterList(), MakeEmptyBody(classType_, GetScope()));
Parameters()[0]->SetName("this");
Parameters()[1]->SetName("that");
Body()->AddStatement(ClassAssignmentStatementPtr(new ClassAssignmentStatement(classType.lock(), this)));
}
void ClassAssignment::Generate(Ir& ir, GenResult& result)
{
GenResult assignmentResult(ir, result.Flags());
assignmentResult.AddMainObject(VoidType::Instance());
assignmentResult.AddObject(result.MainObject()->CreateAddr(ir, classType.lock()->IrType()));
assignmentResult.AddObject(result.Arg1()->CreateAddr(ir, classType.lock()->IrType()));
bool first = true;
for (Llvm::Ir::ObjectPtr arg : result.Args())
{
if (first)
{
first = false;
}
else
{
assignmentResult.AddObject(arg);
}
}
Function::Generate(ir, assignmentResult);
result.Merge(assignmentResult);
}
FunctionPtr CreateClassAssignment(const ClassTypePtr& classType)
{
return FunctionPtr(new ClassAssignment(classType));
}
class ClassDestructionStatement: public Statement
{
public:
ClassDestructionStatement(const ClassTypePtr& classType_, Function* ownerFunction_, bool useThisVariable_):
Statement("classDestruction", classType_->Pos(), ownerFunction_->Body()->GetScope(), classType_->FileScope(), ""), classType(classType_), ownerFunction(ownerFunction_), useThisVariable(useThisVariable_)
{
}
virtual void Dispose()
{
if (!Disposed())
{
Statement::Dispose();
if (baseDestructorCall)
{
baseDestructorCall->Dispose();
baseDestructorCall.reset();
}
int n = memberDestructorCalls.size();
for (int i = 0; i < n; ++i)
{
if (memberDestructorCalls[i])
{
memberDestructorCalls[i]->Dispose();
}
}
memberDestructorCalls.clear();
for (MemberVariablePtr memberVar : memberVars)
{
memberVar->Dispose();
}
memberVars.clear();
}
}
virtual ObjectPtr Clone(CloneContext& context) const { CM_CORE_ASSERT(false); return ObjectPtr(); }
virtual std::string ToString() const
{
return "class destruction statement";
}
virtual void TypeCheckSignature(TypeCheckContext& context)
{
Statement::TypeCheckSignature(context);
bool isNothrow = true;
FunctionSetPtr dtor = FunctionSetTable::Instance()->GetFunctionSet(GetDestructorFunctionSetName());
CM_CORE_ASSERT(dtor);
ClassTypePtr baseClass = classType.lock()->BaseClass();
if (baseClass && baseClass->Destructor())
{
if (baseClass->Destructor()->IsSuppressed())
{
throw TypeCheckException(CANNOT_CALL_SUPPRESSED_MEMBER_FUNCTION, "cannot call suppressed member function '" + baseClass->Destructor()->FullName() + "'", Pos());
}
baseDestructorCall = FunctionCallPtr(new FunctionCall(baseClass->Destructor(), Pos(), ownerFunction));
baseDestructorCall->TypeCheck(context);
if (!baseDestructorCall->IsNothrow())
{
isNothrow = false;
}
baseClassType = baseClass;
}
memberVars = classType.lock()->MemberVariables();
int n = memberVars.size();
for (int i = 0; i < n; ++i)
{
MemberVariablePtr memberVar = memberVars[i];
FunctionCallPtr memberDtorCall;
if (!memberVar->HasTrivialDestructor())
{
ArgumentVector args;
args.push_back(Argument(MakePointerType(memberVar->GetType(), TypeCheckExtent::signature), ArgumentCategory::lvalue));
ScopeLookupSet scopes;
scopes.insert(ScopeLookup(memberVar->GetType()->GetScope(), Lookup::this_scope));
memberDtorCall = dtor->ResolveOverload(args, scopes, ResolutionOption::deny_conversions, Pos(), ResolutionFlags::throw_, ownerFunction);
if (!memberDtorCall->IsNothrow())
{
isNothrow = false;
}
}
memberDestructorCalls.push_back(memberDtorCall);
}
if (isNothrow)
{
GetFunction()->SetNothrow();
}
}
virtual void CollectExternalObjects(CompilationUnitPtr compilationUnit, ObjectSet& externalObjects, bool deep)
{
if (ExternalObjectsCollected(compilationUnit)) return;
Statement::CollectExternalObjects(compilationUnit, externalObjects, deep);
if (baseDestructorCall)
{
baseDestructorCall->CollectExternalObjects(compilationUnit, externalObjects, deep);
}
int n = memberDestructorCalls.size();
for (int i = 0; i < n; ++i)
{
FunctionCallPtr memberDestructorCall = memberDestructorCalls[i];
if (memberDestructorCall)
{
memberDestructorCall->CollectExternalObjects(compilationUnit, externalObjects, deep);
}
}
}
virtual void GenerateDestructionStatements(DestructionStackPtr destructionStack)
{
Statement::GenerateDestructionStatements(destructionStack);
int n = memberDestructorCalls.size();
for (int i = n - 1; i >= 0; --i)
{
FunctionCallPtr memberDestructorCall = memberDestructorCalls[i];
if (memberDestructorCall)
{
memberDestructorCall->GenerateDestructionStatements(destructionStack);
}
}
if (baseDestructorCall)
{
baseDestructorCall->GenerateDestructionStatements(destructionStack);
}
}
virtual GenResult Gen(Ir& ir, GenFlags flags)
{
GenResult result(ir, flags);
Llvm::Ir::ObjectPtr thisPtr;
if (useThisVariable)
{
LocalVariablePtr thisVar(new ThisVariable(classType.lock()->Pos(), classType.lock()->EnclosingScope(), classType.lock()->FileScope(), classType.lock()));
TypeCheckContext body(TypeCheckExtent::body);
thisVar->TypeCheck(body);
GenResult thisResult = thisVar->Gen(ir, GenFlags::none);
thisPtr = thisResult.MainObject();
}
else
{
thisPtr = Llvm::Ir::ObjectPtr(new Llvm::Ir::Parameter("this", Llvm::Ir::Pointer(classType.lock()->IrType())));
}
int n = memberVars.size();
for (int i = n - 1; i >= 0; --i)
{
if (memberDestructorCalls[i])
{
GenResult memberResult(ir, flags);
MemberVariablePtr memberVar = memberVars[i];
bool isClassType = memberVar->GetType()->IsClassType();
Llvm::Ir::MemberVarPtr mv(new Llvm::Ir::MemberVar(memberVar->Name(), thisPtr, memberVar->GetIndex(), memberVar->GetType()->IrType()));
ir.SetNextInstructionComment(memberVar->FullName());
memberResult.AddObject(isClassType ? mv->CreateAddr(ir, mv->GetType()) : mv);
ir.SetNextInstructionComment(memberDestructorCalls[i]->ToString());
memberDestructorCalls[i]->Generate(ir, memberResult);
result.Merge(memberResult);
}
}
if (baseDestructorCall)
{
ir.SetNextInstructionComment(baseDestructorCall->ToString());
GenResult baseResult(ir, flags);
Llvm::Ir::TypePtr baseClassPtr = Llvm::Ir::Pointer(baseClassType.lock()->IrType());
Llvm::Ir::ObjectPtr baseClassObject = Llvm::Ir::CreateTemporaryRegVar(baseClassPtr);
result.AddObject(baseClassObject);
baseResult.AddObject(baseClassObject);
ir.Emit(Llvm::Ir::Bitcast(Llvm::Ir::Pointer(classType.lock()->IrType()), baseClassObject, thisPtr, baseClassPtr));
baseDestructorCall->Generate(ir, baseResult);
result.Merge(baseResult);
}
return result;
}
private:
WeakClassTypePtr classType;
WeakClassTypePtr baseClassType;
FunctionCallPtr baseDestructorCall;
FunctionCallList memberDestructorCalls;
MemberVariableList memberVars;
Function* ownerFunction;
bool useThisVariable;
};
typedef std::shared_ptr<ClassDestructionStatement> ClassDestructionStatementPtr;
StatementPtr CreateClassDestructionStatement(ClassTypePtr classType, Function* ownerFunction, bool useThisVariable)
{
return StatementPtr(new ClassDestructionStatement(classType, ownerFunction, useThisVariable));
}
ClassDtor::ClassDtor(const ClassTypePtr& classType_):
DestructorBase(MakeOverloadName(GetDestructorFunctionSetName(), MakeParameterList1(MakePointerType(classType_, TypeCheckExtent::name), classType_->GetScope())),
classType_->Pos(), classType_->GetScope(), classType_->FileScope(), Specifiers::public_, classType_), classType(classType_)
{
SetContent(ObjectPtr(), MakeParameterList1(MakePointerType(classType_, TypeCheckExtent::name), classType_->GetScope()), TemplateParameterList(), MakeEmptyBody(classType_->GetScope()));
Parameters()[0]->SetName("this");
Body()->AddStatement(StatementPtr(new SetVtblPtrStatement(ClassType(), false)));
Body()->AddStatement(StatementPtr(new ClassDestructionStatement(ClassType(), this, false)));
}
void ClassDtor::Generate(Ir& ir, GenResult& result)
{
GenResult dtorResult(ir, result.Flags());
dtorResult.AddMainObject(VoidType::Instance());
dtorResult.AddObject(result.MainObject()->CreateAddr(ir, classType.lock()->IrType()));
Function::Generate(ir, dtorResult);
result.Merge(dtorResult);
}
std::string ClassDtor::GetParsingName() const
{
return "~" + classType.lock()->Name();
}
MemberFunctionPtr CreateClassDtor(ClassTypePtr classType)
{
return MemberFunctionPtr(new ClassDtor(classType));
}
StatementPtr CreateSetVtblPtrStatement(ClassTypePtr classType, bool useThisVariable)
{
return StatementPtr(new SetVtblPtrStatement(classType, useThisVariable));
}
} } // namespace Cm::Core