-
Notifications
You must be signed in to change notification settings - Fork 3
/
MummyCsharpGenerator.cxx
4877 lines (4158 loc) · 134 KB
/
MummyCsharpGenerator.cxx
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
//----------------------------------------------------------------------------
//
// $Id: MummyCsharpGenerator.cxx 517 2010-11-11 15:00:41Z david.cole $
//
// $Author: david.cole $
// $Date: 2010-11-11 10:00:41 -0500 (Thu, 11 Nov 2010) $
// $Revision: 517 $
//
// Copyright (C) 2006-2009 Kitware, Inc.
//
//----------------------------------------------------------------------------
#include "MummyCsharpGenerator.h"
#include "MummyLineOrientedTextFileReader.h"
#include "MummyLog.h"
#include "MummySettings.h"
#include "cableArrayType.h"
#include "cableClass.h"
#include "cableClassType.h"
#include "cableConstructor.h"
#include "cableEnumeration.h"
#include "cableEnumerationType.h"
#include "cableField.h"
#include "cableFunctionType.h"
#include "cableFundamentalType.h"
#include "cableMethod.h"
#include "cablePointerType.h"
#include "cableReferenceType.h"
#include "cableType.h"
#include "cableTypedef.h"
#include "cxxFundamentalType.h"
#include "gxsys/RegularExpression.hxx"
#include "gxsys/SystemTools.hxx"
#include "gxsys/ios/sstream"
#include "gxsys/stl/algorithm"
#include "gxsys/stl/map"
#include "gxsys/stl/set"
#include "gxsys/stl/string"
#include "gxsys/stl/vector"
#include "string.h" // strstr
//----------------------------------------------------------------------------
MummyCsharpGenerator::MummyCsharpGenerator()
{
this->CurrentMethodId = 0;
this->ClassLineNumber = 0;
//this->MethodIdMap; // empty, as constructed
//this->TargetInterface; // empty, as constructed
//this->HintsMap; // empty, as constructed
}
//----------------------------------------------------------------------------
MummyCsharpGenerator::~MummyCsharpGenerator()
{
}
//----------------------------------------------------------------------------
bool MummyCsharpGenerator::GenerateWrappers()
{
gxsys_stl::string elDllVariableName(this->GetSettings()->GetGroup());
elDllVariableName += "EL_dll";
this->EmitCSharpWrapperClass(*GetStream(), elDllVariableName.c_str(),
GetTargetClass());
if (this->GetSettings()->GetVerbose())
{
gxsys_stl::map<const gxsys_stl::string, gxsys_stl::string>::iterator it;
for (it = this->HintsMap.begin(); it != this->HintsMap.end(); ++it)
{
LogInfo(mi_VerboseInfo, << it->first << ": '" << it->second << "'");
}
}
return true;
}
//----------------------------------------------------------------------------
void MummyCsharpGenerator::SetTargetClass(const cable::Class *c)
{
this->MummyGenerator::SetTargetClass(c);
// Ensure HeaderFileReader is always called first with the target class.
// (Presently, there is only one cached HeaderFileReader and it only reads
// the file when it is first called... Subsequent calls ignore the input
// data and simply return the cached HeaderFileReader. Multiple readers
// may eventually be necessary to support BTX/ETX exclusion fully...)
//
this->GetHeaderFileReader(c);
// Only populate the lookup entries with stuff from the *parent* class
// (and its parents...) That way, we can use the existence of a signature
// in the table as evidence that a virtual is being overridden (for C#
// keyword override purposes) or that a static is being redefined at a
// more derived level (hence hiding the parent's definition and requiring
// use of the C# keyword "new" to avoid the warning about hiding...)
//
ClearLookupEntries();
AddLookupEntries(GetWrappableParentClass(c));
// Cache a map of the externalHints file:
//
this->CacheExternalHints(this->GetSettings()->GetExternalHints(c));
}
//----------------------------------------------------------------------------
void MummyCsharpGenerator::CacheExternalHints(const gxsys_stl::string& hintsfile)
{
gxsys_stl::vector<gxsys_stl::string> hints;
gxsys_stl::string line;
gxsys::RegularExpression re;
re.compile("([^\t ]+)[\t ]+([^\t ]+)[\t ]+([^\t ]+)[\t ]+([^\t ]+)");
MummyLineOrientedTextFileReader reader;
reader.SetFileName(hintsfile.c_str());
this->HintsMap.clear();
unsigned int n = reader.GetNumberOfLines();
unsigned int i = 0;
for (i= 1; i<=n; ++i)
{
line = reader.GetLine(i);
if (!line.empty())
{
if (re.find(line))
{
gxsys_stl::string className = re.match(1);
gxsys_stl::string methodName = re.match(2);
gxsys_stl::string type = re.match(3);
gxsys_stl::string count = re.match(4);
gxsys_stl::string key(className + "::" + methodName);
if (this->HintsMap.find(key) == this->HintsMap.end())
{
this->HintsMap[key] = line;
}
else
{
LogFileLineWarningMsg(hintsfile, i, mw_MultipleHints,
"More than one line in the hints file for " << className
<< "::" << methodName);
}
}
}
}
}
//----------------------------------------------------------------------------
void MummyCsharpGenerator::AddTargetInterface(const gxsys_stl::string& iface)
{
if (this->TargetInterface != "")
{
LogWarning(mw_MultipleTargetInterfaces,
"AddTargetInterface being called more than once. " <<
"Implementation currently only supports 1 interface. " <<
"Clobbering existing target interface: " <<
this->TargetInterface);
}
this->TargetInterface = iface;
}
//----------------------------------------------------------------------------
bool MummyCsharpGenerator::HasTargetInterface(const char *iface) const
{
if (iface)
{
return this->TargetInterface == iface;
}
return false;
}
//----------------------------------------------------------------------------
bool MummyCsharpGenerator::IsKeyword(const char *p)
{
static gxsys_stl::set<gxsys_stl::string> keywords;
if (0 == keywords.size())
{
keywords.insert("abstract");
keywords.insert("as");
keywords.insert("base");
keywords.insert("bool");
keywords.insert("break");
keywords.insert("byte");
keywords.insert("case");
keywords.insert("catch");
keywords.insert("char");
keywords.insert("checked");
keywords.insert("class");
keywords.insert("const");
keywords.insert("continue");
keywords.insert("decimal");
keywords.insert("default");
keywords.insert("delegate");
keywords.insert("do");
keywords.insert("double");
keywords.insert("else");
keywords.insert("enum");
keywords.insert("event");
keywords.insert("explicit");
keywords.insert("extern");
keywords.insert("false");
keywords.insert("finally");
keywords.insert("fixed");
keywords.insert("float");
keywords.insert("for");
keywords.insert("foreach");
keywords.insert("goto");
keywords.insert("if");
keywords.insert("implicit");
keywords.insert("in");
keywords.insert("int");
keywords.insert("interface");
keywords.insert("internal");
keywords.insert("is");
keywords.insert("lock");
keywords.insert("long");
keywords.insert("namespace");
keywords.insert("new");
keywords.insert("null");
keywords.insert("object");
keywords.insert("operator");
keywords.insert("out");
keywords.insert("override");
keywords.insert("params");
keywords.insert("private");
keywords.insert("protected");
keywords.insert("public");
keywords.insert("readonly");
keywords.insert("ref");
keywords.insert("return");
keywords.insert("sbyte");
keywords.insert("sealed");
keywords.insert("short");
keywords.insert("sizeof");
keywords.insert("stackalloc");
keywords.insert("static");
keywords.insert("string");
keywords.insert("struct");
keywords.insert("switch");
keywords.insert("this");
keywords.insert("throw");
keywords.insert("true");
keywords.insert("try");
keywords.insert("typeof");
keywords.insert("uint");
keywords.insert("ulong");
keywords.insert("unchecked");
keywords.insert("unsafe");
keywords.insert("ushort");
keywords.insert("using");
keywords.insert("virtual");
keywords.insert("void");
keywords.insert("volatile");
keywords.insert("while");
}
gxsys_stl::set<gxsys_stl::string>::iterator it = keywords.find(gxsys_stl::string(p));
if (it != keywords.end())
{
return true;
}
return false;
}
//----------------------------------------------------------------------------
bool MummyCsharpGenerator::IsReservedMethodName(const gxsys_stl::string &name)
{
return (
name == "Equals" ||
name == "Finalize" ||
name == "GetHashCode" ||
name == "GetType" ||
name == "MemberwiseClone" ||
name == "Object" ||
name == "ToString");
}
//----------------------------------------------------------------------------
gxsys_stl::string MummyCsharpGenerator::GetFundamentalTypeString(const cable::Type *t)
{
gxsys_stl::string s;
if (cable::Type::FundamentalTypeId == t->GetTypeId())
{
switch (cxx::FundamentalType::SafeDownCast(t->GetCxxType().GetType())->GetId())
{
case cxx::FundamentalType::UnsignedChar:
s = "byte";
break;
case cxx::FundamentalType::UnsignedShortInt:
s = "ushort";
break;
case cxx::FundamentalType::UnsignedInt:
case cxx::FundamentalType::UnsignedLongInt:
s = "uint";
break;
case cxx::FundamentalType::SignedChar:
case cxx::FundamentalType::Char:
s = "sbyte";
break;
case cxx::FundamentalType::ShortInt:
s = "short";
break;
case cxx::FundamentalType::Int:
case cxx::FundamentalType::LongInt:
s = "int";
break;
case cxx::FundamentalType::Bool:
s = "bool";
break;
case cxx::FundamentalType::Float:
s = "float";
break;
case cxx::FundamentalType::Double:
s = "double";
break;
case cxx::FundamentalType::Void:
s = "void";
break;
case cxx::FundamentalType::UnsignedLongLongInt:
s = "ulong";
break;
case cxx::FundamentalType::LongLongInt:
s = "long";
break;
//case cxx::FundamentalType::WChar_t:
//case cxx::FundamentalType::LongDouble:
//case cxx::FundamentalType::ComplexFloat:
//case cxx::FundamentalType::ComplexDouble:
//case cxx::FundamentalType::ComplexLongDouble:
//case cxx::FundamentalType::NumberOfTypes:
default:
break;
}
}
if (s == "")
{
LogError(me_UnknownFundamentalType,
<< "Unhandled variable type. GetFundamentalTypeString returning the empty string...");
}
return s;
}
//----------------------------------------------------------------------------
gxsys_stl::string MummyCsharpGenerator::GetWrappedMethodName(const cable::Method *m)
{
gxsys_stl::string name(m->GetName());
if (IsReservedMethodName(name))
{
name = name + "Wrapper";
LogFileLineWarningMsg(m->GetFile(), m->GetLine(), mw_ReservedMethodName,
<< "Reserved method name '" << m->GetName() << "' used. Rename it to eliminate this warning...");
}
return name;
}
//----------------------------------------------------------------------------
gxsys_stl::string GetWrappedEnumName(const cable::Enumeration *e)
{
gxsys_stl::string ename(e->GetName());
// Don't name unnamed enums with invalid C#/C++ identifiers like "$"...
//
if (ename == "" ||
strstr(ename.c_str(), "$"))
{
if (e->Begin() != e->End())
{
ename = *(e->Begin());
ename += "_WrapperEnum";
}
else
{
ename = "WARNING_unnamed_enum";
}
LogFileLineWarningMsg(e->GetFile(), e->GetLine(), mw_UnnamedEnum,
"Unnamed enum found. Name it to eliminate this warning...");
}
return ename;
}
//----------------------------------------------------------------------------
bool ExtractTypeAndCountFromHintLine(
const gxsys_stl::string& hint,
gxsys_stl::string& type,
gxsys_stl::string& count
)
{
gxsys::RegularExpression re;
re.compile("([^\t ]+)[\t ]+([^\t ]+)[\t ]+([^\t ]+)[\t ]+([^\t ]+)");
if (re.find(hint))
{
//className = re.match(1);
//methodName = re.match(2);
type = re.match(3);
count = re.match(4);
return true;
}
return false;
}
//----------------------------------------------------------------------------
bool ReturnTypeMatchesHintType(
cable::Type *t,
const gxsys_stl::string& type
)
{
gxsys_stl::string utype = gxsys::SystemTools::UpperCase(type);
// Init with the "not a real enum value" value:
//
cxx::FundamentalType::Id ftid = cxx::FundamentalType::NumberOfTypes;
// Values of 'type' currently present in the VTK hints file are:
//
if (utype == "301") { ftid = cxx::FundamentalType::Float; }
else if (utype == "304") { ftid = cxx::FundamentalType::Int; }
else if (utype == "307") { ftid = cxx::FundamentalType::Double; }
else if (utype == "30A") { ftid = cxx::FundamentalType::Int; } // vtkIdType (could be 32 or 64 bit...)
else if (utype == "2307") { ftid = cxx::FundamentalType::Double; } // 0x2307 == double* + something?
if (cxx::FundamentalType::NumberOfTypes == ftid)
{
LogWarning(mw_UnknownHintDataType, "Unknown externalHints data type string '" << type << "'");
}
else if (IsFundamentalPointer(t, ftid))
{
return true;
}
return false;
}
//----------------------------------------------------------------------------
bool ExtractCountFromMethodDeclarationLine(
const gxsys_stl::string& line,
gxsys_stl::string& count
)
{
// Grudgingly allow this VTK-ism to creep into the mummy codebase to avoid
// introducing a list of regular expressions in the MummySettings.xml file
// just for VTK-wrapping-hint-based array size specification...
//
// In VTK, in addition to the explicit external hints file VTK/Wrapping/hints,
// the following macros from VTK/Common/vtkSetGet.h are also recognized as
// implicitly having hint sizes associated with them:
//
gxsys::RegularExpression re;
re.compile("^[\t ]*vtkGetVector([0-9]+)Macro\\(.*,.*\\)");
if (re.find(line))
{
count = re.match(1);
return true;
}
re.compile("^[\t ]*vtkGetVectorMacro\\(.*,.*,.*([0-9]+).*\\)");
if (re.find(line))
{
count = re.match(1);
return true;
}
re.compile("^[\t ]*vtkViewportCoordinateMacro\\(.*\\)");
if (re.find(line))
{
count = "2";
return true;
}
re.compile("^[\t ]*vtkWorldCoordinateMacro\\(.*\\)");
if (re.find(line))
{
count = "3";
return true;
}
return false;
}
//----------------------------------------------------------------------------
// Callers of GetMethodArgumentArraySize pass this value as "i" to query
// about the return value of the method. Otherwise, "i" is presumed to be an
// index (0 to cArgs-1) of one of the method's arguments.
#define RETURN_VALUE (0x84848484)
//----------------------------------------------------------------------------
gxsys_stl::string MummyCsharpGenerator::GetMethodArgumentArraySize(const cable::Class *c,
const cable::Method *m, const cable::FunctionType *ft, unsigned int i)
{
gxsys_stl::string argArraySize;
cable::Type *retType = 0;
// First check for an attribute directly in the source code:
//
gxsys_stl::string atts;
if (RETURN_VALUE == i)
{
atts = m->GetAttributes();
}
else
{
atts = ft->GetArgumentAttributes(i);
}
if (atts != "")
{
argArraySize = ExtractArraySize(atts);
}
// If no direct hint, check the externalHints file, if any:
//
if ((argArraySize == "") && (this->HintsMap.size() > 0))
{
// The VTK/Wrapping/hints file only tells us about the size of the pointer
// returned by the method. Only return a non-empty array size from a
// VTK/Wrapping/hints-style file if this is for a return value of a method:
//
if (RETURN_VALUE == i)
{
retType = ft->GetReturns();
if (!IsVoid(retType))
{
gxsys_stl::string methodName(m->GetName());
gxsys_stl::map<const gxsys_stl::string, gxsys_stl::string>::iterator it;
const cable::Class *cIt = c;
while (cIt != NULL && (argArraySize == ""))
{
gxsys_stl::string fullClassName(GetFullyQualifiedNameForCPlusPlus(cIt));
gxsys_stl::string key(fullClassName + "::" + methodName);
gxsys_stl::string hintline;
it = this->HintsMap.find(key);
if (it != this->HintsMap.end())
{
hintline = it->second;
gxsys_stl::string type;
gxsys_stl::string count;
if (ExtractTypeAndCountFromHintLine(hintline, type, count) &&
ReturnTypeMatchesHintType(retType, type))
{
argArraySize = count;
LogVerboseInfo("using external array size hint: " << argArraySize << " " << key);
}
}
// If not found yet, keep looking up at parent class hints to see
// if it is inherited. Method name and return type should still match
// even if the hint is at the parent class level.
//
if (argArraySize == "")
{
cIt = GetParentClass(cIt);
}
}
}
}
}
// If still no hint, see if the line declaring the method in the header
// file uses a macro that gives us a hint about the size of the array...
//
if (argArraySize == "")
{
if (RETURN_VALUE == i)
{
retType = ft->GetReturns();
if (!IsVoid(retType) && !IsObjectPointer(retType))
{
gxsys_stl::string line(this->GetHeaderFileReader(c)->GetLine(m->GetLine()));
gxsys_stl::string count;
if (ExtractCountFromMethodDeclarationLine(line, count))
{
argArraySize = count;
if (this->GetSettings()->GetVerbose())
{
LogFileLineInfoMsg(c->GetFile(), m->GetLine(), mi_VerboseInfo,
"inferred array size hint '" << count <<
"' from method declaration '" << line << "'");
}
}
}
}
}
return argArraySize;
}
//----------------------------------------------------------------------------
//
// GetMethodSignature is very similar to EmitCSharpMethodDeclaration.
// Changes in either may need to be made in both places...
//
gxsys_stl::string MummyCsharpGenerator::GetMethodSignature(const cable::Class *c, const cable::Method *m)
{
gxsys_ios::ostringstream os;
cable::FunctionType *ft = m->GetFunctionType();
unsigned int cArgs = ft->GetNumberOfArguments();
unsigned int i = 0;
cable::Type *argType = 0;
// Method name:
Emit(os, GetWrappedMethodName(m).c_str());
// Open args:
Emit(os, "(");
// The C# arg types:
for (i= 0; i<cArgs; ++i)
{
argType = ft->GetArgument(i);
// Is arg an array?
//
gxsys_stl::string argArraySize(GetMethodArgumentArraySize(c, m, ft, i));
// arg type:
Emit(os, GetCSharpTypeString(argType, false, argArraySize != "").c_str());
// array notation:
if (argArraySize != "")
{
Emit(os, "[]");
}
if (i<cArgs-1)
{
Emit(os, ", ");
}
}
// Close args:
Emit(os, ")");
return os.str();
}
//----------------------------------------------------------------------------
const char *MummyCsharpGenerator::GetArgName(cable::FunctionType *ftype, unsigned int i)
{
const char *p = ftype->GetArgumentName(i);
if (p && *p && !IsKeyword(p))
{
return p;
}
// Hacky, but for now:
static char buf[32];
sprintf(buf, "arg%u", i);
return buf;
}
//----------------------------------------------------------------------------
bool MummyCsharpGenerator::FundamentalTypeIsWrappable(const cable::Type* t)
{
if (cable::Type::FundamentalTypeId == t->GetTypeId())
{
switch (cxx::FundamentalType::SafeDownCast(t->GetCxxType().GetType())->GetId())
{
case cxx::FundamentalType::UnsignedChar:
case cxx::FundamentalType::UnsignedShortInt:
case cxx::FundamentalType::UnsignedInt:
case cxx::FundamentalType::UnsignedLongInt:
case cxx::FundamentalType::SignedChar:
case cxx::FundamentalType::Char:
case cxx::FundamentalType::ShortInt:
case cxx::FundamentalType::Int:
case cxx::FundamentalType::LongInt:
case cxx::FundamentalType::Bool:
case cxx::FundamentalType::Float:
case cxx::FundamentalType::Double:
case cxx::FundamentalType::Void:
case cxx::FundamentalType::UnsignedLongLongInt:
case cxx::FundamentalType::LongLongInt:
return true;
break;
case cxx::FundamentalType::WChar_t:
case cxx::FundamentalType::LongDouble:
case cxx::FundamentalType::ComplexFloat:
case cxx::FundamentalType::ComplexDouble:
case cxx::FundamentalType::ComplexLongDouble:
case cxx::FundamentalType::NumberOfTypes:
default:
break;
}
}
return false;
}
//----------------------------------------------------------------------------
bool MummyCsharpGenerator::TypeIsWrappable(const cable::Type* t)
{
bool wrappable = false;
switch (t->GetTypeId())
{
case cable::Type::EnumerationTypeId:
wrappable = true;
break;
case cable::Type::FundamentalTypeId:
wrappable = FundamentalTypeIsWrappable(t);
break;
case cable::Type::ArrayTypeId:
wrappable = false;//TypeIsWrappable(cable::ArrayType::SafeDownCast(t)->GetTarget());
break;
case cable::Type::ClassTypeId:
wrappable = ClassIsWrappable(cable::ClassType::SafeDownCast(t)->GetClass());
break;
case cable::Type::PointerTypeId:
{
cable::Type *nested_type = cable::PointerType::SafeDownCast(t)->GetTarget();
cable::Type::TypeIdType nested_type_id = nested_type->GetTypeId();
// Only pointers to enums, fundamentals and objects are wrappable
// and then only if the nested type is also wrappable...
//
// A function pointer is "nearly" wrappable (as a delegate definition)
// if its return type and all of its argument types are wrappable.
// And it has a typedef that names it within this class (because
// that typedef is the thing that generates the delegate def in
// the C# class...)
//
// (This is what prevents pointers to pointers from being wrapped
// in the general case...)
//
if (cable::Type::EnumerationTypeId == nested_type_id ||
cable::Type::FundamentalTypeId == nested_type_id ||
cable::Type::ClassTypeId == nested_type_id)
{
wrappable = TypeIsWrappable(nested_type);
}
else if (cable::Type::FunctionTypeId == nested_type_id)
{
gxsys_stl::string s;
// Only really wrappable if nested_type is wrappable and an
// equivalent typedef name exists:
//
wrappable = TypeIsWrappable(nested_type) &&
EquivalentTypedefNameExists(this->GetTargetClass(),
cable::FunctionType::SafeDownCast(nested_type), s);
}
else
{
wrappable = false; // as initialized...
}
}
break;
case cable::Type::ReferenceTypeId:
{
cable::Type *nested_type = cable::ReferenceType::SafeDownCast(t)->GetTarget();
cable::Type::TypeIdType nested_type_id = nested_type->GetTypeId();
wrappable = false; // as initialized...
if (cable::Type::EnumerationTypeId == nested_type_id ||
cable::Type::FundamentalTypeId == nested_type_id ||
cable::Type::ClassTypeId == nested_type_id)
{
wrappable = TypeIsWrappable(nested_type);
}
else if (cable::Type::PointerTypeId == nested_type_id)
{
// References to pointers are allowed, but only if the pointer points
// to a wrappable class...
//
cable::Type *doubly_nested_type = cable::PointerType::SafeDownCast(nested_type)->GetTarget();
cable::Type::TypeIdType doubly_nested_type_id = doubly_nested_type->GetTypeId();
if (cable::Type::ClassTypeId == doubly_nested_type_id)
{
wrappable = TypeIsWrappable(doubly_nested_type);
}
}
}
break;
case cable::Type::OffsetTypeId:
case cable::Type::MethodTypeId:
wrappable = false;
break;
case cable::Type::FunctionTypeId:
wrappable = FunctionTypeIsWrappable(cable::FunctionType::SafeDownCast(t));
break;
default:
wrappable = false;
break;
}
return wrappable;
}
//----------------------------------------------------------------------------
bool IsCxxMainStyleParamPair(const cable::FunctionType* ft, unsigned int i)
{
// Special case C++ "main" style functions, as indicated by *this* *exact*
// *signature* including arg names:
// (..., int argc, char* argv[], ...)
//
if (i < ft->GetNumberOfArguments()-1 &&
gxsys_stl::string("argc") == ft->GetArgumentName(i) &&
cable::Type::FundamentalTypeId == ft->GetArgument(i)->GetTypeId() &&
cxx::FundamentalType::Int == cxx::FundamentalType::SafeDownCast(ft->GetArgument(i)->GetCxxType().GetType())->GetId() &&
gxsys_stl::string("argv") == ft->GetArgumentName(i+1) &&
IsCharPointerPointer(ft->GetArgument(i+1))
)
{
return true;
}
return false;
}
//----------------------------------------------------------------------------
bool MummyCsharpGenerator::FunctionTypeIsWrappable(const cable::FunctionType* ft)
{
bool wrappable = true;
cable::Type *argType = 0;
cable::Type *retType = 0;
unsigned int i = 0;
unsigned int cArgs = ft->GetNumberOfArguments();
retType = ft->GetReturns();
wrappable = TypeIsWrappable(retType);
for (i= 0; wrappable && i<cArgs; ++i)
{
argType = ft->GetArgument(i);
wrappable = TypeIsWrappable(argType);
// If not wrappable because argType is "char**" but it is a cxx main style
// param pair, then go ahead and say it is wrappable...
//
if (!wrappable && IsCxxMainStyleParamPair(ft, i-1))
{
wrappable = true;
}
}
return wrappable;
}
//----------------------------------------------------------------------------
bool MummyCsharpGenerator::MethodWrappableAsEvent(const cable::Method* m, const cable::Context::Access& access)
{
bool wrappableAsEvent = false;
gxsys_stl::string atts;
if (m)
{
if (cable::Context::Public == access)
{
if (cable::Function::FunctionId == m->GetFunctionId() ||
cable::Function::MethodId == m->GetFunctionId())
{
wrappableAsEvent = HasAttribute(m, "gccxml(iwhEvent)");
}
}
}
return wrappableAsEvent;
}
//----------------------------------------------------------------------------
bool MummyCsharpGenerator::MethodIsWrappable(const cable::Method* m, const cable::Context::Access& access)
{
bool wrappable = false;
bool hasDeprecatedAttribute = false;
bool hasExcludeAttribute = false;
bool isExcludedViaBtxEtx = false;
if (m)
{
if (cable::Context::Public == access)
{
if (cable::Function::FunctionId == m->GetFunctionId() ||
cable::Function::MethodId == m->GetFunctionId())
{
if (FunctionTypeIsWrappable(m->GetFunctionType()))
{
wrappable = true;
}
}
}
}
if (m && wrappable)
{
hasDeprecatedAttribute = HasAttribute(m, "deprecated");
hasExcludeAttribute = HasAttribute(m, "gccxml(iwhExclude)");
if (hasDeprecatedAttribute || hasExcludeAttribute)
{
wrappable = false;
}
}
if (m && wrappable)
{
cable::Class* c = cable::Class::SafeDownCast(m->GetContext());
// BTX ETX style exclusion can only be applied to methods of the current target
// class... We need to add multiple HeaderFileReader support to enable BTX ETX style
// exclusion fully (including parent class method exclusion for purposes of constructing
// the initial "what virtual methods do I inherit?" table...)
//
if (c && c == this->GetTargetClass() &&
this->GetHeaderFileReader(c)->IsLineExcluded(m->GetLine()))
{
isExcludedViaBtxEtx = true;
wrappable = false;
}
}
if (this->GetSettings()->GetVerbose())
{
if (m)
{
if (cable::Context::Public == access)
{
if (cable::Function::FunctionId == m->GetFunctionId() ||
cable::Function::MethodId == m->GetFunctionId())
{
if (wrappable)
{
LogInfo(mi_VerboseInfo, << m->GetNameOfClass() << " '" << m->GetName()