forked from llvm-mirror/clang
-
Notifications
You must be signed in to change notification settings - Fork 12
/
SemaCLI.cpp
1321 lines (1059 loc) · 43.7 KB
/
SemaCLI.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
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- SemaCLI.cpp - Semantic Analysis for C++/CLI-----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements semantic analysis for C++/CLI language constructs.
//
//===----------------------------------------------------------------------===//
#include "clang/Sema/Sema.h"
#include "clang/Sema/SemaCLI.h"
#include "clang/Sema/SemaInternal.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Scope.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DeclCLI.h"
#include "clang/AST/TypeCLI.h"
#include "clang/AST/CXXInheritance.h"
#include "llvm/ADT/StringExtras.h"
#include "TypeLocBuilder.h"
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Collections::Generic;
using namespace System::Runtime::InteropServices;
#using <Mono.Cecil.dll>
using namespace Mono::Cecil;
#include "CLIInterop.h"
#include <vcclr.h>
namespace clang {
using namespace sema;
using namespace clix;
static IdentifierInfo *getIdentifier(Sema &S, StringRef Name) {
IdentifierTable& IdentTable = S.getPreprocessor().getIdentifierTable();
return &IdentTable.get(Name);
}
static IdentifierInfo *getIdentifier(Sema &S, System::String ^Name) {
std::string IdentName = marshalString<E_UTF8>(Name);
return getIdentifier(S, IdentName);
}
static NamespaceDecl *findCreateNamespace(Sema &S, DeclContext *DC,
StringRef Namespace) {
IdentifierInfo *II = getIdentifier(S, Namespace);
DeclContextLookupResult Res = DC->lookup(DeclarationName(II));
for (auto it = Res.begin(); it != Res.end(); ++it) {
if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*it)) {
return ND;
}
}
NamespaceDecl *NS = NamespaceDecl::Create(S.getASTContext(), DC,
/*IsInline=*/false, SourceLocation(), SourceLocation(), II, 0);
DC->addDecl(NS);
return NS;
}
static DeclContext *findCreateNamespaces(Sema &S, StringRef Namespace) {
TranslationUnitDecl* TU = S.getASTContext().getTranslationUnitDecl();
DeclContext* DC = TU->getPrimaryContext();
if (Namespace.size() == 0)
return DC;
llvm::SmallVector<StringRef, 4> Namespaces;
llvm::SplitString(Namespace, Namespaces, ".");
NamespaceDecl* NS = 0;
for (unsigned i = 0; i < Namespaces.size(); ++i) {
NS = findCreateNamespace(S, DC, Namespaces[i]);
assert(NS && "Expected a valid namespace");
DC = NS->getPrimaryContext();
}
return NS;
}
static DeclContext *findCreateNamespaces(Sema &S, TypeReference ^Type) {
std::string Namespace = marshalString<E_UTF8>(Type->Namespace);
return findCreateNamespaces(S, Namespace);
}
static DeclContext* findCreateNamespaces(Sema &S, TypeDefinition ^Type) {
std::string Namespace = marshalString<E_UTF8>(Type->Namespace);
return findCreateNamespaces(S, Namespace);
}
static AccessSpecifier convertMethodAccess(MethodDefinition ^Method) {
if (Method->IsPublic) return AS_public;
else if (Method->IsAssembly) return AS_internal;
else if (Method->IsFamily) return AS_protected;
else if (Method->IsFamilyOrAssembly) return AS_protected_public;
else if (Method->IsFamilyAndAssembly) return AS_protected_private;
else return AS_private;
}
static AccessSpecifier convertFieldAccess(FieldDefinition ^Field) {
if (Field->IsPublic) return AS_public;
else if (Field->IsAssembly) return AS_internal;
else if (Field->IsFamily) return AS_protected;
else if (Field->IsFamilyOrAssembly) return AS_protected_public;
else if (Field->IsFamilyAndAssembly) return AS_protected_private;
else return AS_private;
}
static AccessSpecifier convertPropertyAccess(PropertyDefinition ^Prop) {
return AS_public;
}
static bool findCreateType(Sema &S, TypeReference ^TypeRef, QualType &Type,
CXXRecordDecl *MethodRecord = 0);
typedef Mono::Collections::Generic::Collection<Mono::Cecil::CustomAttribute^>
CustomAttributeCollection;
static void createDeclAttributes(Sema &S, CustomAttributeCollection ^Attributes,
Decl *D);
static CXXRecordDecl *findCreateClassDecl(Sema &S, TypeDefinition ^TypeDef);
static bool hasCLIParamsAttribute(Sema &S, MethodDefinition ^Method) {
for each (ParameterDefinition ^Param in Method->Parameters) {
for each (CustomAttribute ^Attr in Param->CustomAttributes) {
CXXRecordDecl *AttrClass =
findCreateClassDecl(S, Attr->AttributeType->Resolve());
if (AttrClass == S.getCLIContext()->ParamArrayAttribute)
return true;
}
}
return false;
}
static DeclarationName getMethodDeclName(Sema &S, MethodDefinition ^Method,
CXXRecordDecl *RD) {
ASTContext &C = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
std::string MethodName = marshalString<E_UTF8>(Method->Name);
IdentifierInfo &II = IdentTable.get(MethodName);
TagDecl *D = RD;
if (RD->isCLIRecord() && RD->getCLIData()->isGeneric()) {
/*Ty = S.getASTContext().getInjectedClassNameType(RD, */
//D = RD->getDescribedClassTemplate();
}
DeclarationName DN;
if (Method->IsConstructor) {
CanQualType Ty = C.getCanonicalType(C.getTagDeclType(RD));
DN = C.DeclarationNames.getCXXConstructorName(Ty);
} else {
DN = C.DeclarationNames.getIdentifier(&II);
}
return DN;
}
static CXXMethodDecl *findCreateMethod(Sema &S, MethodDefinition ^Method,
CXXRecordDecl *RD,
bool AddToDecl = true) {
ASTContext &C = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
if (!RD) {
RD = findCreateClassDecl(S, Method->DeclaringType);
assert (RD && "Expected a valid record decl");
}
DeclarationName DN = getMethodDeclName(S, Method, RD);
unsigned MetadataToken = Method->MetadataToken.ToUInt32();
DeclContextLookupResult Res = RD->lookup(DN);
for (auto it = Res.begin(); it != Res.end(); ++it) {
Decl *D = *it;
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
CLIMethodData *CLIData = MD->getCLIData();
if (CLIData->MetadataToken == MetadataToken)
return MD;
}
}
QualType RT;
findCreateType(S, Method->ReturnType, RT, RD);
if (RT.isNull())
return 0;
//assert(!RT.isNull() && "Expected a valid method return type");
llvm::SmallVector<QualType, 4> ParamTypes;
for each (ParameterDefinition ^Param in Method->Parameters) {
QualType ParamType;
if (!findCreateType(S, Param->ParameterType, ParamType, RD))
return nullptr;
if (ParamType.isNull())
return nullptr;
ParamTypes.push_back(ParamType);
}
FunctionProtoType::ExtProtoInfo Info;
if (Method->CallingConvention == MethodCallingConvention::VarArg) {
Info.Variadic = true;
}
QualType FT = C.getFunctionType(RT, ParamTypes, Info);
TypeSourceInfo *TSI = C.getTrivialTypeSourceInfo(FT);
FunctionProtoTypeLoc FTL = TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>();
DeclarationNameInfo DNI(DN, SourceLocation());
CXXMethodDecl *MD = 0;
if (Method->IsStatic && Method->IsConstructor) {
return 0;
} else if (Method->IsConstructor) {
MD = CXXConstructorDecl::Create(C, RD, SourceLocation(), DNI, FT, TSI,
/*IsExplicit=*/true, /*IsInline=*/false, /*IsImplicit=*/false,
/*IsConstexpr=*/false);
} else {
MD = CXXMethodDecl::Create(C, RD, SourceLocation(), DNI, FT, TSI,
Method->IsStatic ? SC_Static : SC_None, /*IsInline=*/false,
/*IsConstexpr=*/false, SourceLocation());
}
CLIMethodData *CLIData = new (C) CLIMethodData();
CLIData->MetadataToken = MetadataToken;
CLIData->FullName = marshalString<E_UTF8>(Method->FullName);
MD->setCLIData(CLIData);
MD->setAccess(convertMethodAccess(Method));
llvm::SmallVector<ParmVarDecl*, 4> ParamDecls;
unsigned paramIndex = 0;
for each (ParameterDefinition^ Param in Method->Parameters) {
std::string ParamName = marshalString<E_UTF8>(Param->Name);
IdentifierInfo& PII = IdentTable.get(ParamName);
QualType ParamType;
findCreateType(S, Param->ParameterType, ParamType, RD);
ParmVarDecl *PVD = ParmVarDecl::Create(C, MD, SourceLocation(),
SourceLocation(), &PII, ParamType,
C.getTrivialTypeSourceInfo(ParamType),
SC_Auto, 0);
assert(PVD && "Expected a valid parameter decl");
PVD->setScopeInfo(0, paramIndex);
FTL.setArg(paramIndex++, PVD);
createDeclAttributes(S, Param->CustomAttributes, PVD);
ParamDecls.push_back(PVD);
}
createDeclAttributes(S, Method->CustomAttributes, MD);
assert(ParamDecls.size() == Method->Parameters->Count);
MD->setParams(ParamDecls);
if (AddToDecl)
RD->addDecl(MD);
return MD;
}
static DeclaratorDecl *createField(Sema &S, FieldDefinition ^Field,
CXXRecordDecl *RD) {
ASTContext &C = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
std::string FieldName = marshalString<E_UTF8>(Field->Name);
IdentifierInfo &II = IdentTable.get(FieldName);
QualType FT;
findCreateType(S, Field->FieldType, FT, RD);
if (FT.isNull()) {
std::string Name = marshalString<E_UTF8>(Field->FieldType->Name);
return 0;
}
TypeSourceInfo *TSI = C.getTrivialTypeSourceInfo(FT);
DeclaratorDecl *DD = 0;
if (Field->IsStatic) {
DD = VarDecl::Create(C, RD, SourceLocation(), SourceLocation(), &II,
FT, TSI, SC_Static);
} else {
DD = FieldDecl::Create(C, RD, SourceLocation(), SourceLocation(), &II,
FT, TSI, 0, 0, ICIS_NoInit);
}
assert(DD && "Expected a valid declarator decl");
DD->setAccess(convertFieldAccess(Field));
return DD;
}
static CLIRecordType GetCLIRecordType(TypeDefinition ^Type) {
assert(Type->IsClass);
if (Type->IsInterface)
return CLI_RT_InterfaceType;
else if (Type->IsValueType)
return CLI_RT_ValueType;
return CLI_RT_ReferenceType;
}
static CXXRecordDecl * findCreateClassDecl(Sema &S, TypeReference ^TypeRef);
static CLIDefinitionData *GetCLIRecordData(Sema &S, TypeDefinition^ TypeDef) {
CLIDefinitionData *CD = new (S.getASTContext()) CLIDefinitionData();
CD->AssemblyName = marshalString<E_UTF8>(TypeDef->Module->Assembly->Name->Name);
CD->FullName = marshalString<E_UTF8>(TypeDef->FullName);
CD->Type = CLI_RT_ReferenceType;
if (TypeDef->IsInterface)
CD->Type = CLI_RT_InterfaceType;
else if (TypeDef->IsValueType)
CD->Type = CLI_RT_ValueType;
return CD;
}
static ClassTemplateDecl *GetCLIClassTemplate(Sema &S, TypeDefinition^ TypeDef,
CXXRecordDecl *RD) {
assert(TypeDef->HasGenericParameters);
ASTContext &C = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
std::string Name = marshalString<E_UTF8>(TypeDef->Name);
IdentifierInfo &II = IdentTable.get(Name);
CLIGenericData *Data = new (C) CLIGenericData();
llvm::SmallVector<TemplateTypeParmDecl *, 4> Params;
for each(GenericParameter ^Param in TypeDef->GenericParameters) {
CLIGenericParameter GP;
GP.Name = marshalString<E_UTF8>(Param->Name);
GP.Flags = 0;
Data->Parameters.push_back(GP);
if (Param->Type == GenericParameterType::Type) {
TemplateTypeParmDecl *TP = TemplateTypeParmDecl::Create(C,
C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(),
0, 0, &IdentTable.get(GP.Name), false, false);
Params.push_back(TP);
} else if (Param->Type == GenericParameterType::Method) {
llvm_unreachable("Method template parameters not supported yet");
}
}
RD->getCLIData()->setGenericData(Data);
TemplateParameterList *TPL = TemplateParameterList::Create(C,
RD->getLocation(), SourceLocation(),
reinterpret_cast<NamedDecl**>(Params.data()), Params.size(),
SourceLocation());
DeclContext *NS = findCreateNamespaces(S, TypeDef);
ClassTemplateDecl *TD = ClassTemplateDecl::Create(C, NS, SourceLocation(),
&II, TPL, RD, /*PrevDecl=*/0);
RD->setDescribedClassTemplate(TD);
RD->setTypeForDecl(0);
QualType T = TD->getInjectedClassNameSpecialization();
T = C.getInjectedClassNameType(RD, T);
return TD;
}
static CLIRecordDecl * createClass(Sema &S, TypeDefinition^ TypeDef) {
ASTContext &C = S.getASTContext();
std::string Name = marshalString<E_UTF8>(TypeDef->Name);
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
IdentifierInfo &II = IdentTable.get(Name);
DeclContext *NS = findCreateNamespaces(S, TypeDef);
if (!NS) return nullptr;
CLIRecordDecl *RD = CLIRecordDecl::Create(C, TTK_RefClass, NS, SourceLocation(),
SourceLocation(), &II, 0);
RD->setCLIData(GetCLIRecordData(S, TypeDef));
RD->startDefinition();
if (TypeDef->HasGenericParameters) {
ClassTemplateDecl *TD = GetCLIClassTemplate(S, TypeDef, RD);
NS->getPrimaryContext()->addDecl(TD);
} else {
NS->getPrimaryContext()->addDecl(RD);
}
return RD;
}
static CLICustomAttribute* createAttribute(Sema &S, CustomAttribute ^AttrDef) {
ASTContext &C = S.getASTContext();
CXXRecordDecl *AttrClass = findCreateClassDecl(S, AttrDef->AttributeType);
if (!AttrClass) return 0;
CXXMethodDecl *AttrCtor = findCreateMethod(S, AttrDef->Constructor->Resolve(),
AttrClass);
CLICustomAttribute *Attr = new(C) CLICustomAttribute(SourceRange(), C,
AttrClass, AttrCtor);
if (AttrDef->HasConstructorArguments) {
for each (CustomAttributeArgument Arg in AttrDef->ConstructorArguments) {
CLICustomAttribute::Argument A;
//A.Expression = Arg.Type;
Attr->Arguments.push_back(A);
}
}
if (AttrDef->HasFields) {
for each (CustomAttributeNamedArgument Arg in AttrDef->Fields) {
CLICustomAttribute::Argument A;
A.Name = marshalString<E_UTF8>(Arg.Name);
//A.Expression = Arg.Type;
Attr->Arguments.push_back(A);
}
}
if (AttrDef->HasProperties) {
for each (CustomAttributeNamedArgument Arg in AttrDef->Properties) {
CLICustomAttribute::Argument A;
A.Name = marshalString<E_UTF8>(Arg.Name);
//A.Expression = Arg.Type;
Attr->Arguments.push_back(A);
}
}
return Attr;
}
static void createDeclAttributes(Sema &S, CustomAttributeCollection ^Attributes,
Decl *D) {
for each (CustomAttribute ^Attr in Attributes) {
CLICustomAttribute *CLIAttr = createAttribute(S, Attr);
D->addAttr(CLIAttr);
}
}
static void createClassBases(Sema &S, TypeDefinition ^TypeDef,
CXXRecordDecl *RD) {
ASTContext &C = S.getASTContext();
if (TypeDef->BaseType != nullptr) {
if (CLIRecordDecl *BRD = findCreateClassDecl(S, TypeDef->BaseType)) {
QualType BaseType = C.getTypeDeclType(BRD);
CXXBaseSpecifier *Base = new (C) CXXBaseSpecifier(SourceRange(), false,
false, AS_public, C.getTrivialTypeSourceInfo(BaseType), SourceLocation());
RD->setBases(&Base, 1);
}
}
for each (TypeReference ^Interface in TypeDef->Interfaces) {
if (CLIRecordDecl *BRD = findCreateClassDecl(S, Interface)) {
QualType BaseType = C.getTypeDeclType(BRD);
CXXBaseSpecifier *Base = new (C) CXXBaseSpecifier(SourceRange(), false,
false, AS_public, C.getTrivialTypeSourceInfo(BaseType), SourceLocation());
RD->getCLIData()->Interfaces.push_back(Base);
}
}
}
static void createClassMethods(Sema &S, TypeDefinition ^TypeDef,
CXXRecordDecl *RD) {
for each (MethodDefinition ^Method in TypeDef->Methods) {
if (String::IsNullOrWhiteSpace(Method->Name))
continue;
if (Method->IsGetter || Method->IsSetter)
continue;
FunctionDecl *FD = findCreateMethod(S, Method, RD);
}
}
static void createClassFields(Sema &S, TypeDefinition ^TypeDef,
CXXRecordDecl *RD) {
for each (FieldDefinition ^Field in TypeDef->Fields) {
if (String::IsNullOrWhiteSpace(Field->Name))
continue;
if (DeclaratorDecl *DD = createField(S, Field, RD))
RD->addDecl(DD);
}
}
static void createClassProperties(Sema &S, TypeDefinition ^TypeDef,
CXXRecordDecl *RD) {
ASTContext &C = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
for each (PropertyDefinition ^PropDef in TypeDef->Properties) {
if (String::IsNullOrWhiteSpace(PropDef->Name))
continue;
std::string Name = marshalString<E_UTF8>(PropDef->Name);
IdentifierInfo &II = IdentTable.get(Name);
DeclarationName DN = C.DeclarationNames.getIdentifier(&II);
QualType PropTy;
if (!findCreateType(S, PropDef->PropertyType, PropTy, RD))
continue;
CLIPropertyDecl *PD = CLIPropertyDecl::Create(S.getASTContext(),
RD->getPrimaryContext(), DN, PropTy);
if (PropDef->HasParameters) {
for each (ParameterDefinition ^Param in PropDef->Parameters) {
QualType IndexTy;
findCreateType(S, Param->ParameterType, IndexTy, RD);
PD->IndexerTypes.push_back(IndexTy);
}
}
PD->setAccess(convertPropertyAccess(PropDef));
if (PropDef->GetMethod)
PD->GetMethod = findCreateMethod(S, PropDef->GetMethod, RD,
/*AddToDecl=*/false);
if (PropDef->SetMethod)
PD->SetMethod = findCreateMethod(S, PropDef->SetMethod, RD,
/*AddToDecl=*/false);
createDeclAttributes(S, PropDef->CustomAttributes, PD);
RD->addDecl(PD);
}
}
static void createClassImplicitOperator(Sema &S, TypeDefinition ^TypeDef,
CXXRecordDecl *RD,
OverloadedOperatorKind Op) {
ASTContext &C = S.getASTContext();
QualType ClassTy = C.getHandleType(C.getRecordType(RD));
llvm::SmallVector<QualType, 1> ParamTypes;
ParamTypes.push_back(ClassTy);
QualType ReturnTy = C.BoolTy;
FunctionProtoType::ExtProtoInfo Info;
QualType FunctionTy = C.getFunctionType(ReturnTy, ParamTypes, Info);
TypeSourceInfo *TSI = C.getTrivialTypeSourceInfo(FunctionTy);
FunctionProtoTypeLoc FTL = TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>();
DeclarationName DN = C.DeclarationNames.getCXXOperatorName(Op);
DeclarationNameInfo DNI(DN, SourceLocation());
CXXMethodDecl *MD = CXXMethodDecl::Create(C, RD, SourceLocation(),
DNI, FunctionTy, TSI, SC_Auto, /*IsInline=*/false,
/*IsConstexpr=*/false, SourceLocation());
MD->setImplicit(true);
llvm::SmallVector<StringRef, 1> ParamNames;
ParamNames.push_back("rhs");
llvm::SmallVector<ParmVarDecl*, 2> ParamDecls;
for (unsigned i = 0; i < ParamNames.size(); ++i) {
StringRef ParamName = ParamNames[i];
IdentifierInfo &PII = S.getPreprocessor().getIdentifierTable().
get(ParamName);
QualType ParamType = ClassTy;
ParmVarDecl *PVD = ParmVarDecl::Create(C, MD, SourceLocation(),
SourceLocation(), &PII, ParamType,
C.getTrivialTypeSourceInfo(ParamType), SC_Auto, 0);
assert(PVD && "Expected a valid parameter decl");
PVD->setScopeInfo(0, i);
FTL.setArg(i++, PVD);
ParamDecls.push_back(PVD);
}
MD->setParams(ParamDecls);
RD->addDecl(MD);
}
static void createClassImplicitOperators(Sema &S, TypeDefinition ^TypeDef,
CXXRecordDecl *RD) {
// 15.8.1 Handle equality operators
// Every ref class type and value class type C implicitly provides the
// following predefined equality operators:
//
// bool operator ==(C^ x, C^ y);
// bool operator !=(C^ x, C^ y);
createClassImplicitOperator(S, TypeDef, RD, OO_EqualEqual);
createClassImplicitOperator(S, TypeDef, RD, OO_ExclaimEqual);
}
static void createClassDecls(Sema &S, TypeDefinition ^TypeDef, CXXRecordDecl *RD) {
createClassBases(S, TypeDef, RD);
createClassMethods(S, TypeDef, RD);
createClassFields(S, TypeDef, RD);
createClassProperties(S, TypeDef, RD);
createClassImplicitOperators(S, TypeDef, RD);
RD->completeDefinition();
}
static CXXRecordDecl * findCreateClassDecl(Sema &S, TypeDefinition ^TypeDef) {
if (!TypeDef) return 0;
DeclContext *NS = findCreateNamespaces(S, TypeDef);
if (!NS) return 0;
IdentifierInfo *II = getIdentifier(S, TypeDef->Name);
DeclContextLookupResult Res = NS->lookup(II);
for (auto it = Res.begin(); it != Res.end(); ++it) {
Decl *D = *it;
if (CLIRecordDecl *RD = dyn_cast<CLIRecordDecl>(D)) {
return RD;
} else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(D)) {
CXXRecordDecl *TD = CTD->getTemplatedDecl();
if (TD->isCLIRecord())
return TD;
}
}
// If we do not find the type, then it has not been found yet and we need
// to create it explicitly.
CXXRecordDecl *RD = createClass(S, TypeDef);
createClassDecls(S, TypeDef, RD);
return RD;
}
static CXXRecordDecl * findCreateClassDecl(Sema &S, TypeReference ^TypeRef) {
TypeDefinition ^TypeDef = TypeRef->Resolve();
return findCreateClassDecl(S, TypeDef);
}
static bool convertPrimitiveType(Sema &S, TypeReference ^TypeRef, QualType &Type) {
ASTContext& C = S.getASTContext();
TypeDefinition ^TypeDef = TypeRef->Resolve();
if (!TypeDef)
return false;
int Hash = llvm::HashString(marshalString<E_UTF8>(TypeDef->FullName));
CLIPrimitiveTypes &P = S.getCLIContext()->Types;
if (Hash == P.Void.Hash) {
Type = C.VoidTy;
return true;
} else if (Hash == P.Boolean.Hash) {
Type = C.BoolTy;
return true;
} else if (Hash == P.Char.Hash) {
Type = C.WCharTy;
return true;
} else if (Hash == P.Byte.Hash) {
Type = C.UnsignedCharTy;
return true;
} else if (Hash == P.SByte.Hash) {
// FIXME: Check for modopt IsSignUnspecifiedByte
Type = C.CharTy;
return true;
} else if (Hash == P.Int16.Hash) {
Type = C.ShortTy;
return true;
} else if (Hash == P.UInt16.Hash) {
Type = C.UnsignedShortTy;
return true;
} else if (Hash == P.Int32.Hash) {
// FIXME: Check for modopt IsLong
Type = C.IntTy;
return true;
} else if (Hash == P.UInt32.Hash) {
// FIXME: Check for modopt IsLong
Type = C.UnsignedIntTy;
return true;
} else if (Hash == P.Int64.Hash) {
Type = C.LongLongTy;
return true;
} else if (Hash == P.UInt64.Hash) {
Type = C.UnsignedLongLongTy;
return true;
} else if (Hash == P.Single.Hash) {
Type = C.FloatTy;
return true;
} else if (Hash == P.Double.Hash) {
// FIXME: Check for modopt IsLong
Type = C.DoubleTy;
return true;
} else if (Hash == P.String.Hash) {
Type = P.String.Ty;
return true;
} else if (Hash == P.IntPtr.Hash) {
Type = P.IntPtr.Ty;
return true;
} else if (Hash == P.UIntPtr.Hash) {
Type = P.UIntPtr.Ty;
return true;
}
return false;
}
static void createClassGenericParameters(Sema &S, CXXRecordDecl *RD,
GenericInstanceType ^Type) {
for each(GenericParameter ^Param in Type->GenericParameters) {
std::string Name = marshalString<E_UTF8>(Param->Name);
//Console::WriteLine("{0} {1}", Param->FullName, Param->Name);
continue;
}
for each(TypeReference ^Arg in Type->GenericArguments) {
std::string Name = marshalString<E_UTF8>(Arg->Name);
//Console::WriteLine("{0} {1}", Arg->FullName, Arg->Name);
continue;
}
// Demangle the CLI generic name.
//std::string Name = marshalString<E_UTF8>(Type->Name);
//Name = Name.substr(0, Name.find("`"));
}
static bool findCreateType(Sema &S, TypeReference ^TypeRef, QualType &Type,
CXXRecordDecl *MethodRecord) {
if (convertPrimitiveType(S, TypeRef, Type))
return true;
ASTContext &Context = S.getASTContext();
if (TypeRef->IsArray) {
Mono::Cecil::ArrayType ^Arr = safe_cast<Mono::Cecil::ArrayType^>(TypeRef);
QualType ElementType;
if (!findCreateType(S, Arr->ElementType, ElementType, MethodRecord))
return false;
CXXRecordDecl *RD = findCreateClassDecl(S, Arr->ElementType);
Type = Context.getHandleType(Context.getCLIArrayType(ElementType,
Arr->Rank, S.getCLIContext()->Types.Array.Decl));
return true;
} else if (TypeRef->IsGenericParameter) {
assert(MethodRecord && "Expected a valid C++/CLI record");
ClassTemplateDecl *TD = MethodRecord->getDescribedClassTemplate();
if (!TD) {
Type = Context.VoidTy;
return false;
}
assert(TD && "Expected a valid class template");
std::string Name = marshalString<E_UTF8>(TypeRef->Name);
TemplateParameterList *TPL = TD->getTemplateParameters();
for (unsigned I = 0, E = TPL->size(); I != E; ++I) {
NamedDecl *TP = TPL->getParam(I);
TemplateTypeParmDecl *TT = dyn_cast<TemplateTypeParmDecl>(TP);
assert(TT && "Expected a template type parameter");
if (TP->getName() == Name) {
Type = Context.getTemplateTypeParmType(0, I, 0, TT);
return true;
}
}
} else {
if (CXXRecordDecl *RD = findCreateClassDecl(S, TypeRef)) {
if (TypeRef->IsGenericInstance) {
GenericInstanceType ^Instance = safe_cast<GenericInstanceType^>(TypeRef);
createClassGenericParameters(S, RD, Instance);
Type = Context.getHandleType(Context.getRecordType(RD));
return true;
}
Type = Context.getHandleType(Context.getRecordType(RD));
return true;
}
}
//llvm_unreachable("Unhandled managed type");
Type = Context.VoidTy;
return false;
}
static void initializeCLIType(Sema &S, CLIPrimitiveType &P, CLITypeKind Kind,
TypeDefinition ^Type) {
using namespace clix;
P.Hash = llvm::HashString(marshalString<E_UTF8>(Type->FullName));
P.Token = Type->MetadataToken.ToUInt32();
P.Decl = createClass(S, Type);
P.Decl->getCLIData()->Kind = Kind;
QualType RT = S.getASTContext().getRecordType(P.Decl);
if (Type->IsValueType)
P.Ty = RT;
else
P.Ty = S.getASTContext().getHandleType(RT);
}
static void initializeCLIClass(Sema &S, CLIPrimitiveType &P,
TypeDefinition ^Type) {
createClassDecls(S, Type, P.Decl);
}
static void initializeCLITypes(Sema &S, AssemblyDefinition^ Assembly) {
ModuleDefinition ^Module = Assembly->MainModule;
CLIPrimitiveTypes &P = S.getCLIContext()->Types;
// Get the metadata tokens for each type so we can look it up later.
#define CLI_TYPE(X) \
initializeCLIType(S, P.X, CLI_TK_None, Module->GetType("System."#X));
#define CLI_PRITIMIVE_TYPE(X) \
initializeCLIType(S, P.X, CLI_TK_##X, Module->GetType("System."#X));
#include "clang/AST/CLITypes.def"
#undef CLI_TYPE
#undef CLI_PRITIMIVE_TYPE
#define CLI_TYPE(X) \
initializeCLIClass(S, P.X, Module->GetType("System."#X));
#include "clang/AST/CLITypes.def"
#undef CLI_TYPE
CLISemaContext &Ctx = *S.getCLIContext();
Ctx.DefaultMemberAttribute = findCreateClassDecl(S,
Module->GetType("System.Reflection.DefaultMemberAttribute"));
Ctx.ParamArrayAttribute = findCreateClassDecl(S,
Module->GetType("System.ParamArrayAttribute"));
Ctx.IEnumerable = findCreateClassDecl(S,
Module->GetType("System.Collections.IEnumerable"));
Ctx.GenericIEnumerable = findCreateClassDecl(S,
Module->GetType("System.Collections.Generic.IEnumerable`1"));
}
static NamespaceDecl *initializeNamespaceDecl(Sema &S) {
ASTContext &ASTContext = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
TranslationUnitDecl *TU = ASTContext.getTranslationUnitDecl();
NamespaceDecl *Namespace = NamespaceDecl::Create(ASTContext,
TU->getPrimaryContext(), /*IsInline=*/false, SourceLocation(),
SourceLocation(), &IdentTable.get("cli"), 0);
Namespace->setImplicit(true);
TU->addDecl(Namespace);
return Namespace;
}
static ClassTemplateDecl *initializeCLIArrayDecl(Sema &S,
NamespaceDecl *Namespace) {
ASTContext &ASTContext = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
CXXRecordDecl *RD = CXXRecordDecl::Create(ASTContext, TTK_RefClass,
Namespace->getPrimaryContext(), SourceLocation(), SourceLocation(),
&IdentTable.get("array"), 0, false);
RD->startDefinition();
RD->completeDefinition();
// Set System::Array as the base class.
QualType BaseType = ASTContext.getTypeDeclType(S.getCLIContext()->
Types.Array.Decl);
CXXBaseSpecifier *Base = new (ASTContext) CXXBaseSpecifier(SourceRange(),
false, false, AS_public, ASTContext.getTrivialTypeSourceInfo(BaseType),
SourceLocation());
RD->setBases(&Base, 1);
TemplateTypeParmDecl *TT = TemplateTypeParmDecl::Create(ASTContext,
RD->getPrimaryContext(), SourceLocation(), SourceLocation(),
0, 0, &IdentTable.get("T"), /*Typename=*/true, /*ParameterPack=*/false);
NonTypeTemplateParmDecl *NT = NonTypeTemplateParmDecl::Create(ASTContext,
RD->getPrimaryContext(), SourceLocation(), SourceLocation(),
0, 0, &IdentTable.get("Rank"), ASTContext.UnsignedIntTy, false,
ASTContext.getTrivialTypeSourceInfo(ASTContext.UnsignedIntTy));
NT->setDefaultArgument(IntegerLiteral::Create(ASTContext,
llvm::APInt(32, 1), ASTContext.UnsignedIntTy, SourceLocation()), false);
NamedDecl *TemplateDecls[] = { TT, NT };
TemplateParameterList *TPL = TemplateParameterList::Create(ASTContext,
SourceLocation(), SourceLocation(), TemplateDecls, 2, SourceLocation());
ClassTemplateDecl *ClassTemplate =
ClassTemplateDecl::Create(ASTContext, Namespace->getPrimaryContext(),
SourceLocation(), DeclarationName(&IdentTable.get("array")), TPL,
RD, 0);
Namespace->addDecl(ClassTemplate);
return ClassTemplate;
}
static ClassTemplateDecl *initializeInteriorPtrDecl(Sema &S,
NamespaceDecl *Namespace) {
ASTContext &ASTContext = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
CXXRecordDecl *RD = CXXRecordDecl::Create(ASTContext, TTK_Class,
Namespace->getPrimaryContext(), SourceLocation(), SourceLocation(),
&IdentTable.get("interior_ptr"), 0, false);
TemplateTypeParmDecl *TT = TemplateTypeParmDecl::Create(ASTContext,
RD->getPrimaryContext(), SourceLocation(), SourceLocation(),
0, 0, &IdentTable.get("T"), /*Typename=*/true, false);
NamedDecl *TemplateDecls[] = { TT };
TemplateParameterList *TPL = TemplateParameterList::Create(ASTContext,
SourceLocation(), SourceLocation(), TemplateDecls, 1, SourceLocation());
ClassTemplateDecl *ClassTemplate =
ClassTemplateDecl::Create(ASTContext, Namespace->getPrimaryContext(),
SourceLocation(), DeclarationName(&IdentTable.get("interior_ptr")), TPL,
RD, 0);
Namespace->addDecl(ClassTemplate);
return ClassTemplate;
}
static ClassTemplateDecl *initializePinPtrDecl(Sema &S,
NamespaceDecl *Namespace) {
ASTContext &ASTContext = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
CXXRecordDecl *RD = CXXRecordDecl::Create(ASTContext, TTK_Class,
Namespace->getPrimaryContext(), SourceLocation(), SourceLocation(),
&IdentTable.get("pin_ptr"), 0, false);
TemplateTypeParmDecl *TT = TemplateTypeParmDecl::Create(ASTContext,
RD->getPrimaryContext(), SourceLocation(), SourceLocation(),
0, 0, &IdentTable.get("T"), /*Typename=*/true, false);
NamedDecl *TemplateDecls[] = { TT };
TemplateParameterList *TPL = TemplateParameterList::Create(ASTContext,
SourceLocation(), SourceLocation(), TemplateDecls, 1, SourceLocation());
ClassTemplateDecl *ClassTemplate =
ClassTemplateDecl::Create(ASTContext, Namespace->getPrimaryContext(),
SourceLocation(), DeclarationName(&IdentTable.get("pin_ptr")), TPL,
RD, 0);
Namespace->addDecl(ClassTemplate);
return ClassTemplate;
}
static ClassTemplateDecl *initializeSafeCastDecl(Sema &S,
NamespaceDecl *Namespace) {
ASTContext &ASTContext = S.getASTContext();
IdentifierTable &IdentTable = S.getPreprocessor().getIdentifierTable();
CXXRecordDecl *RD = CXXRecordDecl::Create(ASTContext, TTK_Class,
Namespace->getPrimaryContext(), SourceLocation(), SourceLocation(),
&IdentTable.get("safe_cast"), 0, false);
TemplateTypeParmDecl *TT = TemplateTypeParmDecl::Create(ASTContext,
RD->getPrimaryContext(), SourceLocation(), SourceLocation(),
0, 0, &IdentTable.get("T"), /*Typename=*/true, false);
NamedDecl *TemplateDecls[] = { TT };
TemplateParameterList *TPL = TemplateParameterList::Create(ASTContext,
SourceLocation(), SourceLocation(), TemplateDecls, 1, SourceLocation());
ClassTemplateDecl *ClassTemplate =
ClassTemplateDecl::Create(ASTContext, Namespace->getPrimaryContext(),
SourceLocation(), DeclarationName(&IdentTable.get("safe_cast")), TPL,
RD, 0);
Namespace->addDecl(ClassTemplate);
return ClassTemplate;
}
static void initializeCLINamespace(Sema &S) {
CLISemaContext *Ctx = S.getCLIContext();
assert(!Ctx->CLINamespace && "Expected unitialized CLI namespace");