This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
CppCliReflector.cs
3772 lines (3537 loc) · 144 KB
/
CppCliReflector.cs
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
///////////////////////////////////////////////////////////////////////////////
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Reflector;
using Reflector.CodeModel;
using System.Globalization;
namespace Reflector.Languages
{
// TODO:
// generic<> should only appear on the individual methods, not foreach method within a class
// WriteType should use a GetName syntax.
// using (int i) {} does not work.
//
class CppCliLanguagePackage : IPackage
{
public virtual void Load(IServiceProvider serviceProvider)
{
languageManager = serviceProvider.GetService(typeof(ILanguageManager)) as ILanguageManager;
cppCliLanguage = new CppCliLanguage();
languageManager.RegisterLanguage(cppCliLanguage);
}
public virtual void Unload()
{
languageManager.UnregisterLanguage(cppCliLanguage);
}
ILanguageManager languageManager;
CppCliLanguage cppCliLanguage;
};
struct MethodNameExt
{
string N;
string OriginalN;
ICollection O;
public bool Constructor;
public bool StaticConstructor;
public bool ImplicitOrExplicit;
public bool Explicit;
public MethodNameExt(string Name, ICollection Overrides, string TypeName)
{
Constructor = false;
StaticConstructor = false;
ImplicitOrExplicit = false;
Explicit = false;
if (Name == "op_Explicit")
{
ImplicitOrExplicit = true;
Explicit = true;
}
else if (Name == "op_Implicit")
{
ImplicitOrExplicit = true;
}
else if (Name == ".ctor")
{
Name = TypeName;
Constructor = true;
}
else if (Name == ".cctor")
{
Name = TypeName;
StaticConstructor = true;
}
OriginalN = Name;
O = Overrides;
N = null;
}
public string Name
{
get
{
if (N==null)
{
N = OriginalN;
if (!Constructor)
{
if (O==null || O.Count > 0)
{
N = N.Replace(".", "_"); // C# explicit overrides have names that can't be used in C++, so we tweak them here
}
string name;
if (CppCliLanguage.LanguageWriter.specialMethodNames.TryGetValue(N, out name))
{
N = name;
}
}
}
return N;
}
}
}
class CppCliLanguage : ILanguage
{
// Methods
internal CppCliLanguage()
{
}
public ILanguageWriter GetWriter(IFormatter formatter, ILanguageWriterConfiguration configuration)
{
return new LanguageWriter(formatter, configuration);
}
public string FileExtension { get { return "cpp"; } }
public string Name { get { return "C++/CLI"; } }
public bool Translate { get { return true; } }
internal class LanguageWriter : ILanguageWriter
{
static internal Dictionary<string, string> specialMethodNames;
static internal Dictionary<string, string> specialTypeNames;
internal Hashtable refOnStack; //
bool SuppressOutput; //
bool SomeConstructor; //
string[] ExtraTemporaries; //extra temporaries
string[] ExtraMappings; //what they are mapped to.
bool[] EssentialTemporaries; //we need these.
int[] TemporaryBlocks; //where we saw them
int Block; //block number for extra temporaries
int NextBlock; //the next block we see.
ILanguageWriterConfiguration configuration;
IFormatter formatter;
ITypeReference baseType;
int SkipTryCount; //count of elements in Try block we handled already
int SkipNullptrCount;
bool SkipWriteLine; //don't need a writeline, like inside a for
static LanguageWriter()
{
specialTypeNames = new Dictionary<string, string>();
specialTypeNames["Void"] = "void";
specialTypeNames["Boolean"] = "bool";
specialTypeNames["Char"] = "wchar_t";
specialTypeNames["SByte"] = "char";
specialTypeNames["Byte"] = "unsigned char";
specialTypeNames["Int16"] = "short";
specialTypeNames["ushort"] = "ushort";
specialTypeNames["Int32"] = "int";
specialTypeNames["UInt32"] = "uint";
specialTypeNames["Int64"] = "long";
specialTypeNames["UInt64"] = "ulong";
specialTypeNames["Single"] = "float";
specialTypeNames["Double"] = "double";
specialMethodNames = new Dictionary<string, string>();
// CLS-compliant unary operators
specialMethodNames["op_AddressOf"] = "operator & ";
specialMethodNames["op_LogicalNot"] = "operator ! ";
specialMethodNames["op_OnesComplement"] = "operator ~ ";
specialMethodNames["op_PointerDereference"] = "operator * ";
specialMethodNames["op_UnaryNegation"] = "operator - ";
specialMethodNames["op_UnaryPlus"] = "operator + ";
// CLS-Compliant binary operators
specialMethodNames["op_MemberSelection"] = "operator . ";
specialMethodNames["op_Addition"] = "operator + ";
specialMethodNames["op_BitwiseAnd"] = "operator & ";
specialMethodNames["op_BitwiseOr"] = "operator | ";
specialMethodNames["op_ExclusiveOr"] = "operator ";
specialMethodNames["op_Increment"] = "operator ++ ";
specialMethodNames["op_Subtraction"] = "operator - ";
specialMethodNames["op_Decrement"] = "operator -- ";
specialMethodNames["op_Multiply"] = "operator * ";
specialMethodNames["op_Division"] = "operator / ";
specialMethodNames["op_Modulus"] = "operator % ";
specialMethodNames["op_Negation"] = "operator ! ";
specialMethodNames["op_LeftShift"] = "operator << ";
specialMethodNames["op_RightShift"] = "operator >> ";
specialMethodNames["op_Equality"] = "operator == ";
specialMethodNames["op_Inequality"] = "operator != ";
specialMethodNames["op_GreaterThanOrEqual"] = "operator >= ";
specialMethodNames["op_LessThanOrEqual"] = "operator <= ";
specialMethodNames["op_GreaterThan"] = "operator > ";
specialMethodNames["op_LessThan"] = "operator < ";
specialMethodNames["op_True"] = "operator true ";
specialMethodNames["op_False"] = "operator false ";
specialMethodNames["op_Implicit"] = "implicit ";
specialMethodNames["op_Explicit"] = "explicit ";
}
internal LanguageWriter(IFormatter formatter, ILanguageWriterConfiguration configuration)
{
this.formatter = formatter;
this.configuration = configuration;
}
public void WriteAssembly(IAssembly assembly)
{
this.Write("// Assembly");
this.Write(" ");
this.WriteDeclaration(assembly.Name);
if (assembly.Version != null)
{
this.Write(", ");
this.Write("Version");
this.Write(" ");
this.Write(assembly.Version.ToString());
}
this.WriteLine();
if ((this.configuration["ShowCustomAttributes"] == "true") && (assembly.Attributes.Count != 0))
{
this.WriteLine();
//this.WriteCustomAttributeCollection(this.formatter, assembly);
this.WriteLine();
}
this.WriteProperty("Location", assembly.Location);
this.WriteProperty("Name", assembly.ToString());
}
public void WriteAssemblyReference(IAssemblyReference assemblyReference)
{
this.Write("// Assembly Reference");
this.Write(" ");
this.WriteDeclaration(assemblyReference.Name);
this.WriteLine();
this.WriteProperty("Version", assemblyReference.Version.ToString());
this.WriteProperty("Name", assemblyReference.ToString());
}
void WriteExpressionCollection(IExpressionCollection iExpressionCollection)
{
string separator = "";
foreach (IExpression iExpression in iExpressionCollection)
{
this.Write(separator);
this.WriteExpression(iExpression);
separator = ", ";
}
}
void TypeWriter(StringWriter writer, IType iType)
{
writer.Write(GetNameFromType(iType));
}
string GetNameFromTypeReference(ITypeReference iTypeReference)
{
string s = iTypeReference.Name;
string s1;
if (iTypeReference.Namespace == "System" && specialTypeNames.TryGetValue(s, out s1))
{
s = s1;
}
ITypeCollection genericParameters = iTypeReference.GenericArguments;
if (genericParameters.Count > 0)
{
string separator = "";
s += "<";
foreach(IType iType in genericParameters)
{
s += separator;
separator = ",";
s += GetNameFromType(iType);
}
s += ">";
}
return s.Replace(".", ".").Replace("+", ".");
}
string GetNameFromType(IType iType)
{
IOptionalModifier iOptionalModifier = (iType) as IOptionalModifier;
if (iOptionalModifier != null)
{
string s = GetNameFromType(iOptionalModifier.ElementType);
if (iOptionalModifier.Modifier.Name == "IsLong" && iOptionalModifier.Modifier.Namespace == "System.Runtime.CompilerServices")
{
if (s == "int")
{
s = "long";
}
}
else
{
s += " " + iOptionalModifier.Modifier.ToString();
}
return s;
}
ITypeReference iTypeReference = (iType) as ITypeReference;
if (iTypeReference != null)
{
return GetNameFromTypeReference(iTypeReference);
}
return iType.ToString();
}
void WriteTypeReference(ITypeReference typeReference)
{
string name = GetNameFromTypeReference(typeReference);
string s = typeReference.Namespace + "." + typeReference.Name;
s = s.Replace(".", ".").Replace("+", ".");
this.WriteReference(name, s, typeReference);
}
void WriteType(IType type)
{
WriteType<object>(type, null, null, null, false);
}
void WriteType<T>(IType type, Action<T> callback, T middle, ICustomAttributeProvider iCustomAttributeProvider, bool fRefOnStack)
{
if ((this.configuration["ShowCustomAttributes"] == "true") && iCustomAttributeProvider != null)
{
this.WriteCustomAttributeCollection(iCustomAttributeProvider, type);
}
ITypeReference tagType;
IArrayType arrayType;
IPointerType pointerType;
IReferenceType refType;
IOptionalModifier modoptType;
IRequiredModifier modreqType;
IFunctionPointer funcPtrType;
IGenericParameter genericParam;
IGenericArgument genericArg;
IValueTypeConstraint iValueTypeConstraint;
IDefaultConstructorConstraint iDefaultConstructorConstraint;
if ((tagType = type as ITypeReference) != null)
{
this.WriteTypeReference(tagType);
if (!Helper.IsValueType(tagType) && !fRefOnStack)
{
this.Write("");
}
}
else if ((arrayType = type as IArrayType) != null)
{
this.Write("array<");
this.WriteType(arrayType.ElementType);
if (arrayType.Dimensions.Count > 1)
{
this.Write(", " + arrayType.Dimensions.Count.ToString());
}
this.Write(">");
}
else if ((pointerType = type as IPointerType) != null)
{
this.WriteType(pointerType.ElementType);
this.Write("*");
}
else if ((refType = type as IReferenceType) != null)
{
this.WriteType(refType.ElementType);
this.Write("%");
}
#if false
else if ((pinType = type as IPinnedType) != null)
{
this.Write("pin_ptr<");
this.WriteType(pinType.ElementType);
this.Write(">");
}
#endif
else if ((modoptType = type as IOptionalModifier) != null)
{
WriteModifiedType(modoptType.Modifier, modoptType.ElementType, false, fRefOnStack);
}
else if ((modreqType = type as IRequiredModifier) != null)
{
WriteModifiedType(modreqType.Modifier, modreqType.ElementType, true, fRefOnStack);
}
else if ((funcPtrType = type as IFunctionPointer) != null)
{
this.Write("funcptr");
/*
this.WriteType(pointer1.ReturnType.Type, formatter, false);
this.Write(" *(");
for (int num2 = 0; num2 < pointer1.Parameters.Count; num2++)
{
if (num2 != 0)
{
this.Write(", ");
}
this.WriteType(pointer1.Parameters[num2].ParameterType, formatter, false);
}
this.Write(")");
*/
}
else if ((genericParam = type as IGenericParameter) != null)
{
this.Write(genericParam.Name);
}
else if ((genericArg = type as IGenericArgument) != null)
{
this.WriteType(genericArg.Resolve());
}
else if ((iValueTypeConstraint = type as IValueTypeConstraint) != null)
{
this.WriteKeyword("value");
this.WriteKeyword(" ");
this.WriteKeyword("class");
}
else if ((iDefaultConstructorConstraint = type as IDefaultConstructorConstraint) != null)
{
this.Write("TODO #1");
}
else
{
this.Write("WHAT TYPE IS IT?");
}
if (callback != null)
{
this.Write(" ");
callback(middle);
}
}
public void WriteExpression(IExpression expression)
{
if (expression==null)
{
return;
}
IPropertyIndexerExpression iPropertyIndexerExpression;
IDelegateCreateExpression iDelegateCreateExpression;
ITypeOfExpression iTypeOfExpression;
ISnippetExpression iSnippetExpression;
IArrayIndexerExpression iArrayIndexerExpression;
//IObjectInitializeExpression iObjectInitializeExpression;
IAddressDereferenceExpression iAddressDereferenceExpression;
IAddressOfExpression iAddressOfExpression;
IAddressOutExpression iAddressOutExpression;
IAddressReferenceExpression iAddressReferenceExpression;
IArgumentListExpression iArgumentListExpression;
IUnaryExpression iUnaryExpression;
IBinaryExpression iBinaryExpression;
IBaseReferenceExpression iBaseReferenceExpression;
ITypeReferenceExpression iTypeReferenceExpression;
IObjectCreateExpression iObjectCreateExpression;
ICastExpression iCastExpression;
ILiteralExpression literalExpression;
IMethodInvokeExpression iMethodInvokeExpression;
IMethodReferenceExpression iMethodReferenceExpression;
IVariableDeclarationExpression iVariableDeclarationExpression;
IVariableReferenceExpression iVariableReferenceExpression;
IArgumentReferenceExpression iArgumentReferenceExpression;
//IArrayInitializerExpression iArrayInitializerExpression;
IThisReferenceExpression iThisReferenceExpression;
IPropertyReferenceExpression iPropertyReferenceExpression;
IArrayCreateExpression iArrayCreateExpression;
IConditionExpression iConditionExpression;
IFieldReferenceExpression iFieldReferenceExpression;
//INamedArgumentExpression iNamedArgumentExpression;
#if false
IStatementExpression iStatementExpression;
#endif
ICanCastExpression iCanCastExpression;
ITryCastExpression iTryCastExpression;
IAssignExpression iAssignExpression;
if ((iPropertyIndexerExpression = expression as IPropertyIndexerExpression) != null)
{
WritePropertyIndexerExpression(iPropertyIndexerExpression);
return;
}
if ((iDelegateCreateExpression = expression as IDelegateCreateExpression) != null)
{
WriteDelegateCreateExpression(iDelegateCreateExpression);
return;
}
if ((iTypeOfExpression = expression as ITypeOfExpression) != null)
{
WriteTypeOfExpression(iTypeOfExpression);
return;
}
if ((iSnippetExpression = expression as ISnippetExpression) != null)
{
WriteSnippetExpression(iSnippetExpression);
return;
}
if ((iArrayIndexerExpression = expression as IArrayIndexerExpression) != null)
{
WriteArrayIndexerExpression(iArrayIndexerExpression);
return;
}
//if ((iObjectInitializeExpression = expression as IObjectInitializeExpression) != null)
//{
// WriteObjectInitializeExpression(iObjectInitializeExpression);
// return;
//}
if ((iAddressDereferenceExpression = expression as IAddressDereferenceExpression) != null)
{
WriteAddressDereferenceExpression(iAddressDereferenceExpression);
return;
}
if ((iAddressOfExpression = expression as IAddressOfExpression) != null)
{
WriteAddressOfExpression(iAddressOfExpression);
return;
}
if ((iAddressOutExpression = expression as IAddressOutExpression) != null)
{
WriteAddressOutExpression(iAddressOutExpression);
return;
}
if ((iAddressReferenceExpression = expression as IAddressReferenceExpression) != null)
{
WriteAddressReferenceExpression(iAddressReferenceExpression);
return;
}
if ((iArgumentListExpression = expression as IArgumentListExpression) != null)
{
WriteArgumentListExpression(iArgumentListExpression);
return;
}
if ((iUnaryExpression = expression as IUnaryExpression) != null)
{
WriteUnaryExpression(iUnaryExpression);
return;
}
if ((iBinaryExpression = expression as IBinaryExpression) != null)
{
WriteBinaryExpression(iBinaryExpression);
return;
}
if ((iBaseReferenceExpression = expression as IBaseReferenceExpression) != null)
{
WriteBaseReferenceExpression(iBaseReferenceExpression);
return;
}
if ((iTypeReferenceExpression = expression as ITypeReferenceExpression) != null)
{
WriteTypeReferenceExpression(iTypeReferenceExpression);
return;
}
if ((iObjectCreateExpression = expression as IObjectCreateExpression) != null)
{
WriteObjectCreateExpression(iObjectCreateExpression);
return;
}
if ((iCastExpression = expression as ICastExpression) != null)
{
WriteCastExpression(iCastExpression);
return;
}
if ((literalExpression = expression as ILiteralExpression) != null)
{
WriteLiteralExpression(literalExpression);
return;
}
if ((iMethodInvokeExpression = expression as IMethodInvokeExpression) != null)
{
WriteMethodInvokeExpression(iMethodInvokeExpression);
return;
}
if ((iMethodReferenceExpression = expression as IMethodReferenceExpression) != null)
{
WriteMethodReferenceExpression(iMethodReferenceExpression);
return;
}
if ((iVariableDeclarationExpression = expression as IVariableDeclarationExpression) != null)
{
WriteVariableDeclaration(iVariableDeclarationExpression.Variable);
return;
}
if ((iVariableReferenceExpression = expression as IVariableReferenceExpression) != null)
{
WriteVariableReferenceExpression(iVariableReferenceExpression);
return;
}
if ((iArgumentReferenceExpression = expression as IArgumentReferenceExpression) != null)
{
WriteArgumentReferenceExpression(iArgumentReferenceExpression);
return;
}
//if ((iArrayInitializerExpression = expression as IArrayInitializerExpression) != null)
//{
// WriteArrayInitializerExpression(iArrayInitializerExpression);
// return;
//}
if ((iThisReferenceExpression = expression as IThisReferenceExpression) != null)
{
WriteThisReferenceExpression(iThisReferenceExpression);
return;
}
if ((iPropertyReferenceExpression = expression as IPropertyReferenceExpression) != null)
{
WritePropertyReferenceExpression(iPropertyReferenceExpression);
return;
}
if ((iArrayCreateExpression = expression as IArrayCreateExpression) != null)
{
WriteArrayCreateExpression(iArrayCreateExpression);
return;
}
if ((iConditionExpression = expression as IConditionExpression) != null)
{
WriteConditionExpression(iConditionExpression);
return;
}
if ((iFieldReferenceExpression = expression as IFieldReferenceExpression) != null)
{
WriteFieldReferenceExpression(iFieldReferenceExpression);
return;
}
//if ((iNamedArgumentExpression = expression as INamedArgumentExpression) != null)
//{
// WriteNamedArgumentExpression(iNamedArgumentExpression);
// return;
//}
#if false
if ((iStatementExpression = expression as IStatementExpression) != null)
{
WriteStatementExpression(iStatementExpression);
return;
}
#endif
if ((iCanCastExpression = expression as ICanCastExpression) != null)
{
WriteCanCastExpression(iCanCastExpression);
return;
}
if ((iTryCastExpression = expression as ITryCastExpression) != null)
{
WriteTryCastExpression(iTryCastExpression);
return;
}
if ((iAssignExpression = expression as IAssignExpression) != null)
{
WriteAssignExpression(iAssignExpression);
return;
}
// we already checked for expression==null above
this.Write(expression.ToString());
}
void WriteLiteralExpression(ILiteralExpression literalExpression)
{
Object value = literalExpression.Value;
if (value is bool)
{
bool val = (bool)(value);
this.WriteLiteral(val ? "true" : "false");
return;
}
if (value is char)
{
this.Write("'");
this.WriteOneChar((char)value);
this.Write("'");
return;
}
if (value is byte)
{
byte num = (byte)(value);
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture));
return;
}
if (value is sbyte)
{
sbyte num = (sbyte)(value);
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture));
return;
}
if (value is short)
{
short num = (short)(value);
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture));
return;
}
if (value is ushort)
{
ushort num = (ushort)(value);
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture));
return;
}
if (value is int)
{
int num = (int)(value);
uint u = (uint)num;
if (u>= 0x80000000 && u<=0x8fffffff) //Error codes as hex
{
this.WriteLiteral("0x" + num.ToString("X8"));
}
else
{
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture));
}
return;
}
if (value is uint)
{
uint u = (uint)(value);
if (u>= 0x80000000) //probably hex
{
this.WriteLiteral("0x" + u.ToString("X8"));
}
else
{
this.WriteLiteral(u.ToString(CultureInfo.InvariantCulture));
}
return;
}
if (value is long)
{
long num = (long)(value);
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture));
return;
}
if (value is ulong)
{
ulong num = (ulong)(value);
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture));
return;
}
if (value is float)
{
float num = (float)(value);
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture)+"f");
return;
}
if (value is double)
{
double num = (double)(value);
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture));
return;
}
if (value is decimal)
{
decimal num = (decimal)(value);
this.WriteLiteral(num.ToString(CultureInfo.InvariantCulture));
return;
}
if (value == null)
{
this.WriteLiteral("null");
return;
}
string s = value as string;
if (s != null)
{
this.Write("\"");
foreach(char w in s)
{
this.WriteOneChar(w);
}
this.Write("\"");
return;
}
if (value is byte[])
{
byte[] a = (byte[])value;
string separator="{ ";
for (int num1 = 0; num1 < a.Length; num1++)
{
this.Write(separator);
this.WriteLiteral("0x" + a[num1].ToString("X2"));
separator=",";
}
this.Write(" }");
return;
}
this.Write("Literal expression type NYI");
}
void WriteOneChar(char w)
{
if ((uint)w < 0x80)
{
string toWrite;
switch (w)
{
case '\0': toWrite = "\\0"; break;
case '\r': toWrite = "\\r"; break;
case '\n': toWrite = "\\n"; break;
case '\t': toWrite = "\\t"; break;
case '\v': toWrite = "\\v"; break;
case '\b': toWrite = "\\b"; break;
default:
toWrite = w.ToString(CultureInfo.InvariantCulture);
break;
}
this.WriteLiteral(toWrite);
}
else
{
uint num = (uint)w;
this.WriteLiteral("\\u" + num.ToString("X4"));
}
}
// A simple callback to print out a name that might appear in the
// middle of a type declaration
void WriteName(string middle)
{
try
{
if (middle != null)
{
this.Write(middle);
}
}
catch(Exception )
{
this.Write("exception deanwi");
}
}
public void WriteFieldDeclaration(IFieldDeclaration fieldDeclaration)
{
WriteFieldVisibilitySpecifier(fieldDeclaration.Visibility);
if (fieldDeclaration.Literal)
{
this.WriteKeyword("literal");
this.Write(" ");
}
else if (fieldDeclaration.Static)
{
this.WriteKeyword("static");
this.Write(" ");
}
if (fieldDeclaration.ReadOnly)
{
this.WriteKeyword("initonly");
this.Write(" ");
}
this.WriteType(fieldDeclaration.FieldType, this.WriteName, fieldDeclaration.Name, null, false);
IExpression initExpr = fieldDeclaration.Initializer;
if (initExpr != null)
{
this.Write(" = ");
this.WriteExpression(initExpr);
}
this.Write(";");
}
public void WriteMethodDeclaration(IMethodDeclaration methodDeclaration)
{
bool OuterBlock = true; //surround the method with {}. Needed because sometimes we have to emit the first part of a try block.
SkipTryCount = 0;
SomeConstructor = false;
refOnStack = new Hashtable();
IConstructorDeclaration iConstructorDeclaration = (methodDeclaration) as IConstructorDeclaration;
// Need to check if we have a special function like the dtor or the finalizer
ITypeDeclaration declaringTypeDecl = (methodDeclaration.DeclaringType as ITypeReference).Resolve();
MethodNameExt MNE = new MethodNameExt(methodDeclaration.Name, methodDeclaration.Overrides, declaringTypeDecl.Name);
this.baseType = declaringTypeDecl.BaseType;
bool fGlobal = declaringTypeDecl.Name == "<Module>";
bool firstThingAfterVisibility = true;
if (!fGlobal && !declaringTypeDecl.Interface && (!methodDeclaration.SpecialName || (methodDeclaration.Name != ".cctor")))
{
WriteMethodVisibilitySpecifier(methodDeclaration.Visibility);
}
if ((this.configuration["ShowCustomAttributes"] == "true") && (methodDeclaration.Attributes.Count != 0))
{
this.WriteLine();
firstThingAfterVisibility = false;
this.WriteCustomAttributeCollection(methodDeclaration, null);
this.WriteLine();
}
WriteGenericArguments(declaringTypeDecl.GenericArguments, ref firstThingAfterVisibility);
WriteGenericArguments(methodDeclaration.GenericArguments, ref firstThingAfterVisibility);
if (firstThingAfterVisibility)
{
this.Write(" ");
firstThingAfterVisibility = false;
}
if (!fGlobal && methodDeclaration.Static)
{
this.WriteKeyword("static");
this.Write(" ");
}
if (!declaringTypeDecl.Interface && methodDeclaration.Virtual)
{
this.WriteKeyword("virtual");
this.Write(" ");
}
if (MNE.Constructor || MNE.StaticConstructor)
{
this.SomeConstructor = true;
this.WriteMethodDeclMiddle(methodDeclaration);
}
else if (MNE.ImplicitOrExplicit)
{
if (MNE.Explicit)
{
this.WriteKeyword("explicit");
this.Write(" ");
}
this.WriteKeyword("operator");
this.Write(" ");
this.WriteType(methodDeclaration.ReturnType.Type);
this.Write("(");
this.Write(")");
}
else
{
this.WriteType(methodDeclaration.ReturnType.Type,
this.WriteMethodDeclMiddle, methodDeclaration, methodDeclaration.ReturnType, false);
}
// new,sealed,abstract,override
if (!methodDeclaration.NewSlot && methodDeclaration.Final)
{
this.Write(" ");
this.WriteKeyword("sealed");
}
if (methodDeclaration.Virtual)
{
if (methodDeclaration.Abstract)
{
this.Write(" ");
this.WriteKeyword("abstract");
}
if (!methodDeclaration.NewSlot)
{
this.Write(" ");
this.WriteKeyword("override");
}
// How do we decide to write 'new' here? If the method is introduced
// in the current class, it's marked newslot, but we only use the 'new'
// keyword when it would otherwise be an override.
}
// explicit override list
ICollection explicitOverrides = methodDeclaration.Overrides;
if (explicitOverrides.Count > 0)
{
string separator = " = ";
foreach (IMethodReference overriddingMethod in explicitOverrides)
{
this.Write(separator);
this.WriteReference(overriddingMethod.Name, "", overriddingMethod);
separator = ", ";
}
}
this.WriteGenericParameterConstraintCollection(methodDeclaration.GenericArguments);
if (MNE.Constructor && iConstructorDeclaration != null)
{
IMethodInvokeExpression iMethodInvokeExpression = iConstructorDeclaration.Initializer;
if (iMethodInvokeExpression != null)
{
// R() : R1(1) {}
IMethodReferenceExpression iMethodReferenceExpression = iMethodInvokeExpression.Method as IMethodReferenceExpression;
if (iMethodReferenceExpression != null)
{
if (iMethodInvokeExpression.Arguments.Count > 0)
{
this.Write(" : ");
this.WriteExpression(iMethodReferenceExpression.Target);
this.Write("(");