Exemple #1
0
        /// <summary>
        /// returns float/double/creal when cfloat/cdouble/creal are given
        /// </summary>
        /// <returns>The reflect non complex type.</returns>
        /// <param name="t">T.</param>
        /// <param name="ctxt">Ctxt.</param>
        static ITypeDeclaration help_ReflectNonComplexType(AbstractType t, ResolutionContext ctxt)
        {
            var pt = t as PrimitiveType;

            if (pt != null)
            {
                switch (pt.TypeToken)
                {
                case DTokens.Cfloat:
                    return(new DTokenDeclaration(DTokens.Float));

                case DTokens.Cdouble:
                    return(new DTokenDeclaration(DTokens.Double));

                case DTokens.Creal:
                    return(new DTokenDeclaration(DTokens.Real));
                }
            }
            return(DTypeToTypeDeclVisitor.GenerateTypeDecl(t));
        }
Exemple #2
0
        static StaticProperties()
        {
            var props = new Dictionary <int, StaticPropertyInfo>();

            Properties[PropOwnerType.Generic] = props;

            props.AddProp(new StaticPropertyInfo("init", "A type's or variable's static initializer expression")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType
            });
            props.AddProp(new StaticPropertyInfo("sizeof", "Size of a type or variable in bytes", DTokens.Uint));             // Do not define it as size_t due to unnecessary recursive definition as typeof(int.sizeof)
            props.AddProp(new StaticPropertyInfo("alignof", "Variable offset", DTokens.Uint)
            {
                RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("mangleof", "String representing the ‘mangled’ representation of the type", "string"));
            props.AddProp(new StaticPropertyInfo("stringof", "String representing the source representation of the type", "string")
            {
                ValueGetter = (vp, v) => {
                    var t = AbstractType.Get(v);
                    if (t == null)
                    {
                        return(new NullValue());
                    }
                    return(new ArrayValue(Evaluation.GetStringType(vp.ResolutionContext), (t is DSymbol) ? DNode.GetNodePath((t as DSymbol).Definition, true) : t.ToCode()));
                }
            });



            props = new Dictionary <int, StaticPropertyInfo>();
            Properties[PropOwnerType.Integral] = props;

            props.AddProp(new StaticPropertyInfo("max", "Maximum value")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType
            });
            props.AddProp(new StaticPropertyInfo("min", "Minimum value")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType
            });



            props = new Dictionary <int, StaticPropertyInfo>();
            Properties[PropOwnerType.FloatingPoint] = props;

            props.AddProp(new StaticPropertyInfo("infinity", "Infinity value")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType
            });
            props.AddProp(new StaticPropertyInfo("nan", "Not-a-Number value")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType
            });
            props.AddProp(new StaticPropertyInfo("dig", "Number of decimal digits of precision", DTokens.Int));
            props.AddProp(new StaticPropertyInfo("epsilon", "Smallest increment to the value 1")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType
            });
            props.AddProp(new StaticPropertyInfo("mant_dig", "Number of bits in mantissa", DTokens.Int));
            props.AddProp(new StaticPropertyInfo("max_10_exp", "Maximum int value such that 10^max_10_exp is representable", DTokens.Int));
            props.AddProp(new StaticPropertyInfo("max_exp", "Maximum int value such that 2^max_exp-1 is representable", DTokens.Int));
            props.AddProp(new StaticPropertyInfo("min_10_exp", "Minimum int value such that 10^max_10_exp is representable", DTokens.Int));
            props.AddProp(new StaticPropertyInfo("min_exp", "Minimum int value such that 2^max_exp-1 is representable", DTokens.Int));
            props.AddProp(new StaticPropertyInfo("min_normal", "Number of decimal digits of precision", DTokens.Int));
            props.AddProp(new StaticPropertyInfo("re", "Real part")
            {
                TypeGetter = help_ReflectNonComplexType, ResolvedBaseTypeGetter = help_ReflectResolvedNonComplexType, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("im", "Imaginary part")
            {
                TypeGetter = help_ReflectNonComplexType, ResolvedBaseTypeGetter = help_ReflectResolvedNonComplexType, RequireThis = true
            });



            props = new Dictionary <int, StaticPropertyInfo>();
            Properties[PropOwnerType.Array] = props;

            props.AddProp(new StaticPropertyInfo("length", "Array length", DTokens.Int)
            {
                RequireThis = true,
                ValueGetter =
                    (vp, v) => {
                    var av = v as ArrayValue;
                    return(new PrimitiveValue(DTokens.Int, av.Elements != null ? av.Elements.Length : 0, null, 0m));
                }
            });

            props.AddProp(new StaticPropertyInfo("dup", "Create a dynamic array of the same size and copy the contents of the array into it.")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("idup", "D2.0 only! Creates immutable copy of the array")
            {
                TypeGetter = (t, ctxt) => new MemberFunctionAttributeDecl(DTokens.Immutable)
                {
                    InnerType = help_ReflectType(t, ctxt)
                }, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("reverse", "Reverses in place the order of the elements in the array. Returns the array.")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("sort", "Sorts in place the order of the elements in the array. Returns the array.")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("ptr", "Returns pointer to the array")
            {
                ResolvedBaseTypeGetter = (t, ctxt) => new PointerType((t as DerivedDataType).Base, t.DeclarationOrExpressionBase),
                TypeGetter             = (t, ctxt) => new PointerDecl(DTypeToTypeDeclVisitor.GenerateTypeDecl((t as DerivedDataType).Base)),
                RequireThis            = true
            });



            props = new Dictionary <int, StaticPropertyInfo>(props);            // Copy from arrays' properties!
            Properties[PropOwnerType.AssocArray] = props;

            props.AddProp(new StaticPropertyInfo("length", "Returns number of values in the associative array. Unlike for dynamic arrays, it is read-only.", "size_t")
            {
                RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("keys", "Returns dynamic array, the elements of which are the keys in the associative array.")
            {
                TypeGetter = (t, ctxt) => new ArrayDecl {
                    ValueType = DTypeToTypeDeclVisitor.GenerateTypeDecl((t as AssocArrayType).KeyType)
                }, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("values", "Returns dynamic array, the elements of which are the values in the associative array.")
            {
                TypeGetter = (t, ctxt) => new ArrayDecl {
                    ValueType = DTypeToTypeDeclVisitor.GenerateTypeDecl((t as AssocArrayType).ValueType)
                }, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("rehash", "Reorganizes the associative array in place so that lookups are more efficient. rehash is effective when, for example, the program is done loading up a symbol table and now needs fast lookups in it. Returns a reference to the reorganized array.")
            {
                TypeGetter = help_ReflectType, ResolvedBaseTypeGetter = help_ReflectResolvedType, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("byKey", "Returns a delegate suitable for use as an Aggregate to a ForeachStatement which will iterate over the keys of the associative array.")
            {
                TypeGetter = (t, ctxt) => new DelegateDeclaration()
                {
                    ReturnType = new ArrayDecl()
                    {
                        ValueType = DTypeToTypeDeclVisitor.GenerateTypeDecl((t as AssocArrayType).KeyType)
                    }
                }, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("byValue", "Returns a delegate suitable for use as an Aggregate to a ForeachStatement which will iterate over the values of the associative array.")
            {
                TypeGetter = (t, ctxt) => new DelegateDeclaration()
                {
                    ReturnType = new ArrayDecl()
                    {
                        ValueType = DTypeToTypeDeclVisitor.GenerateTypeDecl((t as AssocArrayType).ValueType)
                    }
                }, RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("get", null)
            {
                RequireThis = true,
                NodeGetter  = (t, ctxt) =>
                {
                    var ad        = t as AssocArrayType;
                    var valueType = DTypeToTypeDeclVisitor.GenerateTypeDecl(ad.ValueType);
                    return(new DMethod()
                    {
                        Name = "get",
                        Description = "Looks up key; if it exists returns corresponding value else evaluates and returns defaultValue.",
                        Type = valueType,
                        Parameters = new List <INode> {
                            new DVariable()
                            {
                                Name = "key",
                                Type = DTypeToTypeDeclVisitor.GenerateTypeDecl(ad.KeyType)
                            },
                            new DVariable()
                            {
                                Name = "defaultValue",
                                Type = valueType,
                                Attributes = new List <DAttribute> {
                                    new Modifier(DTokens.Lazy)
                                }
                            }
                        }
                    });
                }
            });
            props.AddProp(new StaticPropertyInfo("remove", null)
            {
                RequireThis = true,
                NodeGetter  = (t, ctxt) => new DMethod
                {
                    Name        = "remove",
                    Description = "remove(key) does nothing if the given key does not exist and returns false. If the given key does exist, it removes it from the AA and returns true.",
                    Type        = new DTokenDeclaration(DTokens.Bool),
                    Parameters  = new List <INode> {
                        new DVariable {
                            Name = "key",
                            Type = DTypeToTypeDeclVisitor.GenerateTypeDecl((t as AssocArrayType).KeyType)
                        }
                    }
                }
            });


            props = new Dictionary <int, StaticPropertyInfo>();
            Properties[PropOwnerType.TypeTuple] = props;

            props.AddProp(new StaticPropertyInfo("length", "Returns number of values in the type tuple.", "size_t")
            {
                RequireThis = true,
                ValueGetter =
                    (vp, v) => {
                    var tt = v as DTuple;
                    if (tt == null && v is TypeValue)
                    {
                        tt = (v as TypeValue).RepresentedType as DTuple;
                    }
                    return(tt != null ? new PrimitiveValue(DTokens.Int, tt.Items == null ? 0m : (decimal)tt.Items.Length, null, 0m) : null);
                }
            });



            props = new Dictionary <int, StaticPropertyInfo>();
            Properties[PropOwnerType.Delegate] = props;


            props.AddProp(new StaticPropertyInfo("ptr", "The .ptr property of a delegate will return the frame pointer value as a void*.",
                                                 (ITypeDeclaration) new PointerDecl(new DTokenDeclaration(DTokens.Void)))
            {
                RequireThis = true
            });
            props.AddProp(new StaticPropertyInfo("funcptr", "The .funcptr property of a delegate will return the function pointer value as a function type.")
            {
                RequireThis = true
            });



            props = new Dictionary <int, StaticPropertyInfo>();
            Properties[PropOwnerType.ClassLike] = props;

            props.AddProp(new StaticPropertyInfo("classinfo", "Information about the dynamic type of the class", (ITypeDeclaration) new IdentifierDeclaration("TypeInfo_Class")
            {
                ExpressesVariableAccess = true, InnerDeclaration = new IdentifierDeclaration("object")
            })
            {
                RequireThis = true
            });

            props = new Dictionary <int, StaticPropertyInfo>();
            Properties[PropOwnerType.Struct] = props;

            props.AddProp(new StaticPropertyInfo("sizeof", "Size in bytes of struct", DTokens.Uint));
            props.AddProp(new StaticPropertyInfo("alignof", "Size boundary struct needs to be aligned on", DTokens.Uint));
            props.AddProp(new StaticPropertyInfo("tupleof", "Gets type tuple of fields")
            {
                TypeGetter = (t, ctxt) =>
                {
                    var members = GetStructMembers(t as StructType, ctxt);
                    var l       = new List <IExpression>();

                    var vis = new DTypeToTypeDeclVisitor();
                    foreach (var member in members)
                    {
                        var mt = DResolver.StripMemberSymbols(member);
                        if (mt == null)
                        {
                            l.Add(null);
                            continue;
                        }
                        var td = mt.Accept(vis);
                        if (td == null)
                        {
                            l.Add(null);
                            continue;
                        }
                        l.Add(td as IExpression ?? new TypeDeclarationExpression(td));
                    }

                    return(new TemplateInstanceExpression(new IdentifierDeclaration("Tuple"))
                    {
                        Arguments = l.ToArray()
                    });
                },

                ResolvedBaseTypeGetter = (t, ctxt) =>
                {
                    var members    = GetStructMembers(t as StructType, ctxt);
                    var tupleItems = new List <ISemantic>();

                    foreach (var member in members)
                    {
                        var mt = DResolver.StripMemberSymbols(member);
                        if (mt != null)
                        {
                            tupleItems.Add(mt);
                        }
                    }

                    return(new DTuple(t.DeclarationOrExpressionBase, tupleItems));
                }
            });
        }
Exemple #3
0
 static ITypeDeclaration help_ReflectType(AbstractType t, ResolutionContext ctxt)
 {
     return(DTypeToTypeDeclVisitor.GenerateTypeDecl(t));
 }