Exemplo n.º 1
0
        DVariable Argument(ref char c, out AbstractType parType)
        {
            bool scoped;

            if (scoped = (c == 'M'))
            {
                c = (char)r.Read();                 //TODO: Handle scoped
            }
            var par = new DVariable {
                Attributes = new List <DAttribute>()
            };

            if (c == 'J' || c == 'K' || c == 'L')
            {
                switch (c)
                {
                case 'J':
                    par.Attributes.Add(new Modifier(DTokens.Out));
                    break;

                case 'K':
                    par.Attributes.Add(new Modifier(DTokens.Ref));
                    break;

                case 'L':
                    par.Attributes.Add(new Modifier(DTokens.Lazy));
                    break;
                }
                c = (char)r.Read();
            }

            parType  = Type(c);
            par.Type = DTypeToTypeDeclVisitor.GenerateTypeDecl(parType);
            return(par);
        }
Exemplo n.º 2
0
        IExpression TemplateArg()
        {
            switch ((char)r.Read())
            {
            case 'T':
                return(new TypeDeclarationExpression(DTypeToTypeDeclVisitor.GenerateTypeDecl(Type())));

            case 'V':
                var t = Type();                         // Where should the explicit type be used when there's already a value?
                return(Value());

            case 'S':
                return(new IdentifierExpression(LName(), LiteralFormat.StringLiteral));
            }
            return(null);
        }
Exemplo n.º 3
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(av.Elements != null ? av.Elements.Length : 0);
                }});

            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),
                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(tt.Items == null ? 0 : tt.Items.Length) : 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(tupleItems);
                }
            });
        }
Exemplo n.º 4
0
        void HandleIndexSliceExpression(PostfixExpression x)
        {
            res.IsMethodArguments = true;
            res.ParsedExpression  = x;

            var overloads = new List <AbstractType>();

            if (x.PostfixForeExpression == null)
            {
                return;
            }

            var b     = ExpressionTypeEvaluation.EvaluateType(x.PostfixForeExpression, ctxt);
            var bases = AmbiguousType.TryDissolve(b);

            var ov = TypeDeclarationResolver.ResolveFurtherTypeIdentifier(ExpressionTypeEvaluation.OpSliceIdHash, bases, ctxt, x, false);

            if (ov != null)
            {
                overloads.AddRange(ov);
            }

            ov = TypeDeclarationResolver.ResolveFurtherTypeIdentifier(ExpressionTypeEvaluation.OpIndexIdHash, bases, ctxt, x, false);
            if (ov != null)
            {
                overloads.AddRange(ov);
            }

            if (overloads.Count == 0)
            {
                b = DResolver.StripMemberSymbols(b);
                var toTypeDecl = new DTypeToTypeDeclVisitor();
                var aa         = b as AssocArrayType;
                if (aa != null)
                {
                    var retType = aa.ValueType != null?aa.ValueType.Accept(toTypeDecl) : null;

                    var dm = new DMethod {
                        Name = "opIndex",
                        Type = retType
                    };
                    dm.Parameters.Add(new DVariable {
                        Name = "index",
                        Type = aa.KeyType != null ? aa.KeyType.Accept(toTypeDecl) : null
                    });
                    overloads.Add(new MemberSymbol(dm, aa.ValueType, x));

                    if ((aa is ArrayType) && !(aa as ArrayType).IsStaticArray)
                    {
                        dm = new DMethod
                        {
                            Name = "opSlice",
                            Type = retType
                        };
                        overloads.Add(new MemberSymbol(dm, aa.ValueType, x));
                    }
                }
                else if (b is PointerType)
                {
                    b = (b as PointerType).Base;
                    var dm = new DMethod
                    {
                        Name = "opIndex",
                        Type = b != null?b.Accept(toTypeDecl) : null
                    };
                    dm.Parameters.Add(new DVariable
                    {
                        Name = "index",
                        Type = new IdentifierDeclaration("size_t")
                    });
                    overloads.Add(new MemberSymbol(dm, b, x));
                }
            }

            res.ResolvedTypesOrMethods = overloads.ToArray();
        }
		public void Visit(PostfixExpression_ArrayAccess x)
		{//TODO for Slices: Omit opIndex overloads if it's obvious that we don't want them -- a[1.. |
			if (x.Arguments != null)
				res.CurrentlyTypedArgumentIndex = x.Arguments.Length;

			res.IsMethodArguments = true;
			res.ParsedExpression = x;

			var overloads = new List<AbstractType>();

			if (x.PostfixForeExpression == null)
				return;

			var b = ExpressionTypeEvaluation.EvaluateType(x.PostfixForeExpression, ctxt);

			var ov = TypeDeclarationResolver.ResolveFurtherTypeIdentifier(ExpressionTypeEvaluation.OpSliceIdHash, b, ctxt, x, false);
			if (ov != null)
				overloads.AddRange(ov);

			ov = TypeDeclarationResolver.ResolveFurtherTypeIdentifier(ExpressionTypeEvaluation.OpIndexIdHash, b, ctxt, x, false);
			if (ov != null)
				overloads.AddRange(ov);

			if (overloads.Count == 0)
			{
				b = DResolver.StripMemberSymbols(b);
				var toTypeDecl = new DTypeToTypeDeclVisitor();
				var aa = b as AssocArrayType;
				if (aa != null){
					var retType = aa.ValueType != null ? aa.ValueType.Accept(toTypeDecl) : null;
					var dm = new DMethod { 
						Name = "opIndex",
						Type = retType
					};
					dm.Parameters.Add(new DVariable { 
						Name = "index",
						Type = aa.KeyType != null ? aa.KeyType.Accept(toTypeDecl) : null 
					});
					overloads.Add(new MemberSymbol(dm, aa.ValueType));

					if ((aa is ArrayType) && !(aa as ArrayType).IsStaticArray)
					{
						dm = new DMethod
						{
							Name = "opSlice",
							Type = retType
						};
						overloads.Add(new MemberSymbol(dm, aa.ValueType));
					}
				}
				else if (b is PointerType)
				{
					b = (b as PointerType).Base;
					var dm = new DMethod
					{
						Name = "opIndex",
						Type = b != null ? b.Accept(toTypeDecl) : null
					};
					dm.Parameters.Add(new DVariable
					{
						Name = "index",
						Type = new IdentifierDeclaration("size_t")
					});
					overloads.Add(new MemberSymbol(dm, b));
				}
			}

			res.ResolvedTypesOrMethods = overloads.ToArray();
		}
        public string GenTooltipSignature(AbstractType t, bool templateParamCompletion = false, int currentMethodParam = -1)
        {
            var ds = t as DSymbol;

            if (ds != null)
            {
                if (currentMethodParam >= 0 && !templateParamCompletion && ds.Definition is DVariable && ds.Base != null)
                {
                    return(GenTooltipSignature(ds.Base, false, currentMethodParam));
                }

                var aliasedSymbol = ds.Tag as D_Parser.Resolver.TypeResolution.TypeDeclarationResolver.AliasTag;
                return(GenTooltipSignature(aliasedSymbol == null || currentMethodParam >= 0 ? ds.Definition : aliasedSymbol.aliasDefinition, templateParamCompletion, currentMethodParam, DTypeToTypeDeclVisitor.GenerateTypeDecl(ds.Base), ds.DeducedTypes != null ? new DeducedTypeDictionary(ds) : null));
            }

            if (t is PackageSymbol)
            {
                var pack = (t as PackageSymbol).Package;
                return("<i>(Package)</i> " + pack.ToString());
            }

            if (t is DelegateType)
            {
                var dt = t as DelegateType;
                //TODO
            }

            return(DCodeToMarkup(t != null ? t.ToCode(true) : "null"));
        }