Exemple #1
0
 private void StashStackExtras(BasicBlock block)
 {
     var items = this.stack.Reverse();
     foreach (var item in items) {
         var lhs = block.PushStash(this.typeSystem, item);
         var stmt = new CodeAssignStatement(lhs, item.Expression);
         this.block.Statements.Insert(item.Index, stmt);
     }
     this.stack.Clear();
 }
        public void RegionsSnippetsAndLinePragmas()
        {
            var cu = new CodeCompileUnit();
            CodeNamespace ns = new CodeNamespace("Namespace1");

            cu.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Compile Unit Region"));
            cu.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            cu.Namespaces.Add(ns);

            var cd = new CodeTypeDeclaration("Class1");
            ns.Types.Add(cd);

            cd.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Outer Type Region"));
            cd.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            cd.Comments.Add(new CodeCommentStatement("Outer Type Comment"));

            CodeMemberField field1 = new CodeMemberField(typeof(String), "field1");
            CodeMemberField field2 = new CodeMemberField(typeof(String), "field2");
            field1.Comments.Add(new CodeCommentStatement("Field 1 Comment"));
            field2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Field Region"));
            field2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeMemberEvent evt1 = new CodeMemberEvent();
            evt1.Name = "Event1";
            evt1.Type = new CodeTypeReference(typeof(System.EventHandler));
            evt1.Attributes = (evt1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;

            CodeMemberEvent evt2 = new CodeMemberEvent();
            evt2.Name = "Event2";
            evt2.Type = new CodeTypeReference(typeof(System.EventHandler));
            evt2.Attributes = (evt2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;

            evt2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Event Region"));
            evt2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeMemberMethod method1 = new CodeMemberMethod();
            method1.Name = "Method1";
            method1.Attributes = (method1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method1.Statements.Add(
                new CodeDelegateInvokeExpression(
                    new CodeEventReferenceExpression(new CodeThisReferenceExpression(), "Event1"),
                    new CodeExpression[] {
                        new CodeThisReferenceExpression(),
                        new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.EventArgs"), "Empty")
                    }));

            CodeMemberMethod method2 = new CodeMemberMethod();
            method2.Name = "Method2";
            method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method2.Statements.Add(
                new CodeDelegateInvokeExpression(
                    new CodeEventReferenceExpression(new CodeThisReferenceExpression(), "Event2"),
                    new CodeExpression[] {
                    new CodeThisReferenceExpression(),
                    new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.EventArgs"), "Empty")
                    }));
            method2.LinePragma = new CodeLinePragma("MethodLinePragma.txt", 500);
            method2.Comments.Add(new CodeCommentStatement("Method 2 Comment"));

            method2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Method Region"));
            method2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeMemberProperty property1 = new CodeMemberProperty();
            property1.Name = "Property1";
            property1.Type = new CodeTypeReference(typeof(string));
            property1.Attributes = (property1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property1.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeFieldReferenceExpression(
                        new CodeThisReferenceExpression(),
                        "field1")));

            CodeMemberProperty property2 = new CodeMemberProperty();
            property2.Name = "Property2";
            property2.Type = new CodeTypeReference(typeof(string));
            property2.Attributes = (property2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property2.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeFieldReferenceExpression(
                        new CodeThisReferenceExpression(),
                        "field2")));

            property2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Property Region"));
            property2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeConstructor constructor1 = new CodeConstructor();
            constructor1.Attributes = (constructor1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            CodeStatement conState1 = new CodeAssignStatement(
                                        new CodeFieldReferenceExpression(
                                            new CodeThisReferenceExpression(),
                                            "field1"),
                                        new CodePrimitiveExpression("value1"));
            conState1.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Statements Region"));
            constructor1.Statements.Add(conState1);
            CodeStatement conState2 = new CodeAssignStatement(
                                        new CodeFieldReferenceExpression(
                                            new CodeThisReferenceExpression(),
                                            "field2"),
                                        new CodePrimitiveExpression("value2"));
            conState2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));
            constructor1.Statements.Add(conState2);

            constructor1.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Constructor Region"));
            constructor1.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeConstructor constructor2 = new CodeConstructor();
            constructor2.Attributes = (constructor2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            constructor2.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "value1"));
            constructor2.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "value2"));

            CodeTypeConstructor typeConstructor2 = new CodeTypeConstructor();

            typeConstructor2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Type Constructor Region"));
            typeConstructor2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeEntryPointMethod methodMain = new CodeEntryPointMethod();

            CodeTypeDeclaration nestedClass1 = new CodeTypeDeclaration("NestedClass1");
            CodeTypeDeclaration nestedClass2 = new CodeTypeDeclaration("NestedClass2");
            nestedClass2.LinePragma = new CodeLinePragma("NestedTypeLinePragma.txt", 400);
            nestedClass2.Comments.Add(new CodeCommentStatement("Nested Type Comment"));

            nestedClass2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Nested Type Region"));
            nestedClass2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeTypeDelegate delegate1 = new CodeTypeDelegate();
            delegate1.Name = "nestedDelegate1";
            delegate1.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("System.Object"), "sender"));
            delegate1.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("System.EventArgs"), "e"));

            CodeTypeDelegate delegate2 = new CodeTypeDelegate();
            delegate2.Name = "nestedDelegate2";
            delegate2.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("System.Object"), "sender"));
            delegate2.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("System.EventArgs"), "e"));

            delegate2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Delegate Region"));
            delegate2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            var snippet1 = new CodeSnippetTypeMember();
            var snippet2 = new CodeSnippetTypeMember();

            CodeRegionDirective regionStart = new CodeRegionDirective(CodeRegionMode.End, "");
            regionStart.RegionText = "Snippet Region";
            regionStart.RegionMode = CodeRegionMode.Start;
            snippet2.StartDirectives.Add(regionStart);
            snippet2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            cd.Members.Add(field1);
            cd.Members.Add(method1);
            cd.Members.Add(constructor1);
            cd.Members.Add(property1);
            cd.Members.Add(methodMain);

            cd.Members.Add(evt1);
            cd.Members.Add(nestedClass1);
            cd.Members.Add(delegate1);

            cd.Members.Add(snippet1);

            cd.Members.Add(field2);
            cd.Members.Add(method2);
            cd.Members.Add(constructor2);
            cd.Members.Add(property2);

            cd.Members.Add(typeConstructor2);
            cd.Members.Add(evt2);
            cd.Members.Add(nestedClass2);
            cd.Members.Add(delegate2);
            cd.Members.Add(snippet2);

            AssertEqual(cu,
                @"#region Compile Unit Region
                  //------------------------------------------------------------------------------
                  // <auto-generated>
                  //     This code was generated by a tool.
                  //     Runtime Version:4.0.30319.42000
                  //
                  //     Changes to this file may cause incorrect behavior and will be lost if
                  //     the code is regenerated.
                  // </auto-generated>
                  //------------------------------------------------------------------------------

                  namespace Namespace1 {


                      #region Outer Type Region
                      // Outer Type Comment
                      public class Class1 {

                          // Field 1 Comment
                          private string field1;

                          #region Field Region
                          private string field2;
                          #endregion


                          #region Snippet Region
                  #endregion


                          #region Type Constructor Region
                          static Class1() {
                          }
                          #endregion

                          #region Constructor Region
                          public Class1() {
                              #region Statements Region
                              this.field1 = ""value1"";
                              this.field2 = ""value2"";
                #endregion
                        }
                #endregion

                        public Class1(string value1, string value2)
                        {
                        }

                        public string Property1
                        {
                            get
                            {
                                return this.field1;
                            }
                        }

                        #region Property Region
                        public string Property2
                        {
                            get
                            {
                                return this.field2;
                            }
                        }
                        #endregion

                        public event System.EventHandler Event1;

                        #region Event Region
                        public event System.EventHandler Event2;
                        #endregion

                        public void Method1()
                        {
                            this.Event1(this, System.EventArgs.Empty);
                        }

                        public static void Main()
                        {
                        }

                        #region Method Region
                        // Method 2 Comment

                #line 500 ""MethodLinePragma.txt""
                        public void Method2()
                        {
                            this.Event2(this, System.EventArgs.Empty);
                        }

                #line default
                #line hidden
                        #endregion

                        public class NestedClass1
                        {
                        }

                        public delegate void nestedDelegate1(object sender, System.EventArgs e);

                        #region Nested Type Region
                        // Nested Type Comment

                #line 400 ""NestedTypeLinePragma.txt""
                        public class NestedClass2
                        {
                        }

                #line default
                #line hidden
                        #endregion

                        #region Delegate Region
                        public delegate void nestedDelegate2(object sender, System.EventArgs e);
                        #endregion
                    }
                #endregion
                }
                  #endregion");
        }
        public void RegionsSnippetsAndLinePragmas()
        {
            CodeCompileUnit cu = new CodeCompileUnit();
            CodeNamespace ns = new CodeNamespace("Namespace1");

            cu.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Compile Unit Region"));
            cu.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            cu.Namespaces.Add(ns);

            CodeTypeDeclaration cd = new CodeTypeDeclaration("Class1");
            ns.Types.Add(cd);

            cd.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Outer Type Region"));
            cd.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            cd.Comments.Add(new CodeCommentStatement("Outer Type Comment"));

            CodeMemberField field1 = new CodeMemberField(typeof(String), "field1");
            CodeMemberField field2 = new CodeMemberField(typeof(String), "field2");
            field1.Comments.Add(new CodeCommentStatement("Field 1 Comment"));
            field2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Field Region"));
            field2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeMemberEvent evt1 = new CodeMemberEvent();
            evt1.Name = "Event1";
            evt1.Type = new CodeTypeReference(typeof(System.EventHandler));
            evt1.Attributes = (evt1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;

            CodeMemberEvent evt2 = new CodeMemberEvent();
            evt2.Name = "Event2";
            evt2.Type = new CodeTypeReference(typeof(System.EventHandler));
            evt2.Attributes = (evt2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;

            evt2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Event Region"));
            evt2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeMemberMethod method1 = new CodeMemberMethod();
            method1.Name = "Method1";
            method1.Attributes = (method1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method1.Statements.Add(
                new CodeDelegateInvokeExpression(
                    new CodeEventReferenceExpression(new CodeThisReferenceExpression(), "Event1"),
                    new CodeExpression[] {
                        new CodeThisReferenceExpression(),
                        new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.EventArgs"), "Empty")
                    }));

            CodeMemberMethod method2 = new CodeMemberMethod();
            method2.Name = "Method2";
            method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method2.Statements.Add(
                new CodeDelegateInvokeExpression(
                    new CodeEventReferenceExpression(new CodeThisReferenceExpression(), "Event2"),
                    new CodeExpression[] {
                    new CodeThisReferenceExpression(),
                    new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.EventArgs"), "Empty")
                    }));
            method2.LinePragma = new CodeLinePragma("MethodLinePragma.txt", 500);
            method2.Comments.Add(new CodeCommentStatement("Method 2 Comment"));

            method2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Method Region"));
            method2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeMemberProperty property1 = new CodeMemberProperty();
            property1.Name = "Property1";
            property1.Type = new CodeTypeReference(typeof(string));
            property1.Attributes = (property1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property1.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeFieldReferenceExpression(
                        new CodeThisReferenceExpression(),
                        "field1")));

            CodeMemberProperty property2 = new CodeMemberProperty();
            property2.Name = "Property2";
            property2.Type = new CodeTypeReference(typeof(string));
            property2.Attributes = (property2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property2.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeFieldReferenceExpression(
                        new CodeThisReferenceExpression(),
                        "field2")));

            property2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Property Region"));
            property2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeConstructor constructor1 = new CodeConstructor();
            constructor1.Attributes = (constructor1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            CodeStatement conState1 = new CodeAssignStatement(
                                        new CodeFieldReferenceExpression(
                                            new CodeThisReferenceExpression(),
                                            "field1"),
                                        new CodePrimitiveExpression("value1"));
            conState1.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Statements Region"));
            constructor1.Statements.Add(conState1);
            CodeStatement conState2 = new CodeAssignStatement(
                                        new CodeFieldReferenceExpression(
                                            new CodeThisReferenceExpression(),
                                            "field2"),
                                        new CodePrimitiveExpression("value2"));
            conState2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));
            constructor1.Statements.Add(conState2);

            constructor1.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Constructor Region"));
            constructor1.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeConstructor constructor2 = new CodeConstructor();
            constructor2.Attributes = (constructor2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            constructor2.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "value1"));
            constructor2.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "value2"));

            CodeTypeConstructor typeConstructor2 = new CodeTypeConstructor();

            typeConstructor2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Type Constructor Region"));
            typeConstructor2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeEntryPointMethod methodMain = new CodeEntryPointMethod();

            CodeTypeDeclaration nestedClass1 = new CodeTypeDeclaration("NestedClass1");
            CodeTypeDeclaration nestedClass2 = new CodeTypeDeclaration("NestedClass2");
            nestedClass2.LinePragma = new CodeLinePragma("NestedTypeLinePragma.txt", 400);
            nestedClass2.Comments.Add(new CodeCommentStatement("Nested Type Comment"));

            nestedClass2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Nested Type Region"));
            nestedClass2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            CodeTypeDelegate delegate1 = new CodeTypeDelegate();
            delegate1.Name = "nestedDelegate1";
            delegate1.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("System.Object"), "sender"));
            delegate1.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("System.EventArgs"), "e"));

            CodeTypeDelegate delegate2 = new CodeTypeDelegate();
            delegate2.Name = "nestedDelegate2";
            delegate2.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("System.Object"), "sender"));
            delegate2.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("System.EventArgs"), "e"));

            delegate2.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Delegate Region"));
            delegate2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            var snippet1 = new CodeSnippetTypeMember();
            var snippet2 = new CodeSnippetTypeMember();

            CodeRegionDirective regionStart = new CodeRegionDirective(CodeRegionMode.End, "");
            regionStart.RegionText = "Snippet Region";
            regionStart.RegionMode = CodeRegionMode.Start;
            snippet2.StartDirectives.Add(regionStart);
            snippet2.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            cd.Members.Add(field1);
            cd.Members.Add(method1);
            cd.Members.Add(constructor1);
            cd.Members.Add(property1);
            cd.Members.Add(methodMain);

            cd.Members.Add(evt1);
            cd.Members.Add(nestedClass1);
            cd.Members.Add(delegate1);

            cd.Members.Add(snippet1);

            cd.Members.Add(field2);
            cd.Members.Add(method2);
            cd.Members.Add(constructor2);
            cd.Members.Add(property2);

            cd.Members.Add(typeConstructor2);
            cd.Members.Add(evt2);
            cd.Members.Add(nestedClass2);
            cd.Members.Add(delegate2);
            cd.Members.Add(snippet2);

            AssertEqual(cu,
                @"#Region ""Compile Unit Region""
                  '------------------------------------------------------------------------------
                  ' <auto-generated>
                  '     This code was generated by a tool.
                  '     Runtime Version:4.0.30319.42000
                  '
                  '     Changes to this file may cause incorrect behavior and will be lost if
                  '     the code is regenerated.
                  ' </auto-generated>
                  '------------------------------------------------------------------------------
                  Option Strict Off
                  Option Explicit On
                  Namespace Namespace1
                      #Region ""Outer Type Region""
                      'Outer Type Comment
                      Public Class Class1
                          'Field 1 Comment
                          Private field1 As String
                          #Region ""Field Region""
                          Private field2 As String
                          #End Region
                          #Region ""Snippet Region""
                          #End Region
                          #Region ""Type Constructor Region""
                          Shared Sub New()
                          End Sub
                          #End Region
                          #Region ""Constructor Region""
                          Public Sub New()
                              MyBase.New
                              Me.field1 = ""value1""
                              Me.field2 = ""value2""
                          End Sub
                          #End Region
                          Public Sub New(ByVal value1 As String, ByVal value2 As String)
                              MyBase.New
                          End Sub
                          Public ReadOnly Property Property1() As String
                              Get
                                  Return Me.field1
                              End Get
                          End Property
                          #Region ""Property Region""
                          Public ReadOnly Property Property2() As String
                              Get
                                  Return Me.field2
                              End Get
                          End Property
                          #End Region
                          Public Event Event1 As System.EventHandler
                          #Region ""Event Region""
                          Public Event Event2 As System.EventHandler
                          #End Region
                          Public Sub Method1()
                              RaiseEvent Event1(Me, System.EventArgs.Empty)
                          End Sub
                          Public Shared Sub Main()
                          End Sub
                          #Region ""Method Region""
                          'Method 2 Comment
                          #ExternalSource(""MethodLinePragma.txt"",500)
                          Public Sub Method2()
                              RaiseEvent Event2(Me, System.EventArgs.Empty)
                          End Sub
                          #End ExternalSource
                          #End Region
                          Public Class NestedClass1
                          End Class
                          Public Delegate Sub nestedDelegate1(ByVal sender As Object, ByVal e As System.EventArgs)
                          #Region ""Nested Type Region""
                          'Nested Type Comment
                          #ExternalSource(""NestedTypeLinePragma.txt"",400)
                          Public Class NestedClass2
                          End Class
                          #End ExternalSource
                          #End Region
                          #Region ""Delegate Region""
                          Public Delegate Sub nestedDelegate2(ByVal sender As Object, ByVal e As System.EventArgs)
                          #End Region
                      End Class
                      #End Region
                  End Namespace
                  #End Region");
        }
        public void GlobalKeyword()
        {
            CodeNamespace ns = new CodeNamespace("Foo");
            ns.Comments.Add(new CodeCommentStatement("Foo namespace"));

            CodeTypeDeclaration cd = new CodeTypeDeclaration("Foo");
            ns.Types.Add(cd);

            string fieldName1 = "_verifyGlobalGeneration1";
            CodeMemberField field = new CodeMemberField();
            field.Name = fieldName1;
            field.Type = new CodeTypeReference(typeof(int), CodeTypeReferenceOptions.GlobalReference);
            field.Attributes = MemberAttributes.Public;
            field.InitExpression = new CodePrimitiveExpression(int.MaxValue);
            cd.Members.Add(field);

            string fieldName2 = "_verifyGlobalGeneration2";
            CodeMemberField field2 = new CodeMemberField();
            field2.Name = fieldName2;
            CodeTypeReference typeRef = new CodeTypeReference("System.Nullable", CodeTypeReferenceOptions.GlobalReference);
            typeRef.TypeArguments.Add(new CodeTypeReference(typeof(int), CodeTypeReferenceOptions.GlobalReference));
            field2.Type = typeRef;
            field2.InitExpression = new CodePrimitiveExpression(0);
            cd.Members.Add(field2);

            CodeMemberMethod method1 = new CodeMemberMethod();
            method1.Name = "TestMethod01";
            method1.Attributes = (method1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public | MemberAttributes.Static;
            method1.ReturnType = new CodeTypeReference(typeof(int));
            method1.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(int.MaxValue)));
            cd.Members.Add(method1);

            CodeMemberMethod method2 = new CodeMemberMethod();
            method2.Name = "TestMethod02";
            method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method2.ReturnType = new CodeTypeReference(typeof(int));
            method2.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "iReturn"));

            CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression(
                                              new CodeMethodReferenceExpression(
                                              new CodeTypeReferenceExpression(new CodeTypeReference("Foo.Foo", CodeTypeReferenceOptions.GlobalReference)), "TestMethod01"));
            CodeAssignStatement cas = new CodeAssignStatement(new CodeVariableReferenceExpression("iReturn"), cmie);
            method2.Statements.Add(cas);
            method2.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("iReturn")));
            cd.Members.Add(method2);

            CodeMemberMethod method3 = new CodeMemberMethod();
            method3.Name = "TestMethod03";
            method3.Attributes = (method3.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method3.ReturnType = new CodeTypeReference(typeof(int));
            method3.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "iReturn"));
            CodeTypeReferenceOptions ctro = CodeTypeReferenceOptions.GlobalReference;
            CodeTypeReference ctr = new CodeTypeReference(typeof(Math), ctro);
            cmie = new CodeMethodInvokeExpression(
                                              new CodeMethodReferenceExpression(
                                              new CodeTypeReferenceExpression(ctr), "Abs"), new CodeExpression[] { new CodePrimitiveExpression(-1) });
            cas = new CodeAssignStatement(new CodeVariableReferenceExpression("iReturn"), cmie);
            method3.Statements.Add(cas);
            method3.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("iReturn")));
            cd.Members.Add(method3);

            CodeMemberProperty property = new CodeMemberProperty();
            property.Name = "GlobalTestProperty1";
            property.Type = new CodeTypeReference(typeof(int));
            property.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(fieldName1)));
            property.SetStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(fieldName1), new CodeVariableReferenceExpression("value")));
            cd.Members.Add(property);

            CodeMemberProperty property2 = new CodeMemberProperty();
            property2.Name = "GlobalTestProperty2";
            property2.Type = typeRef;
            property2.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property2.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(fieldName2)));
            property2.SetStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(fieldName2), new CodeVariableReferenceExpression("value")));
            cd.Members.Add(property2);

            AssertEqual(ns,
                @"'Foo namespace
                  Namespace Foo
                      Public Class Foo
                          Public _verifyGlobalGeneration1 As Integer = 2147483647
                          Private _verifyGlobalGeneration2 As Global.System.Nullable(Of Integer) = 0
                          Public Property GlobalTestProperty1() As Integer
                              Get
                                  Return _verifyGlobalGeneration1
                              End Get
                              Set
                                  _verifyGlobalGeneration1 = value
                              End Set
                          End Property
                          Public Property GlobalTestProperty2() As Global.System.Nullable(Of Integer)
                              Get
                                  Return _verifyGlobalGeneration2
                              End Get
                              Set
                                  _verifyGlobalGeneration2 = value
                              End Set
                          End Property
                          Public Shared Function TestMethod01() As Integer
                              Return 2147483647
                          End Function
                          Public Function TestMethod02() As Integer
                              Dim iReturn As Integer
                              iReturn = Global.Foo.Foo.TestMethod01
                              Return iReturn
                          End Function
                          Public Function TestMethod03() As Integer
                              Dim iReturn As Integer
                              iReturn = Global.System.Math.Abs(-1)
                              Return iReturn
                          End Function
                      End Class
                  End Namespace");
        }
Exemple #5
0
 public CodeAssignStatement Assign(CodeExpression lhs, CodeExpression rhs)
 {
     var ass = new CodeAssignStatement(lhs, rhs);
     Scope.Add(ass);
     return ass;
 }
Exemple #6
0
 protected abstract void GenerateAssignStatement(CodeAssignStatement e);
Exemple #7
0
	static void BuildCity (City c, CodeMemberMethod method, CodeFieldReferenceExpression curVar,
			       CodeFieldReferenceExpression curLocation)
	{
		CodeAssignStatement cityAssign = new CodeAssignStatement ();
		cityAssign.Left = curCity;
		cityAssign.Right = new CodeCastExpression (cityType, curLocation);
		method.Statements.Add (cityAssign);

		CodeMethodInvokeExpression locationAdd;
		
		foreach (Location l in c.Locations) {
			locationAdd = new CodeMethodInvokeExpression (
				curCity,
				"Add",
				new CodeExpression[] {BuildCityLocation (l, method)}
			);
			method.Statements.Add (locationAdd);
		}
	}
        public void GlobalKeyword()
        {
            CodeNamespace ns = new CodeNamespace("Foo");
            ns.Comments.Add(new CodeCommentStatement("Foo namespace"));

            var cd = new CodeTypeDeclaration("Foo");
            ns.Types.Add(cd);

            string fieldName1 = "_verifyGlobalGeneration1";
            CodeMemberField field = new CodeMemberField();
            field.Name = fieldName1;
            field.Type = new CodeTypeReference(typeof(int), CodeTypeReferenceOptions.GlobalReference);
            field.Attributes = MemberAttributes.Public;
            field.InitExpression = new CodePrimitiveExpression(int.MaxValue);
            cd.Members.Add(field);

            string fieldName2 = "_verifyGlobalGeneration2";
            CodeMemberField field2 = new CodeMemberField();
            field2.Name = fieldName2;
            CodeTypeReference typeRef = new CodeTypeReference("System.Nullable", CodeTypeReferenceOptions.GlobalReference);
            typeRef.TypeArguments.Add(new CodeTypeReference(typeof(int), CodeTypeReferenceOptions.GlobalReference));
            field2.Type = typeRef;
            field2.InitExpression = new CodePrimitiveExpression(0);
            cd.Members.Add(field2);

            CodeMemberMethod method1 = new CodeMemberMethod();
            method1.Name = "TestMethod01";
            method1.Attributes = (method1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public | MemberAttributes.Static;
            method1.ReturnType = new CodeTypeReference(typeof(int));
            method1.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(int.MaxValue)));
            cd.Members.Add(method1);

            CodeMemberMethod method2 = new CodeMemberMethod();
            method2.Name = "TestMethod02";
            method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method2.ReturnType = new CodeTypeReference(typeof(int));
            method2.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "iReturn"));

            CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression(
                                              new CodeMethodReferenceExpression(
                                              new CodeTypeReferenceExpression(new CodeTypeReference("Foo.Foo", CodeTypeReferenceOptions.GlobalReference)), "TestMethod01"));
            CodeAssignStatement cas = new CodeAssignStatement(new CodeVariableReferenceExpression("iReturn"), cmie);
            method2.Statements.Add(cas);
            method2.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("iReturn")));
            cd.Members.Add(method2);

            CodeMemberMethod method3 = new CodeMemberMethod();
            method3.Name = "TestMethod03";
            method3.Attributes = (method3.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method3.ReturnType = new CodeTypeReference(typeof(int));
            method3.Statements.Add(new CodeVariableDeclarationStatement(typeof(int), "iReturn"));
            CodeTypeReferenceOptions ctro = CodeTypeReferenceOptions.GlobalReference;
            CodeTypeReference ctr = new CodeTypeReference(typeof(Math), ctro);
            cmie = new CodeMethodInvokeExpression(
                                              new CodeMethodReferenceExpression(
                                              new CodeTypeReferenceExpression(ctr), "Abs"), new CodeExpression[] { new CodePrimitiveExpression(-1) });
            cas = new CodeAssignStatement(new CodeVariableReferenceExpression("iReturn"), cmie);
            method3.Statements.Add(cas);
            method3.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("iReturn")));
            cd.Members.Add(method3);

            CodeMemberProperty property = new CodeMemberProperty();
            property.Name = "GlobalTestProperty1";
            property.Type = new CodeTypeReference(typeof(int));
            property.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(fieldName1)));
            property.SetStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(fieldName1), new CodeVariableReferenceExpression("value")));
            cd.Members.Add(property);

            CodeMemberProperty property2 = new CodeMemberProperty();
            property2.Name = "GlobalTestProperty2";
            property2.Type = typeRef;
            property2.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property2.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(fieldName2)));
            property2.SetStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(fieldName2), new CodeVariableReferenceExpression("value")));
            cd.Members.Add(property2);

            AssertEqual(ns,
                @"// Foo namespace
                  namespace Foo {
                      public class Foo {
                          public int _verifyGlobalGeneration1 = 2147483647;

                          private global::System.Nullable<int> _verifyGlobalGeneration2 = 0;

                          public int GlobalTestProperty1 {
                              get {
                                  return _verifyGlobalGeneration1;
                              }
                              set {
                                  _verifyGlobalGeneration1 = value;
                              }
                          }

                          public global::System.Nullable<int> GlobalTestProperty2 {
                              get {
                                  return _verifyGlobalGeneration2;
                              }
                              set {
                                  _verifyGlobalGeneration2 = value;
                              }
                          }

                          public static int TestMethod01() {
                              return 2147483647;
                          }

                          public int TestMethod02() {
                              int iReturn;
                              iReturn = global::Foo.Foo.TestMethod01();
                              return iReturn;
                          }

                          public int TestMethod03() {
                              int iReturn;
                              iReturn = global::System.Math.Abs(-1);
                              return iReturn;
                          }
                      }
                  }");
        }
			// CodeStatement

			public void Visit(CodeAssignStatement o)
			{
				g.GenerateAssignStatement(o);
			}
    public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
        // create a namespace
        CodeNamespace ns = new CodeNamespace ("NS");
        ns.Imports.Add (new CodeNamespaceImport ("System"));
        cu.Namespaces.Add (ns);

        // create a class
        CodeTypeDeclaration class1 = new CodeTypeDeclaration ();
        class1.Name = "Test";
        class1.IsClass = true;
        ns.Types.Add (class1);

        if (Supports (provider, GeneratorSupport.TryCatchStatements)) {

            // try catch statement with just finally
            // GENERATE (C#):
            //       public static int FirstScenario(int a) {
            //            try {
            //            }
            //            finally {
            //                a = (a + 5);
            //            }
            //            return a;
            //        }
            AddScenario ("CheckFirstScenario");
            CodeMemberMethod cmm = new CodeMemberMethod ();
            cmm.Name = "FirstScenario";
            cmm.ReturnType = new CodeTypeReference (typeof (int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (typeof (int), "a");
            cmm.Parameters.Add (param);

            CodeTryCatchFinallyStatement tcfstmt = new CodeTryCatchFinallyStatement ();
            tcfstmt.FinallyStatements.Add (new CodeAssignStatement (new CodeArgumentReferenceExpression ("a"), new
                CodeBinaryOperatorExpression (new CodeArgumentReferenceExpression ("a"), CodeBinaryOperatorType.Add,
                new CodePrimitiveExpression (5))));
            cmm.Statements.Add (tcfstmt);
            cmm.Statements.Add (new CodeMethodReturnStatement (new CodeArgumentReferenceExpression ("a")));
            class1.Members.Add (cmm);

            // in VB (a = a/a) generates an warning if a is integer. Cast the expression just for VB language. 
            CodeBinaryOperatorExpression cboExpression   = new CodeBinaryOperatorExpression (new CodeArgumentReferenceExpression ("a"), CodeBinaryOperatorType.Divide, new CodeArgumentReferenceExpression ("a"));
            CodeAssignStatement          assignStatement = null;
            if (provider is Microsoft.VisualBasic.VBCodeProvider)
                assignStatement = new CodeAssignStatement (new CodeArgumentReferenceExpression ("a"), new CodeCastExpression (typeof (int), cboExpression));
            else
                assignStatement = new CodeAssignStatement (new CodeArgumentReferenceExpression ("a"), cboExpression);

            // try catch statement with just catch
            // GENERATE (C#):
            //        public static int SecondScenario(int a, string exceptionMessage) {
            //            try {
            //                a = (a / a);
            //            }
            //            catch (System.Exception e) {
            //                a = 3;
            //                exceptionMessage = e.ToString();
            //            }
            //            finally {
            //                a = (a + 1);
            //            }
            //            return a;
            //        }
            AddScenario ("CheckSecondScenario");
            cmm = new CodeMemberMethod ();
            cmm.Name = "SecondScenario";
            cmm.ReturnType = new CodeTypeReference (typeof (int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression (typeof (int), "a");
            cmm.Parameters.Add (param);
            cmm.Parameters.Add (new CodeParameterDeclarationExpression (typeof (String), "exceptionMessage"));

            tcfstmt = new CodeTryCatchFinallyStatement ();
            CodeCatchClause catchClause = new CodeCatchClause ("e");
            tcfstmt.TryStatements.Add (assignStatement);
            catchClause.Statements.Add (new CodeAssignStatement (new CodeArgumentReferenceExpression ("a"),
                new CodePrimitiveExpression (3)));
            catchClause.Statements.Add (new CodeAssignStatement (new CodeArgumentReferenceExpression ("exceptionMessage"),
                new CodeMethodInvokeExpression (new CodeVariableReferenceExpression ("e"), "ToString")));
            tcfstmt.CatchClauses.Add (catchClause);
            tcfstmt.FinallyStatements.Add (CDHelper.CreateIncrementByStatement (new CodeArgumentReferenceExpression ("a"), 1));

            cmm.Statements.Add (tcfstmt);
            cmm.Statements.Add (new CodeMethodReturnStatement (new CodeArgumentReferenceExpression ("a")));

            class1.Members.Add (cmm);

            // try catch statement with multiple catches
            // GENERATE (C#):
            //        public static int ThirdScenario(int a, string exceptionMessage) {
            //            try {
            //                a = (a / a);
            //            }
            //            catch (System.ArgumentNullException e) {
            //                a = 10;
            //                exceptionMessage = e.ToString();
            //            }
            //            catch (System.DivideByZeroException f) {
            //                exceptionMessage = f.ToString();
            //                a = 9;
            //            }
            //            return a;
            //        }
            AddScenario ("CheckThirdScenario");
            cmm = new CodeMemberMethod ();
            cmm.Name = "ThirdScenario";
            cmm.ReturnType = new CodeTypeReference (typeof (int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression (typeof (int), "a");
            cmm.Parameters.Add (param);
            cmm.Parameters.Add (new CodeParameterDeclarationExpression (typeof (String), "exceptionMessage"));

            tcfstmt = new CodeTryCatchFinallyStatement ();
            catchClause = new CodeCatchClause ("e", new CodeTypeReference (typeof (ArgumentNullException)));
            tcfstmt.TryStatements.Add (assignStatement);
            catchClause.Statements.Add (new CodeAssignStatement (new CodeArgumentReferenceExpression ("a"),
                new CodePrimitiveExpression (9)));
            catchClause.Statements.Add (new CodeAssignStatement (new CodeArgumentReferenceExpression ("exceptionMessage"),
                new CodeMethodInvokeExpression (new CodeVariableReferenceExpression ("e"), "ToString")));
            tcfstmt.CatchClauses.Add (catchClause);

            // add a second catch clause
            catchClause = new CodeCatchClause ("f", new CodeTypeReference (typeof (Exception)));
            catchClause.Statements.Add (new CodeAssignStatement (new CodeArgumentReferenceExpression ("exceptionMessage"),
                new CodeMethodInvokeExpression (new CodeVariableReferenceExpression ("f"), "ToString")));
            catchClause.Statements.Add (new CodeAssignStatement (new CodeArgumentReferenceExpression ("a"),
                new CodePrimitiveExpression (9)));
            tcfstmt.CatchClauses.Add (catchClause);

            cmm.Statements.Add (tcfstmt);
            cmm.Statements.Add (new CodeMethodReturnStatement (new CodeArgumentReferenceExpression ("a")));
            class1.Members.Add (cmm);

            // catch throws exception
            // GENERATE (C#):
            //        public static int FourthScenario(int a) {
            //            try {
            //                a = (a / a);
            //            }
            //            catch (System.Exception e) {
            //                // Error handling
            //                throw e;
            //            }
            //            return a;
            //        }
            AddScenario ("CheckFourthScenario");
            cmm = new CodeMemberMethod ();
            cmm.Name = "FourthScenario";
            cmm.ReturnType = new CodeTypeReference (typeof (int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression (typeof (int), "a");
            cmm.Parameters.Add (param);

            tcfstmt = new CodeTryCatchFinallyStatement ();
            catchClause = new CodeCatchClause ("e");
            tcfstmt.TryStatements.Add (assignStatement);
            catchClause.Statements.Add (new CodeCommentStatement ("Error handling"));
            catchClause.Statements.Add (new CodeThrowExceptionStatement (new CodeArgumentReferenceExpression ("e")));
            tcfstmt.CatchClauses.Add (catchClause);
            cmm.Statements.Add (tcfstmt);
            cmm.Statements.Add (new CodeMethodReturnStatement (new CodeArgumentReferenceExpression ("a")));
            class1.Members.Add (cmm);
        }
    }
    public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
        if (!(provider is JScriptCodeProvider)) {
            // GENERATES (C#):
            //   namespace Foo {
            //       public class Foo {
            //
            //           public int _verifyGlobalGeneration1 = 2147483647;
            //
            //           public int System {
            //              get { return 42; }
            //           }
            //   
            //           public int Property {
            //               get { return 2147483647; }
            //           }
            //
            //           public int GlobalTestProperty1 {
            //               get {
            //                   return _verifyGlobalGeneration1;
            //               }
            //               set {
            //                   _verifyGlobalGeneration1 = value;
            //               }
            //           }
            //         
            //           public global::System.Nullable<int> GlobalTestProperty2 {
            //               get {
            //                   return _verifyGlobalGeneration2;
            //               }
            //               set {
            //                   _verifyGlobalGeneration2 = value;
            //               }
            //           }
            //           
            //   
            //           public int TestMethod02() {
            //               int iReturn;
            //               iReturn = global::Foo.Foo.Property;
            //               return iReturn;
            //           }
            //   
            //           public int TestMethod03() {
            //               int iReturn;
            //               iReturn = global::System.Math.Abs(-1);
            //               return iReturn;
            //           }
            //
            //           public int TestMethod04() {
            //               int iReturn;
            //               iReturn = System;
            //               return iReturn;
            //       }
            //   }

            CodeNamespace ns = new CodeNamespace ("Foo");
            ns.Comments.Add (new CodeCommentStatement ("Foo namespace"));

            cu.Namespaces.Add (ns);

            CodeTypeDeclaration cd = new CodeTypeDeclaration ("Foo");
            ns.Types.Add (cd);

            CodeMemberProperty property = new CodeMemberProperty ();
            property.Name = "System";
            property.Attributes = MemberAttributes.Public;
            property.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private | MemberAttributes.Static;
            property.Type = new CodeTypeReference (typeof (int));
            property.GetStatements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (42)));
            cd.Members.Add (property);

            property = new CodeMemberProperty ();
            property.Name = "Property";
            property.Attributes = (property.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private | MemberAttributes.Static;
            property.Type = new CodeTypeReference (typeof (int));
            property.GetStatements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (Int32.MaxValue)));
            cd.Members.Add (property);

            AddScenario ("CallTestMethod02", "Call Foo.Foo.TestMethod02.");
            CodeMemberMethod method2 = new CodeMemberMethod ();
            method2.Name = "TestMethod02";
            method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method2.ReturnType = new CodeTypeReference (typeof (int));
            method2.Statements.Add (new CodeVariableDeclarationStatement (typeof (int), "iReturn"));

            CodePropertyReferenceExpression cpr = new CodePropertyReferenceExpression (
                                              new CodeTypeReferenceExpression (new CodeTypeReference ("Foo.Foo",
                                                      CodeTypeReferenceOptions.GlobalReference)), "Property");

            CodeAssignStatement cas = new CodeAssignStatement (new CodeVariableReferenceExpression ("iReturn"), cpr);
            method2.Statements.Add (cas);
            method2.Statements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("iReturn")));
            cd.Members.Add (method2);

            AddScenario ("CallTestMethod03", "Call Foo.Foo.TestMethod02.");
            CodeMemberMethod method3 = new CodeMemberMethod ();
            method3.Name = "TestMethod03";
            method3.Attributes = (method3.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method3.ReturnType = new CodeTypeReference (typeof (int));
            method3.Statements.Add (new CodeVariableDeclarationStatement (typeof (int), "iReturn"));
            CodeTypeReferenceOptions ctro = CodeTypeReferenceOptions.GlobalReference;
            CodeTypeReference ctr = new CodeTypeReference (typeof (Math), ctro);
            CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression (
                                              new CodeMethodReferenceExpression (
                                              new CodeTypeReferenceExpression (ctr), "Abs"), new CodeExpression[] { new CodePrimitiveExpression (-1) });
            cas = new CodeAssignStatement (new CodeVariableReferenceExpression ("iReturn"), cmie);
            method3.Statements.Add (cas);
            method3.Statements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("iReturn")));
            cd.Members.Add (method3);

            AddScenario ("CallTestMethod04", "Call Foo.Foo.TestMethod04.");
            CodeMemberMethod method4 = new CodeMemberMethod ();
            method4.Name = "TestMethod04";
            method4.Attributes = (method4.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method4.ReturnType = new CodeTypeReference (typeof (int));
            method4.Statements.Add (new CodeVariableDeclarationStatement (typeof (int), "iReturn"));

            cpr = new CodePropertyReferenceExpression (null, "System");

            cas = new CodeAssignStatement (new CodeVariableReferenceExpression ("iReturn"), cpr);
            method4.Statements.Add (cas);
            method4.Statements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("iReturn")));
            cd.Members.Add (method4);

            // Verify that what CodeTypeReferenceOptions are correctly set. 
            // Basically this check gives the code coverage for the get property
            AddScenario ("CTR_GetGlobalRefCheck", "Check that CodeTypeReference.Options gives the proper value.");
            if (ctr.Options == CodeTypeReferenceOptions.GlobalReference)
                VerifyScenario ("CTR_GetGlobalRefCheck");

            // one-off generate statements
            StringWriter sw = new StringWriter ();

            // global shouldn't be generated in this instance
            CodeTypeReference variableType = new CodeTypeReference (typeof (System.String), CodeTypeReferenceOptions.GlobalReference);
            CodeVariableDeclarationStatement variable = new CodeVariableDeclarationStatement (variableType, "myVariable");
            provider.GenerateCodeFromStatement (variable, sw, null);

            // global should be generated in this instance
            CodeTypeReference variableType2 = new CodeTypeReference (typeof (System.Array), CodeTypeReferenceOptions.GlobalReference);
            CodeVariableDeclarationStatement variable2 = new CodeVariableDeclarationStatement (variableType2, "myVariable2");
            provider.GenerateCodeFromStatement (variable2, sw, null);

            AddScenario ("GlobalKeywordShouldExist", "When an array is referred to, a global qualifier should be generated on it.");
            if (sw.ToString ().IndexOf ("global") != -1 && sw.ToString ().IndexOf ("Global") != -1) {
                LogMessage ("Global keyword does not exist in statement: " + sw.ToString ());
            }
            else
                VerifyScenario ("GlobalKeywordShouldExist");
        }
    }
    public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {

        // create a namespace
        CodeNamespace ns = new CodeNamespace ("NS");
        ns.Imports.Add (new CodeNamespaceImport ("System"));
        cu.Namespaces.Add (ns);

        // create a class
        CodeTypeDeclaration class1 = new CodeTypeDeclaration ();
        class1.Name = "Test";
        class1.IsClass = true;
        ns.Types.Add (class1);

        if (Supports (provider, GeneratorSupport.GotoStatements))  {
            // create first method to test gotos that jump ahead to a defined label with statement
            //     GENERATE (C#):
            //            public static int FirstMethod(int i) {
            //                if ((i < 1)) {
            //                    goto comehere;
            //                }
            //                return 6;
            //            comehere:
            //                return 7;
            //            }
            AddScenario ("CheckFirstMethod");
            CodeMemberMethod cmm = new CodeMemberMethod ();
            cmm.Name = "FirstMethod";
            cmm.ReturnType = new CodeTypeReference (typeof (int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (typeof (int), "i");
            cmm.Parameters.Add (param);
            CodeConditionStatement condstmt = new CodeConditionStatement (new CodeBinaryOperatorExpression (
                new CodeArgumentReferenceExpression ("i"), CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression (1)),
                new CodeGotoStatement ("comehere"));
            cmm.Statements.Add (condstmt);
            cmm.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (6)));
            cmm.Statements.Add (new CodeLabeledStatement ("comehere",
                new CodeMethodReturnStatement (new CodePrimitiveExpression (7))));
            class1.Members.Add (cmm);

            // create second method to test gotos that jump ahead to a defined label without a statement attached to it
            //     GENERATE (C#):
            //            public static int SecondMethod(int i) {
            //                if ((i < 1)) {
            //                    goto comehere;
            //                    return 5;
            //                }
            //                return 6;
            //            comehere:
            //                return 7;
            //            }
            AddScenario ("CheckSecondMethod");
            cmm = new CodeMemberMethod ();
            cmm.Name = "SecondMethod";
            cmm.ReturnType = new CodeTypeReference (typeof (int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression (typeof (int), "i");
            cmm.Parameters.Add (param);
            condstmt = new CodeConditionStatement (new CodeBinaryOperatorExpression (
                new CodeArgumentReferenceExpression ("i"), CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression (1)),
                new CodeGotoStatement ("comehere"));
            cmm.Statements.Add (condstmt);
            cmm.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (6)));
            cmm.Statements.Add (new CodeLabeledStatement ("comehere"));
            cmm.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (7)));
            class1.Members.Add (cmm);

            // create third method to test gotos that jump to a previously defined label
            //  GENERATE (C#):   
            //    public static int ThirdMethod(int i) {
            //    label:
            //        i = (i + 5);
            //        if ((i < 1)) {
            //            goto label;
            //        }
            //        return i;
            //    }
            AddScenario ("CheckThirdMethod");
            cmm = new CodeMemberMethod ();
            cmm.Name = "ThirdMethod";
            cmm.ReturnType = new CodeTypeReference (typeof (int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression (typeof (int), "i");
            cmm.Parameters.Add (param);
            CodeAssignStatement assignmt = new CodeAssignStatement (new CodeArgumentReferenceExpression ("i"),
                new CodeBinaryOperatorExpression
                (new CodeArgumentReferenceExpression ("i"), CodeBinaryOperatorType.Add,
                new CodePrimitiveExpression (5)));
            cmm.Statements.Add (new CodeLabeledStatement ("label", assignmt));
            condstmt = new CodeConditionStatement (new CodeBinaryOperatorExpression (
                new CodeArgumentReferenceExpression ("i"), CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression (1)),
                new CodeGotoStatement ("label"));
            cmm.Statements.Add (condstmt);
            cmm.Statements.Add (new CodeMethodReturnStatement (new CodeArgumentReferenceExpression ("i")));
            class1.Members.Add (cmm);
        }
    }
 private void GenerateAssignStatement(CodeAssignStatement e)
 {
     GenerateExpression(e.Left);
     Output.Write(" = ");
     GenerateExpression(e.Right);
     if (!generatingForLoop)
     {
         Output.WriteLine(";");
     }
 }
        public void Params()
        {
            Func<string, int, CodeStatement> createStatement = (objName, iNum) =>
            {
                CodeAssignStatement statement = new CodeAssignStatement(new CodeVariableReferenceExpression("str"),
                                    new CodeMethodInvokeExpression(
                                    new CodeMethodReferenceExpression(
                                    new CodeTypeReferenceExpression(new CodeTypeReference(objName)), "Replace"),
                                    new CodeExpression[]{
                                        new CodePrimitiveExpression("{" + iNum + "}"),
                                        new CodeMethodInvokeExpression(
                                            new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("array"), new CodePrimitiveExpression(iNum)),
                                            "ToString")}));
                return statement;
            };

            CodeNamespace ns = new CodeNamespace("Namespace1");
            ns.Imports.Add(new CodeNamespaceImport("System"));

            CodeTypeDeclaration class1 = new CodeTypeDeclaration();
            class1.Name = "Class1";
            ns.Types.Add(class1);

            CodeMemberMethod fooMethod1 = new CodeMemberMethod();
            fooMethod1.Name = "Foo1";
            fooMethod1.Attributes = MemberAttributes.Public;
            fooMethod1.ReturnType = new CodeTypeReference(typeof(string));

            CodeParameterDeclarationExpression parameter1 = new CodeParameterDeclarationExpression();
            parameter1.Name = "format";
            parameter1.Type = new CodeTypeReference(typeof(string));
            fooMethod1.Parameters.Add(parameter1);

            CodeParameterDeclarationExpression parameter2 = new CodeParameterDeclarationExpression();
            parameter2.Name = "array";
            parameter2.Type = new CodeTypeReference(typeof(object[]));
            parameter2.CustomAttributes.Add(new CodeAttributeDeclaration("System.ParamArrayAttribute"));
            parameter2.CustomAttributes.Add(new CodeAttributeDeclaration("System.Runtime.InteropServices.OptionalAttribute"));
            fooMethod1.Parameters.Add(parameter2);
            class1.Members.Add(fooMethod1);

            fooMethod1.Statements.Add(new CodeVariableDeclarationStatement(typeof(string), "str"));

            fooMethod1.Statements.Add(createStatement("format", 0));
            fooMethod1.Statements.Add(createStatement("str", 1));
            fooMethod1.Statements.Add(createStatement("str", 2));

            fooMethod1.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("str")));

            CodeEntryPointMethod methodMain = new CodeEntryPointMethod();
            methodMain.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("Class1"), "test1", new CodeObjectCreateExpression(new CodeTypeReference("Class1"))));

            methodMain.Statements.Add(new CodeExpressionStatement(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference("test1")), "Foo1"),
                    new CodeExpression[] {
                        new CodePrimitiveExpression("{0} + {1} = {2}"),
                        new CodePrimitiveExpression(1),
                        new CodePrimitiveExpression(2),
                        new CodePrimitiveExpression(3)
                    })));

            class1.Members.Add(methodMain);

            AssertEqual(ns,
                @"namespace Namespace1 {
                      using System;
                      public class Class1 {
                          public virtual string Foo1(string format, [System.Runtime.InteropServices.OptionalAttribute()] params object[] array) {
                              string str;
                              str = format.Replace(""{0}"", array[0].ToString());
                              str = str.Replace(""{1}"", array[1].ToString());
                              str = str.Replace(""{2}"", array[2].ToString());
                              return str;
                          }
                          public static void Main() {
                              Class1 test1 = new Class1();
                              test1.Foo1(""{0} + {1} = {2}"", 1, 2, 3);
                          }
                      }
                  }");
        }
Exemple #15
0
	static void BuildState (State s, CodeMemberMethod method)
	{
		string methodName = String.Format ("State_{0}", stateCounter++);
		CodeMemberMethod stateMethod = new CodeMemberMethod ();
		stateMethod.Name = methodName;
		MainClass.Members.Add (stateMethod);
		
		CodeAssignStatement stateAssign = new CodeAssignStatement ();
		stateAssign.Left = curState;
		stateAssign.Right = new CodeCastExpression (stateType, curCountryLocation);
		stateMethod.Statements.Add (stateAssign);

		CodeMethodInvokeExpression methodCall = new CodeMethodInvokeExpression (
			thisref,
			methodName
		);
		method.Statements.Add (methodCall);
		
		methodCall = new CodeMethodInvokeExpression (
			curState,
			"Add",
			new CodeExpression[] {curStateLocation}
		);
		CodeMethodInvokeExpression locationAddDirect;
		
		CodeObjectCreateExpression expr;
		foreach (Element e in s.Locations) {
			expr = BuildStateLocation (e, stateMethod);
			if (expr == null)
				stateMethod.Statements.Add (methodCall);
			else {
				locationAddDirect = new CodeMethodInvokeExpression (
					curState,
					"Add",
					new CodeExpression[] {expr});
				stateMethod.Statements.Add (locationAddDirect);
			}
		}
	}
        public void Goto()
        {
            CodeNamespace ns = new CodeNamespace("NS");
            ns.Imports.Add(new CodeNamespaceImport("System"));

            CodeTypeDeclaration class1 = new CodeTypeDeclaration();
            class1.Name = "Test";
            class1.IsClass = true;
            ns.Types.Add(class1);

            // create first method to test gotos that jump ahead to a defined label with statement
            var cmm = new CodeMemberMethod();
            cmm.Name = "FirstMethod";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(typeof(int), "i");
            cmm.Parameters.Add(param);
            CodeConditionStatement condstmt = new CodeConditionStatement(new CodeBinaryOperatorExpression(
                                new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(1)),
                                new CodeGotoStatement("comehere"));
            cmm.Statements.Add(condstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(6)));
            cmm.Statements.Add(new CodeLabeledStatement("comehere",
                                new CodeMethodReturnStatement(new CodePrimitiveExpression(7))));
            class1.Members.Add(cmm);

            // create second method to test gotos that jump ahead to a defined label without a statement attached to it
            cmm = new CodeMemberMethod();
            cmm.Name = "SecondMethod";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression(typeof(int), "i");
            cmm.Parameters.Add(param);
            condstmt = new CodeConditionStatement(new CodeBinaryOperatorExpression(
                                new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(1)),
                                new CodeGotoStatement("comehere"));
            cmm.Statements.Add(condstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(6)));
            cmm.Statements.Add(new CodeLabeledStatement("comehere"));
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(7)));
            class1.Members.Add(cmm);

            // create third method to test gotos that jump to a previously defined label
            cmm = new CodeMemberMethod();
            cmm.Name = "ThirdMethod";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression(typeof(int), "i");
            cmm.Parameters.Add(param);
            CodeAssignStatement assignmt = new CodeAssignStatement(new CodeVariableReferenceExpression("i"),
                                new CodeBinaryOperatorExpression
                                (new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add,
                                new CodePrimitiveExpression(5)));
            cmm.Statements.Add(new CodeLabeledStatement("label", assignmt));
            condstmt = new CodeConditionStatement(new CodeBinaryOperatorExpression(
                                new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(1)),
                                new CodeGotoStatement("label"));
            cmm.Statements.Add(condstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("i")));
            class1.Members.Add(cmm);

            AssertEqual(ns,
                @"namespace NS {
                      using System;

                      public class Test {
                          public static int FirstMethod(int i) {
                              if ((i < 1)) {
                                  goto comehere;
                              }
                              return 6;
                          comehere:
                              return 7;
                          }

                          public static int SecondMethod(int i) {
                              if ((i < 1)) {
                                  goto comehere;
                              }
                              return 6;
                          comehere:
                              return 7;
                          }

                          public static int ThirdMethod(int i) {
                          label:
                              i = (i + 5);
                              if ((i < 1)) {
                                  goto label;
                              }
                              return i;
                          }
                      }
                  }");
        }
Exemple #17
0
	static CodeObjectCreateExpression BuildCountryLocation (Element e, CodeMemberMethod method)
	{
		CodeObjectCreateExpression location;

		if (e is Location)
			location = new CodeObjectCreateExpression (locationType,
								   new CodeExpression[] {
									   new CodePrimitiveExpression (e.Name),
									   new CodePrimitiveExpression (((Location)e).Code),
									   new CodePrimitiveExpression (((Location)e).Coordinates)
								   });
		else if (e is City)
			location = new CodeObjectCreateExpression (cityType,
								   new CodeExpression[] {
									   new CodePrimitiveExpression (e.Name),
									   new CodePrimitiveExpression (((City)e).Locations.Count)
								   });
		else if (e is State)
			location = new CodeObjectCreateExpression (stateType,
								   new CodeExpression[] {
									   new CodePrimitiveExpression (e.Name),
									   new CodePrimitiveExpression (((State)e).Locations.Count)
								   });
		else
			throw new ApplicationException (String.Format ("Unexpected <country> child type: {0}", e));

		if (e is Location)
			return location;
		
		CodeAssignStatement locationAssign = new CodeAssignStatement ();
		locationAssign.Left = curCountryLocation;
		locationAssign.Right = location;
		method.Statements.Add (locationAssign);

		if (e is City)
			BuildCity (e as City, method, curCountry, curCountryLocation);
		else if (e is State)
			BuildState (e as State, method);

		return null;
	}
	// Generate various statement categories.
	protected override void GenerateAssignStatement
				(CodeAssignStatement e)
			{
				GenerateExpression(e.Left);
				Output.Write(" = ");
				GenerateExpression(e.Right);
				if(!outputForInit)
				{
					Output.WriteLine(";");
				}
			}
Exemple #19
0
	static void BuildCountry (Country c, CodeMemberMethod method)
	{
		CodeObjectCreateExpression country =
			new CodeObjectCreateExpression (countryType,
							new CodeExpression[] {
								new CodePrimitiveExpression (c.Name),
								new CodePrimitiveExpression (c.Locations.Count)
							});

		string methodName = String.Format ("Country_{0}", countryCounter++);
		CodeMemberMethod countryMethod = new CodeMemberMethod ();
		countryMethod.Name = methodName;
		MainClass.Members.Add (countryMethod);
		
		CodeAssignStatement countryAssign = new CodeAssignStatement ();
		countryAssign.Left = curCountry;
		countryAssign.Right = country;
		countryMethod.Statements.Add (countryAssign);

		CodeMethodInvokeExpression methodCall = new CodeMethodInvokeExpression (
			thisref,
			methodName
		);
		method.Statements.Add (methodCall);

		methodCall = new CodeMethodInvokeExpression (
			curCountry,
			"Add",
			new CodeExpression[] {curCountryLocation}
		);
		CodeMethodInvokeExpression locationAddDirect;
		
		CodeObjectCreateExpression expr;
		
		foreach (Element e in c.Locations) {
			expr = BuildCountryLocation (e, countryMethod);
			if (expr == null)
				countryMethod.Statements.Add (methodCall);
			else {
				locationAddDirect = new CodeMethodInvokeExpression (
					curCountry,
					"Add",
					new CodeExpression[] {expr}
				);
				countryMethod.Statements.Add (locationAddDirect);
			}
		}
	}
 public CodeAssignStatement CreateStatement(CodeExpression objName, int iNum){
     CodeAssignStatement statement =
         new CodeAssignStatement (new CodeVariableReferenceExpression("str"),
             new CodeMethodInvokeExpression(
             new CodeMethodReferenceExpression(
             objName, "Replace"), 
             new CodeExpression[]{
                 new CodePrimitiveExpression("{" + iNum + "}"),
                 new CodeMethodInvokeExpression(
                         new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("array"),
                             new CodePrimitiveExpression(iNum)),
                         "ToString")
             }));
     return statement;
 }
Exemple #21
0
	static void BuildRegion (Region r, CodeMemberMethod method)
	{
		CodeObjectCreateExpression region =
			new CodeObjectCreateExpression (regionType,
							new CodeExpression[] {
								new CodePrimitiveExpression (r.Name),
								new CodePrimitiveExpression (r.Countries.Count)
							});

		string methodName = String.Format ("Region_{0}", regionCounter++);
		CodeMemberMethod regionMethod = new CodeMemberMethod ();
		regionMethod.Name = methodName;
		MainClass.Members.Add (regionMethod);
		
		CodeMethodInvokeExpression methodCall = new CodeMethodInvokeExpression (
			thisref,
			methodName
		);
		method.Statements.Add (methodCall);
		
		CodeAssignStatement regionAssign = new CodeAssignStatement ();
		regionAssign.Left = curRegion;
		regionAssign.Right = region;
		regionMethod.Statements.Add (regionAssign);

		methodCall = new CodeMethodInvokeExpression (
			new CodeFieldReferenceExpression (thisref, "regions"),
			"Add",
			new CodeExpression[] {curRegion}
		);
		regionMethod.Statements.Add (methodCall);
		
		methodCall = new CodeMethodInvokeExpression (
			curRegion,
			"Add",
			new CodeExpression[] {curCountry}
		);
		
		foreach (Country c in r.Countries) {
			BuildCountry (c, regionMethod);
			regionMethod.Statements.Add (methodCall);
		}
	}
        public void TryCatchThrow()
        {
            var cd = new CodeTypeDeclaration();
            cd.Name = "Test";
            cd.IsClass = true;

            // try catch statement with just finally
            CodeMemberMethod cmm = new CodeMemberMethod();
            cmm.Name = "FirstScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);

            CodeTryCatchFinallyStatement tcfstmt = new CodeTryCatchFinallyStatement();
            tcfstmt.FinallyStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"), new
                                                                  CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("a"), CodeBinaryOperatorType.Add,
                                                                                               new CodePrimitiveExpression(5))));
            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
            cd.Members.Add(cmm);

            CodeBinaryOperatorExpression cboExpression = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("a"), CodeBinaryOperatorType.Divide, new CodeVariableReferenceExpression("a"));
            CodeAssignStatement assignStatement = new CodeAssignStatement(new CodeVariableReferenceExpression("a"), cboExpression);

            // try catch statement with just catch
            cmm = new CodeMemberMethod();
            cmm.Name = "SecondScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);
            cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "exceptionMessage"));

            tcfstmt = new CodeTryCatchFinallyStatement();
            CodeCatchClause catchClause = new CodeCatchClause("e");
            tcfstmt.TryStatements.Add(assignStatement);
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
                                                               new CodePrimitiveExpression(3)));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("exceptionMessage"),
                                                               new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("e"), "ToString")));
            tcfstmt.CatchClauses.Add(catchClause);
            tcfstmt.FinallyStatements.Add(CreateVariableIncrementExpression("a", 1));

            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));

            cd.Members.Add(cmm);

            // try catch statement with multiple catches
            cmm = new CodeMemberMethod();
            cmm.Name = "ThirdScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);
            cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "exceptionMessage"));

            tcfstmt = new CodeTryCatchFinallyStatement();
            catchClause = new CodeCatchClause("e", new CodeTypeReference(typeof(ArgumentNullException)));
            tcfstmt.TryStatements.Add(assignStatement);
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
                                                               new CodePrimitiveExpression(9)));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("exceptionMessage"),
                                                               new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("e"), "ToString")));
            tcfstmt.CatchClauses.Add(catchClause);

            // add a second catch clause
            catchClause = new CodeCatchClause("f", new CodeTypeReference(typeof(Exception)));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("exceptionMessage"),
                                                               new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("f"), "ToString")));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
                                                               new CodePrimitiveExpression(9)));
            tcfstmt.CatchClauses.Add(catchClause);

            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
            cd.Members.Add(cmm);

            // catch throws exception
            cmm = new CodeMemberMethod();
            cmm.Name = "FourthScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);

            tcfstmt = new CodeTryCatchFinallyStatement();
            catchClause = new CodeCatchClause("e");
            tcfstmt.TryStatements.Add(assignStatement);
            catchClause.Statements.Add(new CodeCommentStatement("Error handling"));
            catchClause.Statements.Add(new CodeThrowExceptionStatement(new CodeArgumentReferenceExpression("e")));
            tcfstmt.CatchClauses.Add(catchClause);
            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
            cd.Members.Add(cmm);

            AssertEqual(cd,
                @"Public Class Test
                      Public Shared Function FirstScenario(ByVal a As Integer) As Integer
                          Try
                          Finally
                              a = (a + 5)
                          End Try
                          Return a
                      End Function
                      Public Shared Function SecondScenario(ByVal a As Integer, ByVal exceptionMessage As String) As Integer
                          Try
                              a = (a / a)
                          Catch e As System.Exception
                              a = 3
                              exceptionMessage = e.ToString
                          Finally
                              a = (a + 1)
                          End Try
                          Return a
                      End Function
                      Public Shared Function ThirdScenario(ByVal a As Integer, ByVal exceptionMessage As String) As Integer
                          Try
                              a = (a / a)
                          Catch e As System.ArgumentNullException
                              a = 9
                              exceptionMessage = e.ToString
                          Catch f As System.Exception
                              exceptionMessage = f.ToString
                              a = 9
                          End Try
                          Return a
                      End Function
                      Public Shared Function FourthScenario(ByVal a As Integer) As Integer
                          Try
                              a = (a / a)
                          Catch e As System.Exception
                              'Error handling
                              Throw e
                          End Try
                          Return a
                      End Function
                  End Class");
        }
Exemple #23
0
	static void GenerateBuildData (CodeMemberMethod method)
	{
		CodeTypeReference regionListType = new CodeTypeReference ("List",
									  new CodeTypeReference[] {
										  new CodeTypeReference ("Region")
									  });
		CodeObjectCreateExpression newObject =
			new CodeObjectCreateExpression (regionListType,
							new CodeExpression[] {new CodePrimitiveExpression (regions.Count)});
		CodeAssignStatement regionsAssign = new CodeAssignStatement ();
		regionsAssign.Left = new CodeFieldReferenceExpression (thisref, "regions");
		regionsAssign.Right = newObject;
		method.Statements.Add (regionsAssign);

		CodeMemberField field = new CodeMemberField ("Region", "curRegion");
		MainClass.Members.Add (field);
		field = new CodeMemberField ("Country", "curCountry");
		MainClass.Members.Add (field);
		field = new CodeMemberField ("Element", "curCountryLocation");
		MainClass.Members.Add (field);
		field = new CodeMemberField ("City", "curCity");
		MainClass.Members.Add (field);
		field = new CodeMemberField ("State", "curState");
		MainClass.Members.Add (field);
		field = new CodeMemberField ("Element", "curStateLocation");
		MainClass.Members.Add (field);
		
		foreach (Region r in regions)
			BuildRegion (r, method);
	}
        public void Params()
        {
            Func<string, int, CodeStatement> createStatement = (objName, iNum) =>
            {
                CodeAssignStatement statement = new CodeAssignStatement(new CodeVariableReferenceExpression("str"),
                                    new CodeMethodInvokeExpression(
                                    new CodeMethodReferenceExpression(
                                    new CodeTypeReferenceExpression(new CodeTypeReference(objName)), "Replace"),
                                    new CodeExpression[]{
                                        new CodePrimitiveExpression("{" + iNum + "}"),
                                        new CodeMethodInvokeExpression(
                                            new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("array"), new CodePrimitiveExpression(iNum)),
                                            "ToString")}));
                return statement;
            };

            CodeNamespace ns = new CodeNamespace("Namespace1");
            ns.Imports.Add(new CodeNamespaceImport("System"));

            CodeTypeDeclaration class1 = new CodeTypeDeclaration();
            class1.Name = "Class1";
            ns.Types.Add(class1);

            CodeMemberMethod fooMethod1 = new CodeMemberMethod();
            fooMethod1.Name = "Foo1";
            fooMethod1.Attributes = MemberAttributes.Public;
            fooMethod1.ReturnType = new CodeTypeReference(typeof(string));

            CodeParameterDeclarationExpression parameter1 = new CodeParameterDeclarationExpression();
            parameter1.Name = "format";
            parameter1.Type = new CodeTypeReference(typeof(string));
            fooMethod1.Parameters.Add(parameter1);

            CodeParameterDeclarationExpression parameter2 = new CodeParameterDeclarationExpression();
            parameter2.Name = "array";
            parameter2.Type = new CodeTypeReference(typeof(object[]));
            parameter2.CustomAttributes.Add(new CodeAttributeDeclaration("System.ParamArrayAttribute"));
            parameter2.CustomAttributes.Add(new CodeAttributeDeclaration("System.Runtime.InteropServices.OptionalAttribute"));
            fooMethod1.Parameters.Add(parameter2);
            class1.Members.Add(fooMethod1);

            fooMethod1.Statements.Add(new CodeVariableDeclarationStatement(typeof(string), "str"));

            fooMethod1.Statements.Add(createStatement("format", 0));
            fooMethod1.Statements.Add(createStatement("str", 1));
            fooMethod1.Statements.Add(createStatement("str", 2));

            fooMethod1.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("str")));

            CodeEntryPointMethod methodMain = new CodeEntryPointMethod();
            methodMain.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("Class1"), "test1", new CodeObjectCreateExpression(new CodeTypeReference("Class1"))));

            methodMain.Statements.Add(new CodeExpressionStatement(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference("test1")), "Foo1"),
                    new CodeExpression[] {
                        new CodePrimitiveExpression("{0} + {1} = {2}"),
                        new CodePrimitiveExpression(1),
                        new CodePrimitiveExpression(2),
                        new CodePrimitiveExpression(3)
                    })));

            class1.Members.Add(methodMain);

            AssertEqual(ns,
                @"Imports System
                  Namespace Namespace1
                      Public Class Class1
                          Public Overridable Function Foo1(ByVal format As String, <System.ParamArrayAttribute(), System.Runtime.InteropServices.OptionalAttribute()> ByVal array() As Object) As String
                              Dim str As String
                              str = format.Replace(""{0}"", array(0).ToString)
                              str = str.Replace(""{1}"", array(1).ToString)
                              str = str.Replace(""{2}"", array(2).ToString)
                              Return str
                          End Function

                          Public Shared Sub Main()
                              Dim test1 As Class1 = New Class1()
                              test1.Foo1(""{0} + {1} = {2}"", 1, 2, 3)
                          End Sub
                      End Class
                  End Namespace");
        }
        public void TryCatchThrow()
        {
            var cd = new CodeTypeDeclaration();
            cd.Name = "Test";
            cd.IsClass = true;

            // try catch statement with just finally
            var cmm = new CodeMemberMethod();
            cmm.Name = "FirstScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);

            CodeTryCatchFinallyStatement tcfstmt = new CodeTryCatchFinallyStatement();
            tcfstmt.FinallyStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"), new
                                                                  CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("a"), CodeBinaryOperatorType.Add,
                                                                                               new CodePrimitiveExpression(5))));
            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
            cd.Members.Add(cmm);

            CodeBinaryOperatorExpression cboExpression = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("a"), CodeBinaryOperatorType.Divide, new CodeVariableReferenceExpression("a"));
            CodeAssignStatement assignStatement = new CodeAssignStatement(new CodeVariableReferenceExpression("a"), cboExpression);

            // try catch statement with just catch
            cmm = new CodeMemberMethod();
            cmm.Name = "SecondScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);
            cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "exceptionMessage"));

            tcfstmt = new CodeTryCatchFinallyStatement();
            CodeCatchClause catchClause = new CodeCatchClause("e");
            tcfstmt.TryStatements.Add(assignStatement);
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
                                                               new CodePrimitiveExpression(3)));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("exceptionMessage"),
                                                               new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("e"), "ToString")));
            tcfstmt.CatchClauses.Add(catchClause);
            tcfstmt.FinallyStatements.Add(CreateVariableIncrementExpression("a", 1));

            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));

            cd.Members.Add(cmm);

            // try catch statement with multiple catches
            cmm = new CodeMemberMethod();
            cmm.Name = "ThirdScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);
            cmm.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "exceptionMessage"));

            tcfstmt = new CodeTryCatchFinallyStatement();
            catchClause = new CodeCatchClause("e", new CodeTypeReference(typeof(ArgumentNullException)));
            tcfstmt.TryStatements.Add(assignStatement);
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
                                                               new CodePrimitiveExpression(9)));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("exceptionMessage"),
                                                               new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("e"), "ToString")));
            tcfstmt.CatchClauses.Add(catchClause);

            // add a second catch clause
            catchClause = new CodeCatchClause("f", new CodeTypeReference(typeof(Exception)));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("exceptionMessage"),
                                                               new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("f"), "ToString")));
            catchClause.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("a"),
                                                               new CodePrimitiveExpression(9)));
            tcfstmt.CatchClauses.Add(catchClause);

            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
            cd.Members.Add(cmm);

            // catch throws exception
            cmm = new CodeMemberMethod();
            cmm.Name = "FourthScenario";
            cmm.ReturnType = new CodeTypeReference(typeof(int));
            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            param = new CodeParameterDeclarationExpression(typeof(int), "a");
            cmm.Parameters.Add(param);

            tcfstmt = new CodeTryCatchFinallyStatement();
            catchClause = new CodeCatchClause("e");
            tcfstmt.TryStatements.Add(assignStatement);
            catchClause.Statements.Add(new CodeCommentStatement("Error handling"));
            catchClause.Statements.Add(new CodeThrowExceptionStatement(new CodeArgumentReferenceExpression("e")));
            tcfstmt.CatchClauses.Add(catchClause);
            cmm.Statements.Add(tcfstmt);
            cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("a")));
            cd.Members.Add(cmm);

            AssertEqual(cd,
                @"public class Test {
                      public static int FirstScenario(int a) {
                          try {
                          }
                          finally {
                              a = (a + 5);
                          }
                          return a;
                      }
                      public static int SecondScenario(int a, string exceptionMessage) {
                          try {
                              a = (a / a);
                          }
                          catch (System.Exception e) {
                              a = 3;
                              exceptionMessage = e.ToString();
                          }
                          finally {
                              a = (a + 1);
                          }
                          return a;
                      }
                      public static int ThirdScenario(int a, string exceptionMessage) {
                          try {
                              a = (a / a);
                          }
                          catch (System.ArgumentNullException e) {
                              a = 9;
                              exceptionMessage = e.ToString();
                          }
                          catch (System.Exception f) {
                              exceptionMessage = f.ToString();
                              a = 9;
                          }
                          return a;
                      }
                      public static int FourthScenario(int a) {
                          try {
                              a = (a / a);
                          }
                          catch (System.Exception e) {
                              // Error handling
                              throw e;
                          }
                          return a;
                      }
                  }");
        }
    public override void BuildTree (CodeDomProvider provider, CodeCompileUnit cu) {
#if WHIDBEY
        if (!(provider is JScriptCodeProvider)) {
            // GENERATES (C#):
            //
            //  #region Compile Unit Region
            //  
            //  namespace Namespace1 {
            //      
            //      
            //      #region Outer Type Region
            //      // Outer Type Comment
            //      public class Class1 {
            //          
            //          // Field 1 Comment
            //          private string field1;
            //          
            //          public void Method1() {
            //              this.Event1(this, System.EventArgs.Empty);
            //          }
            //          
            //          #region Constructor Region
            //          public Class1() {
            //              #region Statements Region
            //              this.field1 = "value1";
            //              this.field2 = "value2";
            //              #endregion
            //          }
            //          #endregion
            //          
            //          public string Property1 {
            //              get {
            //                  return this.field1;
            //              }
            //          }
            //          
            //          public static void Main() {
            //          }
            //          
            //          public event System.EventHandler Event1;
            //          
            //          public class NestedClass1 {
            //          }
            //          
            //          public delegate void nestedDelegate1(object sender, System.EventArgs e);
            //          
            //  
            //          
            //          #region Field Region
            //          private string field2;
            //          #endregion
            //          
            //          #region Method Region
            //          // Method 2 Comment
            //          
            //          #line 500 "MethodLinePragma.txt"
            //          public void Method2() {
            //              this.Event2(this, System.EventArgs.Empty);
            //          }
            //          
            //          #line default
            //          #line hidden
            //          #endregion
            //          
            //          public Class1(string value1, string value2) {
            //          }
            //          
            //          #region Property Region
            //          public string Property2 {
            //              get {
            //                  return this.field2;
            //              }
            //          }
            //          #endregion
            //          
            //          #region Type Constructor Region
            //          static Class1() {
            //          }
            //          #endregion
            //          
            //          #region Event Region
            //          public event System.EventHandler Event2;
            //          #endregion
            //          
            //          #region Nested Type Region
            //          // Nested Type Comment
            //          
            //          #line 400 "NestedTypeLinePragma.txt"
            //          public class NestedClass2 {
            //          }
            //          
            //          #line default
            //          #line hidden
            //          #endregion
            //          
            //          #region Delegate Region
            //          public delegate void nestedDelegate2(object sender, System.EventArgs e);
            //          #endregion
            //          
            //          #region Snippet Region
            //  
            //          #endregion
            //      }
            //      #endregion
            //  }
            //  #endregion

            CodeNamespace ns = new CodeNamespace ("Namespace1");

            cu.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Compile Unit Region"));
            cu.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));

            cu.Namespaces.Add (ns);

            CodeTypeDeclaration cd = new CodeTypeDeclaration ("Class1");
            ns.Types.Add (cd);

            cd.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Outer Type Region"));
            cd.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));

            cd.Comments.Add (new CodeCommentStatement ("Outer Type Comment"));

            CodeMemberField field1 = new CodeMemberField (typeof (String), "field1");
            CodeMemberField field2 = new CodeMemberField (typeof (String), "field2");
            field1.Comments.Add (new CodeCommentStatement ("Field 1 Comment"));
            field2.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Field Region"));
            field2.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));

            CodeMemberEvent evt1 = new CodeMemberEvent ();
            evt1.Name = "Event1";
            evt1.Type = new CodeTypeReference (typeof (System.EventHandler));
            evt1.Attributes = (evt1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;

            CodeMemberEvent evt2 = new CodeMemberEvent ();
            evt2.Name = "Event2";
            evt2.Type = new CodeTypeReference (typeof (System.EventHandler));
            evt2.Attributes = (evt2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;

            evt2.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Event Region"));
            evt2.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));


            CodeMemberMethod method1 = new CodeMemberMethod ();
            method1.Name = "Method1";
            method1.Attributes = (method1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            if (provider.Supports (GeneratorSupport.DeclareEvents)) {
                method1.Statements.Add (
                    new CodeDelegateInvokeExpression (
                        new CodeEventReferenceExpression (new CodeThisReferenceExpression (), "Event1"),
                        new CodeExpression[] {
                        new CodeThisReferenceExpression(),
                        new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.EventArgs"), "Empty")
                    }));
            }


            CodeMemberMethod method2 = new CodeMemberMethod ();
            method2.Name = "Method2";
            method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            if (provider.Supports (GeneratorSupport.DeclareEvents)) {
                method2.Statements.Add (
                    new CodeDelegateInvokeExpression (
                        new CodeEventReferenceExpression (new CodeThisReferenceExpression (), "Event2"),
                        new CodeExpression[] {
                        new CodeThisReferenceExpression(),
                        new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.EventArgs"), "Empty")
                    }));
            }
            method2.LinePragma = new CodeLinePragma ("MethodLinePragma.txt", 500);
            method2.Comments.Add (new CodeCommentStatement ("Method 2 Comment"));

            method2.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Method Region"));
            method2.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));


            CodeMemberProperty property1 = new CodeMemberProperty ();
            property1.Name = "Property1";
            property1.Type = new CodeTypeReference (typeof (string));
            property1.Attributes = (property1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property1.GetStatements.Add (
                new CodeMethodReturnStatement (
                    new CodeFieldReferenceExpression (
                        new CodeThisReferenceExpression (),
                        "field1")));

            CodeMemberProperty property2 = new CodeMemberProperty ();
            property2.Name = "Property2";
            property2.Type = new CodeTypeReference (typeof (string));
            property2.Attributes = (property2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            property2.GetStatements.Add (
                new CodeMethodReturnStatement (
                    new CodeFieldReferenceExpression (
                        new CodeThisReferenceExpression (),
                        "field2")));

            property2.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Property Region"));
            property2.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));


            CodeConstructor constructor1 = new CodeConstructor ();
            constructor1.Attributes = (constructor1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            CodeStatement conState1 = new CodeAssignStatement (
                                        new CodeFieldReferenceExpression (
                                            new CodeThisReferenceExpression (),
                                            "field1"),
                                        new CodePrimitiveExpression ("value1"));
            conState1.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Statements Region"));
            constructor1.Statements.Add (conState1);
            CodeStatement conState2 = new CodeAssignStatement (
                                        new CodeFieldReferenceExpression (
                                            new CodeThisReferenceExpression (),
                                            "field2"),
                                        new CodePrimitiveExpression ("value2"));
            conState2.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));
            constructor1.Statements.Add (conState2);

            constructor1.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Constructor Region"));
            constructor1.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));

            CodeConstructor constructor2 = new CodeConstructor ();
            constructor2.Attributes = (constructor2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            constructor2.Parameters.Add (new CodeParameterDeclarationExpression (typeof (string), "value1"));
            constructor2.Parameters.Add (new CodeParameterDeclarationExpression (typeof (string), "value2"));

            CodeTypeConstructor typeConstructor2 = new CodeTypeConstructor ();

            typeConstructor2.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Type Constructor Region"));
            typeConstructor2.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));


            CodeEntryPointMethod methodMain = new CodeEntryPointMethod ();

            CodeTypeDeclaration nestedClass1 = new CodeTypeDeclaration ("NestedClass1");
            CodeTypeDeclaration nestedClass2 = new CodeTypeDeclaration ("NestedClass2");
            nestedClass2.LinePragma = new CodeLinePragma ("NestedTypeLinePragma.txt", 400);
            nestedClass2.Comments.Add (new CodeCommentStatement ("Nested Type Comment"));

            nestedClass2.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Nested Type Region"));
            nestedClass2.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));



            CodeTypeDelegate delegate1 = new CodeTypeDelegate ();
            delegate1.Name = "nestedDelegate1";
            delegate1.Parameters.Add (new CodeParameterDeclarationExpression (new CodeTypeReference ("System.Object"), "sender"));
            delegate1.Parameters.Add (new CodeParameterDeclarationExpression (new CodeTypeReference ("System.EventArgs"), "e"));

            CodeTypeDelegate delegate2 = new CodeTypeDelegate ();
            delegate2.Name = "nestedDelegate2";
            delegate2.Parameters.Add (new CodeParameterDeclarationExpression (new CodeTypeReference ("System.Object"), "sender"));
            delegate2.Parameters.Add (new CodeParameterDeclarationExpression (new CodeTypeReference ("System.EventArgs"), "e"));

            delegate2.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Delegate Region"));
            delegate2.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));


            CodeSnippetTypeMember snippet1 = new CodeSnippetTypeMember ();
            CodeSnippetTypeMember snippet2 = new CodeSnippetTypeMember ();

            CodeRegionDirective regionStart = new CodeRegionDirective (CodeRegionMode.End, "");
            regionStart.RegionText = "Snippet Region";
            regionStart.RegionMode = CodeRegionMode.Start;
            snippet2.StartDirectives.Add (regionStart);
            snippet2.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, string.Empty));


            cd.Members.Add (field1);
            cd.Members.Add (method1);
            cd.Members.Add (constructor1);
            cd.Members.Add (property1);
            cd.Members.Add (methodMain);

            if (Supports (provider, GeneratorSupport.DeclareEvents)) {
                cd.Members.Add (evt1);
            }

            if (Supports (provider, GeneratorSupport.NestedTypes)) {
                cd.Members.Add (nestedClass1);
                if (Supports (provider, GeneratorSupport.DeclareDelegates)) {
                    cd.Members.Add (delegate1);
                }
            }

            cd.Members.Add (snippet1);

            cd.Members.Add (field2);
            cd.Members.Add (method2);
            cd.Members.Add (constructor2);
            cd.Members.Add (property2);


            if (Supports (provider, GeneratorSupport.StaticConstructors)) {
                cd.Members.Add (typeConstructor2);
            }

            if (Supports (provider, GeneratorSupport.DeclareEvents)) {
                cd.Members.Add (evt2);
            }
            if (Supports (provider, GeneratorSupport.NestedTypes)) {
                cd.Members.Add (nestedClass2);
                if (Supports (provider, GeneratorSupport.DeclareDelegates)) {
                    cd.Members.Add (delegate2);
                }
            }
            cd.Members.Add (snippet2);
        }
#endif
    }
Exemple #27
0
 private void ValidateAssignStatement(CodeAssignStatement e)
 {
     ValidateExpression(e.Left);
     ValidateExpression(e.Right);
 }