Beispiel #1
0
 private void AddInitCall(ILGenerator il, FieldBuilder fb, MethodInfo init_meth, IConstantNode init_value)
 {
     if (init_meth != null)
     {
         if (init_value == null || init_value != null && init_value.type.type_special_kind != type_special_kind.set_type && init_value.type.type_special_kind != type_special_kind.base_set_type)
         {
             PushLdfld(fb);
             il.Emit(OpCodes.Call, init_meth);
         }
     }
     if (init_value != null)
     {
         if (init_value is IRecordConstantNode)
         {
             LocalBuilder lb = il.DeclareLocal(fb.FieldType.MakePointerType());
             PushLdfld(fb);
             il.Emit(OpCodes.Stloc, lb);
             GenerateRecordInitCode(il, lb, init_value as IRecordConstantNode);
         }
         else
         {
             if (!fb.IsStatic)
             {
                 //PushLdfld(fb);
                 il.Emit(OpCodes.Ldarg_0);
                 init_value.visit(this);
                 EmitBox(init_value, fb.FieldType);
                 il.Emit(OpCodes.Stfld, fb);
             }
             else
             {
                 init_value.visit(this);
                 EmitBox(init_value, fb.FieldType);
                 il.Emit(OpCodes.Stsfld, fb);
             }
         }
     }
 }
Beispiel #2
0
 private void AddInitCall(LocalBuilder lb, MethodInfo init_meth, IConstantNode init_value, ITypeNode type)
 {
     if (init_meth != null)
     {
         if (init_value == null || init_value != null && init_value.type.type_special_kind != type_special_kind.set_type && init_value.type.type_special_kind != type_special_kind.base_set_type)
         {
             if (lb.LocalType.IsValueType)
                 il.Emit(OpCodes.Ldloca, lb);
             else
                 il.Emit(OpCodes.Ldloc, lb);
             il.Emit(OpCodes.Call, init_meth);
         }
     }
     if (init_value != null)
     {
         if (init_value is IRecordConstantNode)
         {
             LocalBuilder llb = il.DeclareLocal(lb.LocalType.MakePointerType());
             il.Emit(OpCodes.Ldloca, lb);
             il.Emit(OpCodes.Stloc, llb);
             GenerateRecordInitCode(il, llb, init_value as IRecordConstantNode);
         }
         else
         {
             init_value.visit(this);
             EmitBox(init_value, lb.LocalType);
             il.Emit(OpCodes.Stloc, lb);
         }
     }
 }
Beispiel #3
0
        //private void ConvertCompiledGenericInstanceTypeMembers(ICompiledGenericTypeInstance value)
        //{
        //    Type t = helper.GetTypeReference(value).tp;
        //    bool is_delegated_type = t.BaseType == typeof(MulticastDelegate);
        //    foreach (IDefinitionNode dn in value.used_members.Keys)
        //    {
        //        ICompiledConstructorNode iccn = dn as ICompiledConstructorNode;
        //        if (iccn != null)
        //        {
        //            ConstructorInfo ci = TypeBuilder.GetConstructor(t, iccn.constructor_info);
        //            helper.AddConstructor(value.used_members[dn] as IFunctionNode, ci);
        //            continue;
        //        }
        //        ICompiledMethodNode icmn = dn as ICompiledMethodNode;
        //        if (icmn != null)
        //        {
        //            if (is_delegated_type && icmn.method_info.IsSpecialName) continue;
        //            Type[] tt = t.GetGenericArguments();
        //            bool is_tb = false;
        //            for (int i = 0; i < tt.Length; i++)
        //            {
        //                if (tt[i] is TypeBuilder)
        //                {
        //                    is_tb = true;
        //                    break;
        //                }
        //            }
        //            if (!is_tb)
        //            {
        //                continue;
        //            }
        //            MethodInfo mi = TypeBuilder.GetMethod(t, icmn.method_info);
        //            helper.AddMethod(value.used_members[dn] as IFunctionNode, mi);
        //            continue;
        //        }
        //        ICompiledClassFieldNode icfn = dn as ICompiledClassFieldNode;
        //        if (icfn != null)
        //        {
        //            Type ftype = GetTypeOfGenericInstanceField(t, icfn.compiled_field);
        //            FieldInfo fi = TypeBuilder.GetField(t, icfn.compiled_field);
        //            helper.AddGenericField(value.used_members[dn] as ICommonClassFieldNode, fi, ftype);
        //            continue;
        //        }
        //        /*ICompiledPropertyNode icpn = dn as ICompiledPropertyNode;
        //        if (icpn != null)
        //        {
        //            ConvertPropertyAccesor(icpn.compiled_get_method, value, t);
        //            ConvertPropertyAccesor(icpn.compiled_set_method, value, t);
        //        }*/
        //    }
        //}

        //private void ConvertCommonGenericInstanceTypeMembers(ICommonGenericTypeInstance value)
        //{
        //    Type t = helper.GetTypeReference(value).tp;
        //    foreach (IDefinitionNode dn in value.used_members.Keys)
        //    {
        //        ICommonMethodNode icmn = dn as ICommonMethodNode;
        //        if (icmn != null)
        //        {
        //            if (icmn.is_constructor)
        //            {
        //                ConstructorInfo cnstr = helper.GetConstructor(icmn).cnstr;
        //                ConstructorInfo ci = TypeBuilder.GetConstructor(t, cnstr);
        //                helper.AddConstructor(value.used_members[dn] as IFunctionNode, ci);
        //                continue;
        //            }
        //            else
        //            {
        //                MethodInfo meth = helper.GetMethod(icmn).mi;
        //                MethodInfo mi = TypeBuilder.GetMethod(t, meth);
        //                helper.AddMethod(value.used_members[dn] as IFunctionNode, mi);
        //                continue;
        //            }
        //        }
        //        ICommonClassFieldNode icfn = dn as ICommonClassFieldNode;
        //        if (icfn != null)
        //        {
        //            FieldInfo finfo = helper.GetField(icfn).fi;
        //            Type ftype = GetTypeOfGenericInstanceField(t, finfo);
        //            FieldInfo fi = TypeBuilder.GetField(t, finfo);
        //            helper.AddGenericField(value.used_members[dn] as ICommonClassFieldNode, fi, ftype);
        //            continue;
        //        }
        //    }
        //}

        private object[] get_constants(IConstantNode[] cnsts)
        {
            object[] objs = new object[cnsts.Length];
            for (int i = 0; i < objs.Length; i++)
            {
                objs[i] = cnsts[i].value;
            }
            return objs;
        }
Beispiel #4
0
 protected FieldBuilder GetConvertedConstants(IConstantNode c)
 {
     if (ConvertedConstants.ContainsKey(c))
         return ConvertedConstants[c];
     ILGenerator ilb = il;
     il = ModulesInitILGenerators[cur_unit_type];
     ConvertConstantDefinitionNode(null, GetTempName(), c.type, c);
     il = ilb;
     return ConvertedConstants[c];
 }
Beispiel #5
0
		public static string get_const_value(IConstantNode c)
		{
			if (c is IIntConstantNode)
				return (c as IIntConstantNode).constant_value.ToString();
			else
				if (c is IDoubleConstantNode)
				return (c as IDoubleConstantNode).constant_value.ToString();
			else if (c is IFloatConstantNode)
				return (c as IFloatConstantNode).constant_value.ToString();
			else
				if (c is IStringConstantNode)
				return "'"+(c as IStringConstantNode).constant_value.ToString()+"'";
			else
				if (c is ICharConstantNode)
				return "'"+(c as ICharConstantNode).constant_value.ToString()+"'";
			else
				if (c is IByteConstantNode)
				return (c as IByteConstantNode).constant_value.ToString();
			else
				if (c is ISByteConstantNode)
				return (c as ISByteConstantNode).constant_value.ToString();
			else
				if (c is IShortConstantNode)
				return (c as IShortConstantNode).constant_value.ToString();
			else
				if (c is IUShortConstantNode)
				return (c as IUShortConstantNode).constant_value.ToString();
			else
				if (c is IUIntConstantNode)
				return (c as IUIntConstantNode).constant_value.ToString();
			else
				if (c is ILongConstantNode)
				return (c as ILongConstantNode).constant_value.ToString();
			else
				if (c is IULongConstantNode)
				return (c as IULongConstantNode).constant_value.ToString();
			else
				if (c is IEnumConstNode)
				return (c as IEnumConstNode).constant_value.ToString();
			else
			if (c is IArrayConstantNode)
			{
				StringBuilder sb = new StringBuilder();
				IArrayConstantNode arr = c as IArrayConstantNode;
				sb.Append('(');
				for (int i=0; i<arr.ElementValues.Length; i++)
				{
					sb.Append(get_const_value(arr.ElementValues[i]));
					if (i<arr.ElementValues.Length-1)
						sb.Append(", ");
				}
				sb.Append(')');
				return sb.ToString();
			}
			else
			if (c is IRecordConstantNode && c.type is ICommonTypeNode)
			{
				StringBuilder sb = new StringBuilder();
				IRecordConstantNode rec = c as IRecordConstantNode;
				sb.Append('(');
				ICommonTypeNode ctn = c.type as ICommonTypeNode;
				ICommonClassFieldNode[] fields = builder.parser.GetDefinedFields(ctn);
				for (int i=0; i<rec.FieldValues.Length; i++)
				{
					if (i<fields.Length)
						sb.Append(fields[i].name+": ");
					sb.Append(get_const_value(rec.FieldValues[i]));
					if (i<rec.FieldValues.Length-1)
						sb.Append(", ");
				}
				sb.Append(')');
				return sb.ToString();
			}
			return "value";
		}
Beispiel #6
0
        private void ConvertArrayConstantDef(IConstantDefinitionNode cnst, string name, ITypeNode type, IConstantNode constant_value)
        {
            //ConvertedConstants.ContainsKey(ArrayConstant)
            TypeInfo ti = helper.GetTypeReference(type);
            FieldAttributes attrs = FieldAttributes.Public | FieldAttributes.Static;
            if (comp_opt.target == TargetType.Dll)
                attrs |= FieldAttributes.InitOnly;
            FieldBuilder fb = cur_type.DefineField(name, ti.tp, attrs);
            if (cnst != null)
                helper.AddConstant(cnst, fb);
            CreateArrayGlobalVariable(il, fb, ti, constant_value as IArrayConstantNode, type);

            if (!ConvertedConstants.ContainsKey(constant_value))
                ConvertedConstants.Add(constant_value, fb);
        }
Beispiel #7
0
        private void ConvertSimpleConstant(IConstantDefinitionNode cnst, string name, ITypeNode type, IConstantNode constant_value)
        {
            FieldBuilder fb = cur_type.DefineField(name, helper.GetTypeReference(type).tp, FieldAttributes.Static | FieldAttributes.Public | FieldAttributes.Literal);
            Type t = helper.GetTypeReference(type).tp;
            if (t.IsEnum)
            {
                if (!(t is EnumBuilder))
                    fb.SetConstant(Enum.ToObject(t, (constant_value as IEnumConstNode).constant_value));
                else
                    fb.SetConstant(constant_value.value);
            }
            else if (!(constant_value is INullConstantNode))
            {
                if (constant_value.value.GetType() != t)
                {

                }
                else
                fb.SetConstant(constant_value.value);
            }
        }
Beispiel #8
0
        private void ConvertSetConstantDef(IConstantDefinitionNode cnst, string name, ITypeNode type, IConstantNode constant_value)
        {
            TypeInfo ti = helper.GetTypeReference(type);
            FieldAttributes attrs = FieldAttributes.Public | FieldAttributes.Static;
            if (comp_opt.target == TargetType.Dll)
                attrs |= FieldAttributes.InitOnly;
            FieldBuilder fb = cur_type.DefineField(name, ti.tp, attrs);
            //il.Emit(OpCodes.Newobj, ti.tp.GetConstructor(Type.EmptyTypes));
            //il.Emit(OpCodes.Stsfld, fb);
            if (cnst != null)
                helper.AddConstant(cnst, fb);
            bool tmp = save_debug_info;
            save_debug_info = false;
            constant_value.visit(this);
            save_debug_info = tmp;
            il.Emit(OpCodes.Stsfld, fb);

            if (!ConvertedConstants.ContainsKey(constant_value))
                ConvertedConstants.Add(constant_value, fb);
        }
Beispiel #9
0
 //protected internal Formula(IConstantNode<NodeType> nodeType, string name, IConstantNode<ITypeDeclaration> type, IFormula parent)
 //    : this(nodeType, name, type, parent, new INode[] { })
 //{
 //}
 protected internal Formula(IConstantNode<NodeType> nodeType, string name, IConstantNode<ITypeDeclaration> type, IFormula parent, params INode[] properties)
     : this(parent, name, new INode[] { type, parent }.Concat(properties).ToArray())
 {
 }
Beispiel #10
0
 /// <summary>
 /// Visits the constant node.
 /// </summary>
 /// <param name="node">The node.</param>
 private void VisitConstantNode(IConstantNode node)
 {
     var value = node.Value;
     if (_isInPrepareMode && node is SourceFieldNode)
     {
         AppendToResult(String.Format("{0}", GetFieldName((SourceFieldNode) node)));
     }
     else
     {
         AppendToResult(GetConstantValue(Convert.ToString(value, CultureInfo.InvariantCulture), node.DataType));
     }
 }
Beispiel #11
0
 public void visit(IConstantNode value)
 {
     string s = value.GetType().Name + ".";
     prepare_node(value.type, s + "type");
     prepare_string_node(value.value.ToString(), s + "value");
 }
		public virtual void visit(IConstantNode value)
		{
		}
Beispiel #13
0
 public void visit(IConstantNode value)
 {
     // abstract
 }
Beispiel #14
0
 private void AddInitCall(FieldBuilder fb, ILGenerator il, MethodInfo called_mb, ConstructorInfo cnstr, IConstantNode init_value)
 {
     ILGenerator ilc = this.il;
     this.il = il;
     //il = mb.GetILGenerator();
     if (called_mb != null && (init_value == null || init_value.type.type_special_kind != type_special_kind.set_type && init_value.type.type_special_kind != type_special_kind.base_set_type))
     {
         if (fb.IsStatic == false)
         {
             il.Emit(OpCodes.Ldarg_0);
             if (fb.FieldType.IsValueType)
                 il.Emit(OpCodes.Ldflda, fb);
             else
                 il.Emit(OpCodes.Ldfld, fb);
         }
         else
         {
             if (fb.FieldType.IsValueType)
                 il.Emit(OpCodes.Ldsflda, fb);
             else
                 il.Emit(OpCodes.Ldsfld, fb);
         }
         il.Emit(OpCodes.Call, called_mb);
     }
     if (init_value != null)
     {
         if (init_value is IRecordConstantNode)
         {
             LocalBuilder lb = il.DeclareLocal(fb.FieldType.MakePointerType());
             PushLdfld(fb);
             il.Emit(OpCodes.Stloc, lb);
             GenerateRecordInitCode(il, lb, init_value as IRecordConstantNode);
         }
         else
         {
             if (!(init_value is IStringConstantNode))
             {
                 if (!fb.IsStatic)
                 {
                     il.Emit(OpCodes.Ldarg_0);
                     //PushLdfld(fb);
                     init_value.visit(this);
                     EmitBox(init_value, fb.FieldType);
                     il.Emit(OpCodes.Stfld, fb);
                 }
                 else
                 {
                     init_value.visit(this);
                     EmitBox(init_value, fb.FieldType);
                     il.Emit(OpCodes.Stsfld, fb);
                 }
             }
             else
             {
                 if (!fb.IsStatic)
                 {
                     Label lbl = il.DefineLabel();
                     il.Emit(OpCodes.Ldarg_0);
                     il.Emit(OpCodes.Ldfld, fb);
                     il.Emit(OpCodes.Call, TypeFactory.StringNullOrEmptyMethod);
                     il.Emit(OpCodes.Brfalse, lbl);
                     //PushLdfld(fb);
                     il.Emit(OpCodes.Ldarg_0);
                     init_value.visit(this);
                     il.Emit(OpCodes.Stfld, fb);
                     il.MarkLabel(lbl);
                 }
                 else
                 {
                     Label lbl = il.DefineLabel();
                     il.Emit(OpCodes.Ldsfld, fb);
                     il.Emit(OpCodes.Call, TypeFactory.StringNullOrEmptyMethod);
                     il.Emit(OpCodes.Brfalse, lbl);
                     //PushLdfld(fb);
                     init_value.visit(this);
                     il.Emit(OpCodes.Stsfld, fb);
                     il.MarkLabel(lbl);
                 }
             }
         }
     }
     this.il = ilc;
 }
Beispiel #15
0
 private void PushConstantValue(IConstantNode cnst)
 {
     if (cnst is IIntConstantNode)
         PushIntConst((cnst as IIntConstantNode).constant_value);
     else if (cnst is IDoubleConstantNode)
         PushDoubleConst((cnst as IDoubleConstantNode).constant_value);
     else if (cnst is IFloatConstantNode)
         PushFloatConst((cnst as IFloatConstantNode).constant_value);
     else if (cnst is ICharConstantNode)
         PushCharConst((cnst as ICharConstantNode).constant_value);
     else if (cnst is IStringConstantNode)
         PushStringConst((cnst as IStringConstantNode).constant_value);
     else if (cnst is IByteConstantNode)
         PushByteConst((cnst as IByteConstantNode).constant_value);
     else if (cnst is ILongConstantNode)
         PushLongConst((cnst as ILongConstantNode).constant_value);
     else if (cnst is IBoolConstantNode)
         PushBoolConst((cnst as IBoolConstantNode).constant_value);
     else if (cnst is ISByteConstantNode)
         PushSByteConst((cnst as ISByteConstantNode).constant_value);
     else if (cnst is IUShortConstantNode)
         PushUShortConst((cnst as IUShortConstantNode).constant_value);
     else if (cnst is IUIntConstantNode)
         PushUIntConst((cnst as IUIntConstantNode).constant_value);
     else if (cnst is IULongConstantNode)
         PushULongConst((cnst as IULongConstantNode).constant_value);
     else if (cnst is IShortConstantNode)
         PushShortConst((cnst as IShortConstantNode).constant_value);
     else if (cnst is IEnumConstNode)
         PushIntConst((cnst as IEnumConstNode).constant_value);
     else if (cnst is INullConstantNode)
         il.Emit(OpCodes.Ldnull);
 }
Beispiel #16
0
 private void ConvertConstantDefinitionNode(IConstantDefinitionNode cnst, string name, ITypeNode type, IConstantNode constant_value)
 {
     if (constant_value is IArrayConstantNode)
         ConvertArrayConstantDef(cnst, name, type, constant_value);
     else
         if (constant_value is IRecordConstantNode || constant_value is ICompiledStaticMethodCallNodeAsConstant)
             ConvertConstantDefWithInitCall(cnst, name, type, constant_value);
         else if (constant_value is ICommonNamespaceFunctionCallNodeAsConstant || constant_value is IBasicFunctionCallNodeAsConstant || constant_value is ICommonConstructorCallAsConstant || constant_value is ICompiledStaticFieldReferenceNodeAsConstant)
             ConvertSetConstantDef(cnst, name, type, constant_value);
         else ConvertSimpleConstant(cnst, name, type, constant_value);
 }
Beispiel #17
0
 private void ConvertConstantDefWithInitCall(IConstantDefinitionNode cnst, string name, ITypeNode type, IConstantNode constant_value)
 {
     TypeInfo ti = helper.GetTypeReference(type);
     FieldAttributes attrs = FieldAttributes.Public | FieldAttributes.Static;
     if (comp_opt.target == TargetType.Dll)
         attrs |= FieldAttributes.InitOnly;
     FieldBuilder fb = cur_type.DefineField(name, ti.tp, attrs);
     if (cnst != null)
         helper.AddConstant(cnst, fb);
     bool tmp = save_debug_info;
     save_debug_info = false;
     AddInitCall(il, fb, ti.init_meth, constant_value);
     save_debug_info = tmp;
     if (!ConvertedConstants.ContainsKey(constant_value))
         ConvertedConstants.Add(constant_value, fb);
 }
 public virtual void visit(IConstantNode value)
 {
 }
Beispiel #19
0
        private void VisitConstantNode(IConstantNode node)
        {
            object value = node.Value;

            if (_isInPrepareMode && node is SourceFieldNode)
                value = String.Format("@{0}", (node as SourceFieldNode).FieldName.Replace(" ", ""));
                //value = (node as SourceFieldNode).FieldName.Replace(" ", "");

            AppendToResult(GetConstantValue(Convert.ToString(value, CultureInfo.InvariantCulture), node.DataType));
        }