/// <summary>
        /// Parses and returns a method.
        /// </summary>
        /// <param name="parent">The parent of the element.</param>
        /// <param name="elementReference">A reference to the element being created.</param>
        /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param>
        /// <param name="generated">Indicates whether the code is marked as generated code.</param>
        /// <param name="xmlHeader">The element's documentation header.</param>
        /// <param name="attributes">The attributes on the element.</param>
        /// <returns>Returns the element.</returns>
        private Method ParseMethod(
            CsElement parent, 
            Reference<ICodePart> elementReference,
            bool unsafeCode, 
            bool generated, 
            XmlHeader xmlHeader, 
            ICollection<Attribute> attributes)
        {
            Param.AssertNotNull(parent, "parent");
            Param.AssertNotNull(elementReference, "elementReference");
            Param.Ignore(unsafeCode);
            Param.Ignore(generated);
            Param.Ignore(xmlHeader);
            Param.Ignore(attributes);

            Node<CsToken> previousTokenNode = this.tokens.Last;

            // Get the modifiers and access.
            AccessModifierType accessModifier = AccessModifierType.Private;

            // Methods within interfaces always have the access of the parent interface.
            Interface parentInterface = parent as Interface;
            if (parentInterface != null)
            {
                accessModifier = parentInterface.AccessModifier;
            }

            // Get the declared modifiers for the method.
            Dictionary<CsTokenType, CsToken> modifiers = this.GetElementModifiers(elementReference, ref accessModifier, MethodModifiers);

            unsafeCode |= modifiers.ContainsKey(CsTokenType.Unsafe);

            TypeToken returnType = null;
            if (!modifiers.ContainsKey(CsTokenType.Implicit) && !modifiers.ContainsKey(CsTokenType.Explicit))
            {
                // Get the return type.
                returnType = this.GetTypeToken(elementReference, unsafeCode, true);
                this.tokens.Add(returnType);
            }

            // Get the name of the method.
            string methodName = null;

            Symbol symbol = this.GetNextSymbol(elementReference);
            if (symbol.SymbolType == SymbolType.Operator)
            {
                this.tokens.Add(this.GetToken(CsTokenType.Operator, SymbolType.Operator, elementReference));

                // Advance up to the next symbol.
                this.AdvanceToNextCodeSymbol(elementReference);

                // The overloaded item will either be a type or a symbol.
                int endIndex = -1;
                CsToken operatorType = null;

                if (this.HasTypeSignature(1, unsafeCode, out endIndex))
                {
                    // The overloaded item is a type.
                    operatorType = this.GetTypeToken(elementReference, unsafeCode, true);
                }
                else
                {
                    // The overloaded item is a symbol.
                    operatorType = this.ConvertOperatorOverloadSymbol(elementReference);
                }

                this.tokens.Add(operatorType);
                methodName = "operator " + operatorType.Text;
            }
            else
            {
                CsToken name = this.GetElementNameToken(elementReference, unsafeCode);
                methodName = name.Text;
                this.tokens.Add(name);
            }

            // Get the parameter list.
            IList<Parameter> parameters = this.ParseParameterList(
                elementReference, unsafeCode, SymbolType.OpenParenthesis, modifiers.ContainsKey(CsTokenType.Static));

            // Check whether there are any type constraint clauses.
            ICollection<TypeParameterConstraintClause> typeConstraints = null;
            symbol = this.GetNextSymbol(elementReference);
            if (symbol.Text == "where")
            {
                typeConstraints = this.ParseTypeConstraintClauses(elementReference, unsafeCode);
            }

            // Create the declaration.
            Node<CsToken> firstTokenNode = previousTokenNode == null ? this.tokens.First : previousTokenNode.Next;
            CsTokenList declarationTokens = new CsTokenList(this.tokens, firstTokenNode, this.tokens.Last);
            Declaration declaration = new Declaration(
                declarationTokens, methodName, ElementType.Method, accessModifier, modifiers);

            Method method = new Method(
                this.document,
                parent,
                xmlHeader,
                attributes,
                declaration,
                returnType,
                parameters,
                typeConstraints,
                unsafeCode,
                generated);

            elementReference.Target = method;

            // If the element is extern, abstract, or containing within an interface, it will not have a body.
            if (modifiers.ContainsKey(CsTokenType.Abstract) ||
                modifiers.ContainsKey(CsTokenType.Extern) ||
                parent.ElementType == ElementType.Interface)
            {
                // Get the closing semicolon.
                this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon, elementReference));
            }
            else
            {
                // Get the method body.
                this.ParseStatementContainer(method, true, unsafeCode);
            }

            return method;
        }
示例#2
0
 private Method ParseMethod(CsElement parent, bool unsafeCode, bool generated, XmlHeader xmlHeader, ICollection<Microsoft.StyleCop.CSharp.Attribute> attributes)
 {
     Microsoft.StyleCop.Node<CsToken> last = this.tokens.Last;
     AccessModifierType @private = AccessModifierType.Private;
     Interface interface2 = parent as Interface;
     if (interface2 != null)
     {
         @private = interface2.AccessModifier;
     }
     Dictionary<CsTokenType, CsToken> elementModifiers = this.GetElementModifiers(ref @private, MethodModifiers);
     unsafeCode |= elementModifiers.ContainsKey(CsTokenType.Unsafe);
     TypeToken item = null;
     if (!elementModifiers.ContainsKey(CsTokenType.Implicit) && !elementModifiers.ContainsKey(CsTokenType.Explicit))
     {
         item = this.GetTypeToken(unsafeCode, true);
         this.tokens.Add(item);
     }
     string name = null;
     if (this.GetNextSymbol().SymbolType == SymbolType.Operator)
     {
         this.tokens.Add(this.GetToken(CsTokenType.Operator, SymbolType.Operator));
         this.AdvanceToNextCodeSymbol();
         int endIndex = -1;
         CsToken typeToken = null;
         if (this.HasTypeSignature(1, unsafeCode, out endIndex))
         {
             typeToken = this.GetTypeToken(unsafeCode, true);
         }
         else
         {
             typeToken = this.ConvertOperatorOverloadSymbol();
         }
         this.tokens.Add(typeToken);
         name = "operator " + typeToken.Text;
     }
     else
     {
         CsToken elementNameToken = this.GetElementNameToken(unsafeCode);
         name = elementNameToken.Text;
         this.tokens.Add(elementNameToken);
     }
     IList<Parameter> parameters = this.ParseParameterList(unsafeCode, SymbolType.OpenParenthesis, elementModifiers.ContainsKey(CsTokenType.Static));
     ICollection<TypeParameterConstraintClause> typeConstraints = null;
     if (this.GetNextSymbol().Text == "where")
     {
         typeConstraints = this.ParseTypeConstraintClauses(unsafeCode);
     }
     Microsoft.StyleCop.Node<CsToken> firstItemNode = (last == null) ? this.tokens.First : last.Next;
     CsTokenList tokens = new CsTokenList(this.tokens, firstItemNode, this.tokens.Last);
     Declaration declaration = new Declaration(tokens, name, ElementType.Method, @private, elementModifiers);
     Method element = new Method(this.document, parent, xmlHeader, attributes, declaration, item, parameters, typeConstraints, unsafeCode, generated);
     if ((elementModifiers.ContainsKey(CsTokenType.Abstract) || elementModifiers.ContainsKey(CsTokenType.Extern)) || (parent.ElementType == ElementType.Interface))
     {
         this.tokens.Add(this.GetToken(CsTokenType.Semicolon, SymbolType.Semicolon));
         return element;
     }
     this.ParseStatementContainer(element, true, unsafeCode);
     return element;
 }