Beispiel #1
0
        /// <summary>
        /// Computes the intersection of the matching types for
        /// <paramref name="argument"/>and
        /// <paramref name="parameter"/>. It returns true if the intersection has any elements in
        /// it.
        /// </summary>
        /// <param name="argument">an argument from see cref="Arguments"/></param>
        /// <param name="parameter">a parameter from see
        /// cref="MethodDefinition.Parameters"/></param>
        /// <returns>true if the argument and the parameter have a matching type in common; false
        /// otherwise</returns>
        private bool ArgumentMatchesDefinition(IResolvesToType argument, IParameterDeclaration parameter)
        {
            var possibleArgumentTypes  = argument.FindMatchingTypes();
            var possibleParameterTypes = parameter.VariableType.FindMatchingTypes();

            return(possibleArgumentTypes.Intersect(possibleParameterTypes).Any());
        }
        /// <summary>
        /// Parses the given typeUseElement and returns a TypeUse object. This handles the "var"
        /// keyword for C# if used
        /// </summary>
        /// <param name="typeUseElement">The XML type use element</param>
        /// <param name="context">The parser context</param>
        /// <returns>A new TypeUse object</returns>
        public override ITypeUse ParseTypeUseElement(XElement typeUseElement, ParserContext context)
        {
            if (typeUseElement == null)
            {
                throw new ArgumentNullException("typeUseElement");
            }

            XElement typeElement;
            XElement typeNameElement;

            // validate the type use typeUseElement (must be a SRC.Name or SRC.Type)
            if (typeUseElement.Name == SRC.Type)
            {
                typeElement     = typeUseElement;
                typeNameElement = typeUseElement.Elements(SRC.Name).LastOrDefault();
            }
            else if (typeUseElement.Name == SRC.Name)
            {
                typeElement     = typeUseElement.Ancestors(SRC.Type).FirstOrDefault();
                typeNameElement = typeUseElement;
            }
            else
            {
                throw new ArgumentException("typeUseElement should be of type type or name", "typeUseElement");
            }

            if (typeNameElement.Value == "var")
            {
                var initElement       = typeElement.ElementsAfterSelf(SRC.Init).FirstOrDefault();
                var expressionElement = (null == initElement ? null : initElement.Element(SRC.Expression));
                var callElement       = (null == expressionElement ? null : expressionElement.Element(SRC.Call));

                IResolvesToType initializer = (null == callElement ? null : ParseCallElement(callElement, context));
                var             typeUse     = new CSharpVarTypeUse()
                {
                    Name                = typeNameElement.Value,
                    Initializer         = initializer,
                    ParentScope         = context.CurrentScope,
                    Location            = context.CreateLocation(typeNameElement),
                    ProgrammingLanguage = this.ParserLanguage,
                };
                return(typeUse);
            }
            else
            {
                return(base.ParseTypeUseElement(typeUseElement, context));
            }
        }
Beispiel #3
0
        public static bool IResolvesToTypesAreEqual(IResolvesToType a, IResolvesToType b)
        {
            //TODO: reimplement this using proper OO-ish design
            if (a == b)
            {
                return(true);
            }
            if (a == null || b == null)
            {
                return(false);
            }
            var aType = a.GetType();

            if (aType != b.GetType())
            {
                return(false);
            }
            if (aType.Name == "VariableUse")
            {
                return(VariableUsesAreEqual((IVariableUse)a, (IVariableUse)b));
            }
            else if (aType.Name == "MethodCall")
            {
                return(MethodCallsAreEqual((IMethodCall)a, (IMethodCall)b));
            }
            else if (aType.Name == "TypeUse")
            {
                return(TypeUsesAreEqual((ITypeUse)a, (ITypeUse)b));
            }
            else if (aType.Name == "LiteralUse")
            {
                return(LiteralUsesAreEqual((LiteralUse)a, (LiteralUse)b));
            }

            return(false);
        }