예제 #1
0
        public static AbstractType Resolve(ArrayDecl ad, ResolutionContext ctxt)
        {
            var valueTypes = Resolve(ad.ValueType, ctxt);

            ctxt.CheckForSingleResult(valueTypes, ad);

            AbstractType valueType        = null;
            AbstractType keyType          = null;
            int          fixedArrayLength = -1;

            if (valueTypes != null && valueTypes.Length != 0)
            {
                valueType = valueTypes[0];
            }

            ISymbolValue val;

            keyType = ResolveKey(ad, out fixedArrayLength, out val, ctxt);

            if (keyType == null || (keyType is PrimitiveType &&
                                    ((PrimitiveType)keyType).TypeToken == DTokens.Int))
            {
                if (fixedArrayLength >= 0)
                {
                    // D Magic: One might access tuple items directly in the pseudo array declaration - so stuff like Tup[0] i; becomes e.g. int i;
                    var dtup = DResolver.StripMemberSymbols(valueType) as DTuple;
                    if (dtup == null)
                    {
                        return(new ArrayType(valueType, fixedArrayLength, ad));
                    }

                    if (dtup.Items != null && fixedArrayLength < dtup.Items.Length)
                    {
                        return(AbstractType.Get(dtup.Items [fixedArrayLength]));
                    }
                    else
                    {
                        ctxt.LogError(ad, "TypeTuple only consists of " + (dtup.Items != null ? dtup.Items.Length : 0) + " items. Can't access item at index " + fixedArrayLength);
                        return(null);
                    }
                }
                return(new ArrayType(valueType, ad));
            }

            return(new AssocArrayType(valueType, keyType, ad));
        }
예제 #2
0
            public AbstractType Visit(DVariable variable)
            {
                AbstractType bt;

                if (CanResolveBase(variable))
                {
                    var bts = TypeDeclarationResolver.Resolve(variable.Type, ctxt);

                    if (bts != null && bts.Length != 0)
                    {
                        bt = bts[0];
                    }

                    // For auto variables, use the initializer to get its type
                    else if (variable.Initializer != null)
                    {
                        bt = DResolver.StripMemberSymbols(ExpressionTypeEvaluation.EvaluateType(variable.Initializer, ctxt));
                    }
                    else
                    {
                        bt = null;
                    }

                    // Check if inside an foreach statement header
                    if (bt == null && ctxt.ScopedStatement != null)
                    {
                        bt = GetForeachIteratorType(variable);
                    }

                    if (bt == null)
                    {
                        ctxt.CheckForSingleResult(bts, variable.Type as ISyntaxRegion ?? variable.Initializer);
                    }
                }
                else
                {
                    bt = null;
                }

                // Note: Also works for aliases! In this case, we simply try to resolve the aliased type, otherwise the variable's base type
                return(variable.IsAlias ?
                       new AliasedType(variable, bt, typeBase) :
                       new MemberSymbol(variable, bt, typeBase));
            }
예제 #3
0
        public static AbstractType Resolve(TypeOfDeclaration typeOf, ResolutionContext ctxt)
        {
            // typeof(return)
            if (typeOf.Expression is TokenExpression && (typeOf.Expression as TokenExpression).Token == DTokens.Return)
            {
                var m = HandleNodeMatch(ctxt.ScopedBlock, ctxt, null, typeOf);
                if (m != null)
                {
                    return(m);
                }
            }
            // typeOf(myInt)  =>  int
            else if (typeOf.Expression != null)
            {
                var wantedTypes = ExpressionTypeEvaluation.EvaluateType(typeOf.Expression, ctxt);
                return(DResolver.StripMemberSymbols(wantedTypes));
            }

            return(null);
        }
예제 #4
0
            /// <summary>
            /// string[] s;
            ///
            /// foreach(i;s)
            /// {
            ///		// i is of type 'string'
            ///		writeln(i);
            /// }
            /// </summary>
            public AbstractType GetForeachIteratorType(DVariable i)
            {
                var r       = new List <AbstractType>();
                var curStmt = ctxt.ScopedStatement;

                bool init = true;

                // Walk up statement hierarchy -- note that foreach loops can be nested
                while (curStmt != null)
                {
                    if (init)
                    {
                        init = false;
                    }
                    else
                    {
                        curStmt = curStmt.Parent;
                    }

                    if (curStmt is ForeachStatement)
                    {
                        var fe = (ForeachStatement)curStmt;

                        if (fe.ForeachTypeList == null)
                        {
                            continue;
                        }

                        // If the searched variable is declared in the header
                        int iteratorIndex = -1;

                        for (int j = 0; j < fe.ForeachTypeList.Length; j++)
                        {
                            if (fe.ForeachTypeList[j] == i)
                            {
                                iteratorIndex = j;
                                break;
                            }
                        }

                        if (iteratorIndex == -1)
                        {
                            continue;
                        }

                        bool keyIsSearched = iteratorIndex == 0 && fe.ForeachTypeList.Length > 1;


                        // foreach(var k, var v; 0 .. 9)
                        if (keyIsSearched && fe.IsRangeStatement)
                        {
                            // -- it's static type int, of course(?)
                            return(new PrimitiveType(DTokens.Int));
                        }

                        var aggregateType = ExpressionTypeEvaluation.EvaluateType(fe.Aggregate, ctxt);

                        aggregateType = DResolver.StripMemberSymbols(aggregateType);

                        if (aggregateType == null)
                        {
                            return(null);
                        }

                        // The most common way to do a foreach
                        if (aggregateType is AssocArrayType)
                        {
                            var ar = (AssocArrayType)aggregateType;

                            return(keyIsSearched ? ar.KeyType : ar.ValueType);
                        }
                        else if (aggregateType is UserDefinedType)
                        {
                            var tr = (UserDefinedType)aggregateType;

                            if (keyIsSearched || !(tr.Definition is IBlockNode))
                            {
                                continue;
                            }

                            bool foundIterPropertyMatch = false;
                            #region Foreach over Structs and Classes with Ranges

                            // Enlist all 'back'/'front' members
                            var t_l = new List <AbstractType>();

                            foreach (var n in (IBlockNode)tr.Definition)
                            {
                                if (fe.IsReverse ? n.Name == "back" : n.Name == "front")
                                {
                                    t_l.Add(HandleNodeMatch(n, ctxt));
                                }
                            }

                            // Remove aliases
                            var iterPropertyTypes = DResolver.StripAliasSymbols(t_l);

                            foreach (var iterPropType in iterPropertyTypes)
                            {
                                if (iterPropType is MemberSymbol)
                                {
                                    foundIterPropertyMatch = true;

                                    var itp = (MemberSymbol)iterPropType;

                                    // Only take non-parameterized methods
                                    if (itp.Definition is DMethod && ((DMethod)itp.Definition).Parameters.Count != 0)
                                    {
                                        continue;
                                    }

                                    // Handle its base type [return type] as iterator type
                                    if (itp.Base != null)
                                    {
                                        r.Add(itp.Base);
                                    }

                                    foundIterPropertyMatch = true;
                                }
                            }

                            if (foundIterPropertyMatch)
                            {
                                continue;
                            }
                            #endregion

                            #region Foreach over Structs and Classes with opApply
                            t_l.Clear();
                            r.Clear();

                            foreach (var n in (IBlockNode)tr.Definition)
                            {
                                if (n is DMethod &&
                                    (fe.IsReverse ? n.Name == "opApplyReverse" : n.Name == "opApply"))
                                {
                                    t_l.Add(HandleNodeMatch(n, ctxt));
                                }
                            }

                            iterPropertyTypes = DResolver.StripAliasSymbols(t_l);

                            foreach (var iterPropertyType in iterPropertyTypes)
                            {
                                if (iterPropertyType is MemberSymbol)
                                {
                                    var mr = (MemberSymbol)iterPropertyType;
                                    var dm = mr.Definition as DMethod;

                                    if (dm == null || dm.Parameters.Count != 1)
                                    {
                                        continue;
                                    }

                                    var dg = dm.Parameters[0].Type as DelegateDeclaration;

                                    if (dg == null || dg.Parameters.Count != fe.ForeachTypeList.Length)
                                    {
                                        continue;
                                    }

                                    var paramType = Resolve(dg.Parameters[iteratorIndex].Type, ctxt);

                                    if (paramType != null && paramType.Length > 0)
                                    {
                                        r.Add(paramType[0]);
                                    }
                                }
                            }
                            #endregion
                        }

                        if (r.Count > 1)
                        {
                            ctxt.LogError(new ResolutionError(curStmt, "Ambigous iterator type"));
                        }

                        return(r.Count != 0 ? r[0] : null);
                    }
                }

                return(null);
            }
예제 #5
0
        /// <summary>
        /// Used for searching further identifier list parts.
        ///
        /// a.b -- nextIdentifier would be 'b' whereas <param name="resultBases">resultBases</param> contained the resolution result for 'a'
        /// </summary>
        public static AbstractType[] ResolveFurtherTypeIdentifier(int nextIdentifierHash,
                                                                  IEnumerable <AbstractType> resultBases,
                                                                  ResolutionContext ctxt,
                                                                  ISyntaxRegion typeIdObject = null)
        {
            MemberSymbol statProp;

            if ((resultBases = DResolver.StripMemberSymbols(resultBases)) == null)
            {
                return(null);
            }

            var r = new List <AbstractType>();

            foreach (var b in resultBases)
            {
                if (b is UserDefinedType)
                {
                    var udt = b as UserDefinedType;
                    var bn  = udt.Definition as IBlockNode;

                    bool pop = b is MixinTemplateType;
                    if (pop)
                    {
                        ctxt.PushNewScope(bn);
                    }
                    ctxt.CurrentContext.IntroduceTemplateParameterTypes(udt);

                    r.AddRange(SingleNodeNameScan.SearchChildrenAndResolve(ctxt, udt, nextIdentifierHash, typeIdObject));

                    List <TemplateParameterSymbol> dedTypes = null;
                    foreach (var t in r)
                    {
                        var ds = t as DSymbol;
                        if (ds != null && ds.DeducedTypes == null)
                        {
                            if (dedTypes == null)
                            {
                                dedTypes = ctxt.DeducedTypesInHierarchy;
                            }

                            ds.DeducedTypes = new System.Collections.ObjectModel.ReadOnlyCollection <TemplateParameterSymbol>(dedTypes);
                        }
                    }

                    statProp = StaticProperties.TryEvalPropertyType(ctxt, b, nextIdentifierHash);
                    if (statProp != null)
                    {
                        r.Add(statProp);
                    }

                    // go the opDispatch way if possible - http://dlang.org/operatoroverloading.html#Dispatch
                    if (r.Count == 0 && nextIdentifierHash != OpDispatchResolution.opDispatchId)
                    {
                        r.AddRange(OpDispatchResolution.TryResolveFurtherIdViaOpDispatch(ctxt, nextIdentifierHash, udt));
                    }

                    if (r.Count == 0)
                    {
                        r.AddRange(UFCSResolver.TryResolveUFCS(b, nextIdentifierHash, !pop && typeIdObject != null ? typeIdObject.Location : ctxt.ScopedBlock.BlockStartLocation, ctxt, typeIdObject));
                    }

                    if (pop)
                    {
                        ctxt.Pop();
                    }
                    else
                    {
                        ctxt.CurrentContext.RemoveParamTypesFromPreferredLocals(udt);
                    }
                }
                else if (b is PackageSymbol)
                {
                    var pack = (b as PackageSymbol).Package;

                    var accessedModule = pack.GetModule(nextIdentifierHash);
                    if (accessedModule != null)
                    {
                        r.Add(new ModuleSymbol(accessedModule as DModule, typeIdObject as ISyntaxRegion, b as PackageSymbol));
                    }
                    else if ((pack = pack.GetPackage(nextIdentifierHash)) != null)
                    {
                        r.Add(new PackageSymbol(pack, typeIdObject as ISyntaxRegion));
                    }
                }
                else if (b is ModuleSymbol)
                {
                    r.AddRange(SingleNodeNameScan.SearchChildrenAndResolve(ctxt, b as ModuleSymbol, nextIdentifierHash, typeIdObject));
                }
                else
                {
                    statProp = StaticProperties.TryEvalPropertyType(ctxt, b, nextIdentifierHash);
                    if (statProp != null)
                    {
                        r.Add(statProp);
                    }

                    if (r.Count == 0)                    // Only if there hasn't been a result yet?
                    {
                        r.AddRange(UFCSResolver.TryResolveUFCS(b, nextIdentifierHash, typeIdObject != null ? typeIdObject.Location : ctxt.ScopedBlock.BlockStartLocation, ctxt, typeIdObject));
                    }
                }
            }

            return(r.Count == 0 ? null : r.ToArray());
        }
예제 #6
0
        public static AbstractType GetMethodReturnType(DMethod method, ResolutionContext ctxt)
        {
            if ((ctxt.Options & ResolutionOptions.DontResolveBaseTypes) == ResolutionOptions.DontResolveBaseTypes)
            {
                return(null);
            }

            /*
             * If a method's type equals null, assume that it's an 'auto' function..
             * 1) Search for a return statement
             * 2) Resolve the returned expression
             * 3) Use that one as the method's type
             */
            bool pushMethodScope = ctxt.ScopedBlock != method;

            if (method.Type != null)
            {
                if (pushMethodScope)
                {
                    ctxt.PushNewScope(method);
                }

                //FIXME: Is it legal to explicitly return a nested type?
                var returnType = TypeDeclarationResolver.Resolve(method.Type, ctxt);

                if (pushMethodScope)
                {
                    ctxt.Pop();
                }

                ctxt.CheckForSingleResult(returnType, method.Type);
                if (returnType != null && returnType.Length > 0)
                {
                    return(returnType[0]);
                }
            }
            else if (method.Body != null)
            {
                ReturnStatement returnStmt = null;
                var             list       = new List <IStatement> {
                    method.Body
                };
                var list2 = new List <IStatement>();

                bool foundMatch = false;
                while (!foundMatch && list.Count > 0)
                {
                    foreach (var stmt in list)
                    {
                        if (stmt is ReturnStatement)
                        {
                            returnStmt = stmt as ReturnStatement;

                            var te = returnStmt.ReturnExpression as TokenExpression;
                            if (te == null || te.Token != DTokens.Null)
                            {
                                foundMatch = true;
                                break;
                            }
                        }

                        var statementContainingStatement = stmt as StatementContainingStatement;
                        if (statementContainingStatement != null)
                        {
                            list2.AddRange(statementContainingStatement.SubStatements);
                        }
                    }

                    list  = list2;
                    list2 = new List <IStatement>();
                }

                if (returnStmt != null && returnStmt.ReturnExpression != null)
                {
                    if (pushMethodScope)
                    {
                        var dedTypes = ctxt.CurrentContext.DeducedTemplateParameters;
                        ctxt.PushNewScope(method, returnStmt);

                        if (dedTypes.Count != 0)
                        {
                            foreach (var kv in dedTypes)
                            {
                                ctxt.CurrentContext.DeducedTemplateParameters[kv.Key] = kv.Value;
                            }
                        }
                    }

                    var t = DResolver.StripMemberSymbols(ExpressionTypeEvaluation.EvaluateType(returnStmt.ReturnExpression, ctxt));

                    if (pushMethodScope)
                    {
                        ctxt.Pop();
                    }

                    return(t);
                }

                return(new PrimitiveType(DTokens.Void));
            }

            return(null);
        }
예제 #7
0
        private static bool DeduceParam(ResolverContextStack ctxt,
                                        DSymbol overload,
                                        DeducedTypeDictionary deducedTypes,
                                        IEnumerator <ISemantic> argEnum,
                                        ITemplateParameter expectedParam)
        {
            if (expectedParam is TemplateThisParameter && overload.Base != null)
            {
                var ttp = (TemplateThisParameter)expectedParam;

                // Get the type of the type of 'this' - so of the result that is the overload's base
                var t = DResolver.StripMemberSymbols(overload.Base);

                if (t == null || t.DeclarationOrExpressionBase == null)
                {
                    return(false);
                }

                //TODO: Still not sure if it's ok to pass a type result to it
                // - looking at things like typeof(T) that shall return e.g. const(A) instead of A only.

                if (!CheckAndDeduceTypeAgainstTplParameter(ttp, t, deducedTypes, ctxt))
                {
                    return(false);
                }

                return(true);
            }

            // Used when no argument but default arg given
            bool useDefaultType = false;

            if (argEnum.MoveNext() || (useDefaultType = HasDefaultType(expectedParam)))
            {
                // On tuples, take all following arguments and pass them to the check function
                if (expectedParam is TemplateTupleParameter)
                {
                    var tupleItems = new List <ISemantic>();
                    // A tuple must at least contain one item!
                    tupleItems.Add(argEnum.Current);
                    while (argEnum.MoveNext())
                    {
                        tupleItems.Add(argEnum.Current);
                    }

                    if (!CheckAndDeduceTypeTuple((TemplateTupleParameter)expectedParam, tupleItems, deducedTypes, ctxt))
                    {
                        return(false);
                    }
                }
                else if (argEnum.Current != null)
                {
                    if (!CheckAndDeduceTypeAgainstTplParameter(expectedParam, argEnum.Current, deducedTypes, ctxt))
                    {
                        return(false);
                    }
                }
                else if (useDefaultType && CheckAndDeduceTypeAgainstTplParameter(expectedParam, null, deducedTypes, ctxt))
                {
                    // It's legit - just do nothing
                }
                else
                {
                    return(false);
                }
            }
            // There might be too few args - but that doesn't mean that it's not correct - it's only required that all parameters got satisfied with a type
            else if (!AllParamatersSatisfied(deducedTypes))
            {
                return(false);
            }

            return(true);
        }
예제 #8
0
        /// <summary>
        /// Takes the class passed via the tr, and resolves its base class and/or implemented interfaces.
        /// Also usable for enums.
        ///
        /// Never returns null. Instead, the original 'tr' object will be returned if no base class was resolved.
        /// Will clone 'tr', whereas the new object will contain the base class.
        /// </summary>
        public static TemplateIntermediateType ResolveClassOrInterface(DClassLike dc, ResolutionContext ctxt, ISyntaxRegion instanceDeclaration, bool ResolveFirstBaseIdOnly = false, IEnumerable <TemplateParameterSymbol> extraDeducedTemplateParams = null)
        {
            if (parsedClassInstanceDecls == null)
            {
                parsedClassInstanceDecls = new List <ISyntaxRegion> ();
            }

            switch (dc.ClassType)
            {
            case DTokens.Class:
            case DTokens.Interface:
                break;

            default:
                if (dc.BaseClasses.Count != 0)
                {
                    ctxt.LogError(dc, "Only classes and interfaces may inherit from other classes/interfaces");
                }
                return(null);
            }

            bool isClass = dc.ClassType == DTokens.Class;

            if (bcStack > 6 || (instanceDeclaration != null && parsedClassInstanceDecls.Contains(instanceDeclaration)))
            {
                return(isClass ? new ClassType(dc, instanceDeclaration, null) as TemplateIntermediateType : new InterfaceType(dc, instanceDeclaration));
            }

            if (instanceDeclaration != null)
            {
                parsedClassInstanceDecls.Add(instanceDeclaration);
            }
            bcStack++;

            var deducedTypes = new DeducedTypeDictionary(dc);
            var tix          = instanceDeclaration as TemplateInstanceExpression;

            if (tix != null && (ctxt.Options & ResolutionOptions.NoTemplateParameterDeduction) == 0)
            {
                // Pop a context frame as we still need to resolve the template instance expression args in the place where the expression occurs, not the instantiated class' location
                var backup = ctxt.Pop();
                if (ctxt.CurrentContext == null)
                {
                    ctxt.Push(backup);
                }

                var givenTemplateArguments = TemplateInstanceHandler.PreResolveTemplateArgs(tix, ctxt);

                if (ctxt.CurrentContext != backup)
                {
                    foreach (var kv in ctxt.CurrentContext.DeducedTemplateParameters)
                    {
                        backup.DeducedTemplateParameters [kv.Key] = kv.Value;
                        deducedTypes [kv.Key] = kv.Value;
                    }
                    ctxt.Push(backup);
                }

                if (!TemplateInstanceHandler.DeduceParams(givenTemplateArguments, false, ctxt, null, dc, deducedTypes))
                {
                    parsedClassInstanceDecls.Remove(instanceDeclaration);
                    bcStack--;
                    return(null);
                }
            }

            if (extraDeducedTemplateParams != null)
            {
                foreach (var tps in extraDeducedTemplateParams)
                {
                    deducedTypes[tps.Parameter] = tps;
                }
            }


            if (dc.BaseClasses == null || dc.BaseClasses.Count < 1)
            {
                parsedClassInstanceDecls.Remove(instanceDeclaration);
                bcStack--;

                // The Object class has no further base class;
                // Normal class instances have the object as base class;
                // Interfaces must not have any default base class/interface
                return(isClass ? new ClassType(dc, instanceDeclaration, dc.NameHash != ObjectNameHash ? ctxt.ParseCache.ObjectClassResult : null, null, deducedTypes.Count != 0 ? deducedTypes.ToReadonly() : null) :
                       new InterfaceType(dc, instanceDeclaration, null, deducedTypes.Count != 0 ? deducedTypes.ToReadonly() : null) as TemplateIntermediateType);
            }


            #region Base class & interface resolution
            TemplateIntermediateType baseClass = null;
            var interfaces = new List <InterfaceType>();

            var back = ctxt.ScopedBlock;
            using (ctxt.Push(dc.Parent))
            {
                var pop = back != ctxt.ScopedBlock;

                foreach (var kv in deducedTypes)
                {
                    ctxt.CurrentContext.DeducedTemplateParameters[kv.Key] = kv.Value;
                }

                try
                {
                    for (int i = 0; i < (ResolveFirstBaseIdOnly ? 1 : dc.BaseClasses.Count); i++)
                    {
                        var type = dc.BaseClasses[i];

                        // If there's an explicit 'Object' inheritance, also return the pre-resolved object class
                        if (type is IdentifierDeclaration &&
                            (type as IdentifierDeclaration).IdHash == ObjectNameHash)
                        {
                            if (baseClass != null)
                            {
                                ctxt.LogError(new ResolutionError(dc, "Class must not have two base classes"));
                                continue;
                            }
                            else if (i != 0)
                            {
                                ctxt.LogError(new ResolutionError(dc, "The base class name must preceed base interfaces"));
                                continue;
                            }

                            baseClass = ctxt.ParseCache.ObjectClassResult;
                            continue;
                        }

                        if (type == null || (type is IdentifierDeclaration && (type as IdentifierDeclaration).IdHash == dc.NameHash) || dc.NodeRoot == dc)
                        {
                            ctxt.LogError(new ResolutionError(dc, "A class cannot inherit from itself"));
                            continue;
                        }

                        var r = DResolver.StripMemberSymbols(TypeDeclarationResolver.ResolveSingle(type, ctxt));

                        if (r is ClassType || r is TemplateType)
                        {
                            if (!isClass)
                            {
                                ctxt.LogError(new ResolutionError(type, "An interface cannot inherit from non-interfaces"));
                            }
                            else if (i == 0)
                            {
                                baseClass = r as TemplateIntermediateType;
                            }
                            else
                            {
                                ctxt.LogError(new ResolutionError(dc, "The base " + (r is ClassType ? "class" : "template") + " name must preceed base interfaces"));
                            }
                        }
                        else if (r is InterfaceType)
                        {
                            interfaces.Add(r as InterfaceType);

                            if (isClass && dc.NameHash != ObjectNameHash && baseClass == null)
                            {
                                baseClass = ctxt.ParseCache.ObjectClassResult;
                            }
                        }
                        else
                        {
                            ctxt.LogError(new ResolutionError(type, "Resolved class is neither a class nor an interface"));
                            continue;
                        }
                    }
                }
                finally
                {
                    bcStack--;
                    parsedClassInstanceDecls.Remove(instanceDeclaration);
                }

                if (!pop)
                {
                    foreach (var kv in deducedTypes)                     // May be backup old tps?
                    {
                        ctxt.CurrentContext.DeducedTemplateParameters.Remove(kv.Key);
                    }
                }
            }
            #endregion

            if (isClass)
            {
                return(new ClassType(dc, instanceDeclaration, baseClass, interfaces.Count == 0 ? null : interfaces.ToArray(), deducedTypes.Count != 0 ? deducedTypes.ToReadonly() : null));
            }

            return(new InterfaceType(dc, instanceDeclaration, interfaces.Count == 0 ? null : interfaces.ToArray(), deducedTypes.Count != 0 ? deducedTypes.ToReadonly() : null));
        }