1
1
using CppSharp . AST ;
2
+ using CppSharp . Generators . C ;
2
3
using System . Collections . Generic ;
3
4
using System . Linq ;
4
5
using System . Text ;
@@ -103,6 +104,14 @@ public virtual void GenerateTranslationUnitRegistrationFunctionBegin(Translation
103
104
public virtual void GenerateTranslationUnitRegistrationFunctionBody ( TranslationUnit translationUnit )
104
105
{
105
106
GenerateDeclarationContainerList ( translationUnit ) ;
107
+
108
+ GenerationContext . Scoped ( RegistrableGeneratorContext . IsDetach , DetachmentOption . On , ( ) =>
109
+ {
110
+ foreach ( var variable in translationUnit . Variables )
111
+ {
112
+ variable . Visit ( this ) ;
113
+ }
114
+ } ) ;
106
115
}
107
116
108
117
public virtual void GenerateTranslationUnitRegistrationFunctionEnd ( TranslationUnit translationUnit )
@@ -175,19 +184,14 @@ public virtual void GenerateNamespaceBody(Namespace @namespace)
175
184
176
185
public virtual void GenerateNamespaceDeclarationList ( Namespace @namespace , DetachmentOption detachment )
177
186
{
178
- if ( detachment == DetachmentOption . Off )
179
- {
180
- GenerateNamespaceFunctions ( @namespace ) ;
181
- GenerateNamespaceVariables ( @namespace ) ;
182
- }
183
- else
187
+ GenerateNamespaceContainerList ( @namespace ) ;
188
+ GenerateNamespaceTemplates ( @namespace ) ;
189
+ GenerateNamespaceTypedefs ( @namespace ) ;
190
+ GenerationContext . Scoped ( RegistrableGeneratorContext . IsDetach , DetachmentOption . On , ( ) =>
184
191
{
185
- GenerateNamespaceContainerList ( @namespace ) ;
186
- GenerateNamespaceTemplates ( @namespace ) ;
187
- GenerateNamespaceTypedefs ( @namespace ) ;
188
192
GenerateNamespaceFunctions ( @namespace ) ;
189
193
GenerateNamespaceVariables ( @namespace ) ;
190
- }
194
+ } ) ;
191
195
}
192
196
193
197
public virtual void GenerateNamespaceContainerList ( Namespace @namespace )
@@ -209,6 +213,10 @@ public virtual void GenerateNamespaceFunctions(Namespace @namespace)
209
213
210
214
public virtual void GenerateNamespaceVariables ( Namespace @namespace )
211
215
{
216
+ foreach ( var variable in @namespace . Variables )
217
+ {
218
+ variable . Visit ( this ) ;
219
+ }
212
220
}
213
221
214
222
public virtual void GenerateNamespaceEnd ( Namespace @namespace )
@@ -543,6 +551,14 @@ public virtual void GenerateClassDeclFunctions(Class @class)
543
551
544
552
public virtual void GenerateClassDeclVariables ( Class @class )
545
553
{
554
+ foreach ( var field in @class . Fields )
555
+ {
556
+ field . Visit ( this ) ;
557
+ }
558
+ foreach ( var variable in @class . Variables )
559
+ {
560
+ variable . Visit ( this ) ;
561
+ }
546
562
}
547
563
548
564
public virtual void GenerateClassDeclEnd ( Class @class )
@@ -611,6 +627,217 @@ public override bool VisitClassDecl(Class @class)
611
627
612
628
#endregion
613
629
630
+ #region Field
631
+
632
+ #region Field
633
+
634
+ public virtual bool CanGenerateFieldDecl ( Field field )
635
+ {
636
+ if ( AlreadyVisited ( field ) )
637
+ {
638
+ return false ;
639
+ }
640
+ else if ( field . Access != AccessSpecifier . Public )
641
+ {
642
+ return false ;
643
+ }
644
+ else if ( ! NonTemplateAllowed )
645
+ {
646
+ return false ;
647
+ }
648
+ return field . IsGenerated ;
649
+ }
650
+
651
+ public virtual bool GenerateFieldDecl ( Field field )
652
+ {
653
+ var isDetach = GenerationContext . PeekIsDetach ( DetachmentOption . Off ) ;
654
+
655
+ if ( isDetach == DetachmentOption . Forced || isDetach == Utils . FindDetachmentOption ( field ) )
656
+ {
657
+ string fieldName = field . Name ;
658
+ string fieldNameQuoted = $ "\" { fieldName } \" ";
659
+ string fieldContextualName = NamingStrategy . GetContextualName ( field , GenerationContext , FQNOption . IgnoreNone ) ;
660
+
661
+ if ( isDetach != DetachmentOption . Off )
662
+ {
663
+ Write ( $ "{ NamingStrategy . GetBindingContext ( field , GenerationContext ) } [{ fieldNameQuoted } ] = ") ;
664
+ }
665
+ else
666
+ {
667
+ WriteLine ( "," ) ;
668
+ Write ( $ "{ fieldNameQuoted } , ") ;
669
+ }
670
+ // TODO : check for typemaps!!!
671
+ {
672
+ Write ( $ "&{ fieldContextualName } ") ;
673
+ }
674
+ if ( isDetach != DetachmentOption . Off )
675
+ {
676
+ WriteLine ( ";" ) ;
677
+ }
678
+ }
679
+
680
+ return true ;
681
+ }
682
+
683
+ #endregion
684
+
685
+ #region Bitfield
686
+
687
+ public virtual bool CanGenerateFieldDeclBitfield ( Field field )
688
+ {
689
+ if ( AlreadyVisited ( field ) )
690
+ {
691
+ return false ;
692
+ }
693
+ else if ( field . Access != AccessSpecifier . Public )
694
+ {
695
+ return false ;
696
+ }
697
+ else if ( ! NonTemplateAllowed )
698
+ {
699
+ return false ;
700
+ }
701
+ return field . IsGenerated ;
702
+ }
703
+
704
+ public virtual bool GenerateFieldDeclBitfield ( Field field )
705
+ {
706
+ var isDetach = GenerationContext . PeekIsDetach ( DetachmentOption . Off ) ;
707
+
708
+ if ( isDetach == DetachmentOption . Forced || isDetach == Utils . FindDetachmentOption ( field ) )
709
+ {
710
+ string bitfieldOriginalName = field . OriginalName ;
711
+ string bitfieldName = field . Name ;
712
+ string bitfieldNameQuoted = $ "\" { bitfieldName } \" ";
713
+ string bitfieldCppContext = NamingStrategy . GetCppContext ( field , GenerationContext , FQNOption . IgnoreNone ) ;
714
+ string bitfieldType = field . Type . Visit ( new CppTypePrinter ( Context ) ) ;
715
+
716
+ if ( isDetach != DetachmentOption . Off )
717
+ {
718
+ Write ( $ "{ NamingStrategy . GetBindingContext ( field , GenerationContext ) } [{ bitfieldNameQuoted } ] = ") ;
719
+ }
720
+ else
721
+ {
722
+ WriteLine ( "," ) ;
723
+ Write ( $ "{ bitfieldNameQuoted } , ") ;
724
+ }
725
+ WriteLine ( "::sol::property(" ) ;
726
+ Indent ( ) ;
727
+ WriteLine ( $ "[]({ bitfieldCppContext } & self) {{") ;
728
+ Indent ( ) ;
729
+ WriteLine ( $ "return self.{ bitfieldOriginalName } ;") ;
730
+ Unindent ( ) ;
731
+ WriteLine ( "}, " ) ;
732
+ WriteLine ( $ "[]({ bitfieldCppContext } & self, { bitfieldType } value) {{") ;
733
+ Indent ( ) ;
734
+ WriteLine ( $ "self.{ bitfieldOriginalName } = value;") ;
735
+ Unindent ( ) ;
736
+ WriteLine ( "}" ) ;
737
+ Unindent ( ) ;
738
+ Write ( ")" ) ;
739
+ if ( isDetach != DetachmentOption . Off )
740
+ {
741
+ WriteLine ( ";" ) ;
742
+ }
743
+ }
744
+
745
+ return true ;
746
+ }
747
+
748
+ #endregion
749
+
750
+ public override bool VisitFieldDecl ( Field field )
751
+ {
752
+ if ( field . IsBitField )
753
+ {
754
+ if ( ! CanGenerateFieldDeclBitfield ( field ) )
755
+ {
756
+ return false ;
757
+ }
758
+
759
+ return GenerateFieldDeclBitfield ( field ) ;
760
+ }
761
+ else
762
+ {
763
+ if ( ! CanGenerateFieldDecl ( field ) )
764
+ {
765
+ return false ;
766
+ }
767
+
768
+ return GenerateFieldDecl ( field ) ;
769
+ }
770
+ return false ;
771
+ }
772
+
773
+ #endregion
774
+
775
+ #region Variable
776
+
777
+ public virtual bool CanGenerateVariableDecl ( Variable variable )
778
+ {
779
+ if ( AlreadyVisited ( variable ) )
780
+ {
781
+ return false ;
782
+ }
783
+ else if ( variable . Access != AccessSpecifier . Public )
784
+ {
785
+ return false ;
786
+ }
787
+ else if ( ! NonTemplateAllowed )
788
+ {
789
+ return false ;
790
+ }
791
+ return variable . IsGenerated ;
792
+ }
793
+
794
+ public virtual bool GenerateVariableDecl ( Variable variable )
795
+ {
796
+ var isDetach = GenerationContext . PeekIsDetach ( DetachmentOption . Off ) ;
797
+
798
+ if ( isDetach == DetachmentOption . Forced || isDetach == Utils . FindDetachmentOption ( variable ) )
799
+ {
800
+ string variableName = variable . Name ;
801
+ string variableNameQuoted = $ "\" { variableName } \" ";
802
+ string variableBindingContext = NamingStrategy . GetBindingContext ( variable , GenerationContext ) ;
803
+ string variableContextualName = NamingStrategy . GetContextualName ( variable , GenerationContext , FQNOption . IgnoreNone ) ;
804
+ // TODO: Bug in sol until it gets resolved: we can only bind static class variable by reference.
805
+ if ( variable . OriginalNamespace is Class )
806
+ {
807
+ variableContextualName = $ "::std::ref({ variableContextualName } )";
808
+ }
809
+
810
+ // TODO: check for typemaps!!!
811
+ if ( isDetach != DetachmentOption . Off )
812
+ {
813
+ Write ( $ "{ variableBindingContext } [{ variableNameQuoted } ] = ::sol::var({ variableContextualName } );") ;
814
+ }
815
+ else
816
+ {
817
+ WriteLine ( "," ) ;
818
+ Write ( $ "{ variableNameQuoted } , ::sol::var({ variableContextualName } )") ;
819
+ }
820
+ if ( isDetach != DetachmentOption . Off )
821
+ {
822
+ WriteLine ( ";" ) ;
823
+ }
824
+ }
825
+
826
+ return true ;
827
+ }
828
+
829
+ public override bool VisitVariableDecl ( Variable variable )
830
+ {
831
+ if ( ! CanGenerateVariableDecl ( variable ) )
832
+ {
833
+ return false ;
834
+ }
835
+
836
+ return GenerateVariableDecl ( variable ) ;
837
+ }
838
+
839
+ #endregion
840
+
614
841
public virtual bool CanGenerateConstructor ( Method method )
615
842
{
616
843
// if not self:isNonTemplateAllowed(context) then
0 commit comments