Пример #1
0
        /// <summary>
        /// Returns a type descriptor for a field of the described type.
        /// </summary>
        /// <param name="field">field of the described type</param>
        /// <returns>descriptor for specified field</returns>
        public TypeDescriptor GetFieldType(FieldInfo field)
        {
            TypeDescriptor result;

            if (!_memberTypes.TryGetValue(field, out result))
            {
                result = TypeDescriptor.MakeType(field.FieldType);
            }
            return(result);
        }
Пример #2
0
 /// <summary>
 /// Constructs a <c>FunctionCall</c> representation of a string concatenation operation.
 /// </summary>
 /// <param name="exprs">expressions representing the strings to be concatenated, from left-most to right-most</param>
 public static FunctionCall StringConcat(Expression[] exprs)
 {
     return(new FunctionCall()
     {
         Callee = MakeFun(
             new IntrinsicFunction(IntrinsicFunction.EAction.StringConcat, null),
             typeof(string)),
         Arguments = exprs,
         ResultType = TypeDescriptor.MakeType(typeof(string)),
         SetResultTypeClass = EResultTypeClass.ObjectReference
     });
 }
Пример #3
0
        /// <summary>
        /// Creates an enumeration type for a given set of literals. If an enumeration with the specified literal names
        /// already exists, the existing one is returned.
        /// </summary>
        /// <param name="suggestedTypeName">desired name of the enumeration type</param>
        /// <param name="literals">enumeration literals</param>
        public TypeDescriptor GetOrCreateEnum(string suggestedTypeName, params string[] literals)
        {
            var            fieldSeq = new HashableSequence <string>(literals);
            TypeDescriptor existing;

            if (EnumLookup.TryGetValue(fieldSeq, out existing))
            {
                return(existing);
            }

            EnumBuilder tbe        = null;
            var         modBuilder = this.GetDesign().ModBuilder;

            try
            {
                tbe = modBuilder.DefineEnum(suggestedTypeName, TypeAttributes.Public, typeof(int));
            }
            catch (ArgumentException)
            {
                // assume existing type name
                int count = 0;
                while (true)
                {
                    string mname = suggestedTypeName + "_" + count;
                    try
                    {
                        tbe = modBuilder.DefineEnum(mname, TypeAttributes.Public, typeof(int));
                        break;
                    }
                    catch (ArgumentException)
                    {
                        ++count;
                    }
                }
            }
            int i = 0;

            foreach (string fieldName in literals)
            {
                FieldBuilder fb = tbe.DefineLiteral(fieldName, i);
                fb.SetConstant(i);
                ++i;
            }
            Type           te = tbe.CreateType();
            TypeDescriptor td = TypeDescriptor.MakeType(te);

            EnumLookup.Add(fieldSeq, td);
            td.Nest(this);
            return(td);
        }