/// <summary>
        /// Adds a method with return type <paramref name="type"/> and attributes=Public|Final to this type.
        /// </summary>
        public static EasyMethod AddMethod(this CodeTypeDeclaration typeDecl, CodeTypeReference type, string name)
        {
            EasyMethod p = new EasyMethod(type, name);

            typeDecl.Members.Add(p);
            if (typeDecl.IsInterface == false)
            {
                p.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            }
            return(p);
        }
Example #2
0
 /// <summary>
 /// Adds a method with return type <paramref name="type"/> and attributes=Public|Final to this type.
 /// </summary>
 public EasyMethod AddMethod(CodeTypeReference type, string name)
 {
     EasyMethod p = new EasyMethod(type, name);
     this.Members.Add(p);
     if (this.IsInterface == false) {
         p.Attributes = MemberAttributes.Public | MemberAttributes.Final;
     }
     return p;
 }
Example #3
0
		static bool AddVisitCode(EasyMethod m, FieldInfo field, CodeExpression var, List<CodeStatement> assertions, bool transformer)
		{
			CodeExpression prop = var.Property(GetPropertyName(field.Name));
			CodeExpression nodeStack = Easy.Var("nodeStack");
			if (field.FieldType.FullName.StartsWith("System.Collections.Generic.List")) {
				Type elType = field.FieldType.GetGenericArguments()[0];
				if (!typeof(INode).IsAssignableFrom(elType))
					return false;
				assertions.Add(AssertIsNotNull(prop));
				string code;
				if (transformer) {
					code = CreateTransformerLoop(GetCode(prop), ConvertType(elType).BaseType);
				} else {
					code =
						"\t\t\tforeach (" + ConvertType(elType).BaseType + " o in " + GetCode(prop) + ") {\n" +
						"\t\t\t\tDebug.Assert(o != null);\n" +
						"\t\t\t\to.AcceptVisitor(this, data);\n" +
						"\t\t\t}";
				}
				m.Statements.Add(new CodeSnippetStatement(code));
				return true;
			}
			if (!typeof(INode).IsAssignableFrom(field.FieldType))
				return false;
			assertions.Add(AssertIsNotNull(prop));
			if (transformer) {
				m.Statements.Add(nodeStack.InvokeMethod("Push", prop));
			}
			m.Statements.Add(prop.InvokeMethod("AcceptVisitor",
			                                   Easy.This,
			                                   Easy.Var("data")));
			if (transformer) {
				m.Body.Assign(prop, nodeStack.InvokeMethod("Pop").CastTo(ConvertType(field.FieldType)));
			}
			return true;
		}
Example #4
0
		static void AddFieldVisitCode(EasyMethod m, Type type, CodeExpression var, List<CodeStatement> assertions, bool transformer)
		{
			if (type != null) {
				if (type.BaseType != typeof(StatementWithEmbeddedStatement)) {
					AddFieldVisitCode(m, type.BaseType, var, assertions, transformer);
				}
				foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) {
					AddVisitCode(m, field, var, assertions, transformer);
				}
				if (type.BaseType == typeof(StatementWithEmbeddedStatement)) {
					AddFieldVisitCode(m, type.BaseType, var, assertions, transformer);
				}
			}
		}
Example #5
0
 static bool AddVisitCode(EasyMethod m, FieldInfo field, CodeExpression var, List<CodeStatement> assertions, bool transformer)
 {
     CodeExpression prop = var.Property(GetPropertyName(field.Name));
     CodeExpression nodeStack = Easy.Var("nodeStack");
     if (field.FieldType.FullName.StartsWith("System.Collections.Generic.List")) {
         Type elType = field.FieldType.GetGenericArguments()[0];
         if (!typeof(INode).IsAssignableFrom(elType))
             return false;
         assertions.Add(IfNullSetFailure(prop));
         string code;
         string propertyName = GetCode(prop);
         if (transformer) {
             code = CreateTransformerLoop(propertyName, ConvertType(elType).BaseType);
         } else {
             code =
                 "\t\t\tif (" + propertyName + ".Count == data." + GetPropertyName(field.Name) + ".Count) {\n" +
                 "\t\t\tfor (int i=0; i<" + propertyName + ".Count;i++) {\n" +
                 "\t\t\t\t" + ConvertType(elType).BaseType + " o = " + propertyName + "[i];\n" +
                 "\t\t\t\tif(o == null){return SetFailure();}\n" + "\t\t\t\tif((bool)o.AcceptVisitor(this, data." + GetPropertyName(field.Name) + "[i]) == false) return SetFailure();\n" +
                 "\t\t\t}" +
                 "\t\t\t}" +
                 "\t\t\telse { return SetFailure(); }";
         }
         m.Statements.Add(new CodeSnippetStatement(code));
         return true;
     }
     if (!typeof(INode).IsAssignableFrom(field.FieldType))
         return false;
     assertions.Add(IfNullSetFailure(prop));
     if (transformer) {
         m.Statements.Add(nodeStack.InvokeMethod("Push", prop));
     }
     m.Statements.Add(prop.InvokeMethod("AcceptVisitor",
                                        Easy.This,
                                        Easy.Var("data." + var.Property(GetPropertyName(field.Name)).PropertyName)));
     if (transformer) {
         m.Body.Assign(prop, nodeStack.InvokeMethod("Pop").CastTo(ConvertType(field.FieldType)));
     }
     return true;
 }