/// <summary>
 /// Get any constraints for the specified <see cref="TypeParameter"/> on this method, or on the base virtual method if this method is an override.
 /// </summary>
 public List <TypeParameterConstraint> GetTypeParameterConstraints(TypeParameter typeParameter)
 {
     // Override methods don't specify constraints - they inherit them from the base virtual method.
     // In order to handle invalid code, just look in the first occurrence of constraints, searching
     // any base method if the current one is an override.
     if (_constraintClauses != null && _constraintClauses.Count > 0)
     {
         foreach (ConstraintClause constraintClause in _constraintClauses)
         {
             if (constraintClause.TypeParameter.Reference == typeParameter)
             {
                 return(constraintClause.Constraints);
             }
         }
     }
     else
     {
         MethodRef baseMethodRef = FindBaseMethod();
         if (baseMethodRef != null)
         {
             // If the constraints are from a base method, we have to translate the type parameter
             int index = FindTypeParameterIndex(typeParameter);
             TypeParameterRef typeParameterRef = baseMethodRef.GetTypeParameter(index);
             return(baseMethodRef.GetTypeParameterConstraints(typeParameterRef));
         }
     }
     return(null);
 }
 protected void ParseNameTypeParameters(Parser parser)
 {
     MoveComments(parser.LastToken);
     _name = parser.GetIdentifierText();                       // Parse the name
     MoveEOLComment(parser.LastToken);                         // Associate any skipped EOL comment
     _typeParameters = TypeParameter.ParseList(parser, this);  // Parse any type parameters
     MoveEOLComment(parser.LastToken);                         // Associate any skipped EOL comment
 }
 internal override void AsTextName(CodeWriter writer, RenderFlags flags)
 {
     base.AsTextName(writer, flags);
     if (HasTypeParameters)
     {
         TypeParameter.AsTextTypeParameters(writer, _typeParameters, flags);
     }
 }
        /// <summary>
        /// Find the index of the specified <see cref="TypeParameter"/> in the declaration of the <see cref="TypeDecl"/> or
        /// an enclosing <see cref="TypeDecl"/> if this one is nested.  Also handles partial types.
        /// </summary>
        /// <returns>The index of the <see cref="TypeParameter"/>, or -1 if not found.</returns>
        public int FindTypeParameterIndex(TypeParameter typeParameter)
        {
            int index;

            if (FindTypeParameterIndex(typeParameter, out index))
            {
                return(index);
            }
            return(-1);
        }
        /// <summary>
        /// Find the index of the specified type parameter.
        /// </summary>
        public int FindTypeParameterIndex(TypeParameter typeParameter)
        {
            int index = 0;

            foreach (TypeParameter genericParameter in TypeParameters)
            {
                if (genericParameter == typeParameter)
                {
                    return(index);
                }
                ++index;
            }
            return(-1);
        }
        public void AsTextName(CodeWriter writer, RenderFlags flags)
        {
            if (flags.HasFlag(RenderFlags.Description) && _parent is TypeDecl)
            {
                ((TypeDecl)_parent).AsTextName(writer, flags);
                Dot.AsTextDot(writer);
            }

            writer.WriteIdentifier(_name, flags);
            if (HasTypeParameters)
            {
                TypeParameter.AsTextTypeParameters(writer, _typeParameters, flags);
            }
        }
 /// <summary>
 /// Get any constraints for the specified <see cref="TypeParameter"/> on this type.
 /// </summary>
 public List <TypeParameterConstraint> GetTypeParameterConstraints(TypeParameter typeParameter)
 {
     if (_constraintClauses != null)
     {
         foreach (ConstraintClause constraintClause in _constraintClauses)
         {
             if (constraintClause.TypeParameter.Reference == typeParameter)
             {
                 return(constraintClause.Constraints);
             }
         }
     }
     return(null);
 }
 private bool FindTypeParameterIndexLocal(TypeParameter typeParameter, ref int index)
 {
     if (_typeParameters != null)
     {
         foreach (TypeParameter localTypeParameter in _typeParameters)
         {
             if (localTypeParameter == typeParameter)
             {
                 return(true);
             }
             ++index;
         }
     }
     return(false);
 }
        /// <summary>
        /// Find the index of the specified <see cref="TypeParameter"/> in the declaration of the <see cref="TypeDecl"/> or
        /// an enclosing <see cref="TypeDecl"/> if this one is nested.  Also handles partial types.
        /// </summary>
        /// <returns>Returns true if the index of the TypeParameter was found, otherwise false.</returns>
        protected bool FindTypeParameterIndex(TypeParameter typeParameter, out int index)
        {
            // First, recursively check any parent types
            if (_parent is TypeDecl)
            {
                if (((TypeDecl)_parent).FindTypeParameterIndex(typeParameter, out index))
                {
                    return(true);
                }
            }
            else
            {
                index = 0;
            }

            // Then, check the type parameters on the current type
            int startingIndex = index;

            if (FindTypeParameterIndexLocal(typeParameter, ref index))
            {
                return(true);
            }

            // Finally, also check the type parameters of any partial types
            if (IsPartial)
            {
                int endingIndex = index;
                foreach (TypeDecl otherPart in GetOtherParts())
                {
                    index = startingIndex;
                    if (otherPart.FindTypeParameterIndexLocal(typeParameter, ref index))
                    {
                        return(true);
                    }
                }
                index = endingIndex;
            }

            return(false);
        }
        /// <summary>
        /// Parse a list of type parameters.
        /// </summary>
        public static ChildList <TypeParameter> ParseList(Parser parser, CodeObject parent)
        {
            ChildList <TypeParameter> parameters = null;

            if (parser.TokenText == ParseTokenStart || (parser.InDocComment && parser.TokenText == ParseTokenAltStart &&
                                                        TypeRefBase.PeekTypeArguments(parser, TypeRefBase.ParseTokenAltArgumentEnd, ParseFlags.None)))
            {
                string argumentEnd = (parser.TokenText == ParseTokenAltStart ? ParseTokenAltEnd : ParseTokenEnd);
                parent.MoveAllComments(parser.LastToken);  // Move any skipped comments to the parent
                parser.NextToken();                        // Move past '<'

                // Create a string of possible terminators (assuming 1 char terminators for now)
                string terminators = argumentEnd + MethodDeclBase.ParseTokenStart + ConstraintClause.ParseTokenSeparator + Statement.ParseTokenTerminator;

                while (parser.Token != null && (parser.TokenText.Length != 1 || terminators.IndexOf(parser.TokenText[0]) < 0))
                {
                    TypeParameter typeParameter = new TypeParameter(parser, parent);
                    if (typeParameter.Name != null)
                    {
                        if (parameters == null)
                        {
                            parameters = new ChildList <TypeParameter>(parent);
                        }
                        parameters.Add(typeParameter);
                        if (parser.TokenText == ParseTokenSeparator)
                        {
                            parser.NextToken();  // Move past ','
                        }
                    }
                    else
                    {
                        parser.NextToken();  // Move past bad token (non-identifier)
                    }
                }
                parser.NextToken();  // Move past '>'
            }
            return(parameters);
        }
 protected internal GenericMethodDecl(Parser parser, CodeObject parent, bool typeParametersAlreadyParsed, ParseFlags flags)
     : base(parser, parent, false, flags)
 {
     if (typeParametersAlreadyParsed)
     {
         // The type parameters were already parsed on the unused Dot expression - fetch them from there
         UnresolvedRef unresolvedRef = (UnresolvedRef)((Dot)parser.LastUnusedCodeObject).Right;
         _typeParameters = new ChildList <TypeParameter>(this);
         foreach (Expression expression in unresolvedRef.TypeArguments)
         {
             _typeParameters.Add(new TypeParameter(expression is UnresolvedRef ? ((UnresolvedRef)expression).Name : null));
         }
         unresolvedRef.TypeArguments = null;
     }
     ParseMethodNameAndType(parser, parent, true, false);
     ParseModifiersAndAnnotations(parser);  // Parse any attributes and/or modifiers
     if (!typeParametersAlreadyParsed)
     {
         _typeParameters = TypeParameter.ParseList(parser, this);  // Parse any type parameters
     }
     ParseParameters(parser);
     _constraintClauses = ConstraintClause.ParseList(parser, this);  // Parse any constraint clauses
     ParseTerminatorOrBody(parser, flags);
 }
Example #12
0
 /// <summary>
 /// Construct a <see cref="TypeParameterRef"/> from a <see cref="TypeParameter"/>.
 /// </summary>
 public TypeParameterRef(TypeParameter declaration, bool isFirstOnLine, List <int> arrayRanks)
     : base(declaration, isFirstOnLine, null, arrayRanks)
 {
 }
Example #13
0
 /// <summary>
 /// Create a <see cref="DocTypeParam"/>.
 /// </summary>
 public DocTypeParam(TypeParameter typeParameter, string text)
     : base(typeParameter.CreateRef(), text)
 {
 }
Example #14
0
 /// <summary>
 /// Create a <see cref="DocTypeParam"/>.
 /// </summary>
 public DocTypeParam(TypeParameter typeParameter, params DocComment[] docComments)
     : base(typeParameter.CreateRef(), docComments)
 {
 }
Example #15
0
 /// <summary>
 /// Construct a <see cref="TypeParameterRef"/> from a <see cref="TypeParameter"/>.
 /// </summary>
 public TypeParameterRef(TypeParameter declaration, params int[] arrayRanks)
     : base(declaration, false, arrayRanks)
 {
 }
Example #16
0
 /// <summary>
 /// Construct a <see cref="TypeParameterRef"/> from a <see cref="TypeParameter"/>.
 /// </summary>
 public TypeParameterRef(TypeParameter declaration, bool isFirstOnLine, params int[] arrayRanks)
     : base(declaration, isFirstOnLine, arrayRanks)
 {
 }
Example #17
0
 /// <summary>
 /// Construct a <see cref="TypeParameterRef"/> from a <see cref="TypeParameter"/>.
 /// </summary>
 public TypeParameterRef(TypeParameter declaration, List <int> arrayRanks)
     : base(declaration, false, null, arrayRanks)
 {
 }
Example #18
0
 /// <summary>
 /// Construct a <see cref="TypeParameterRef"/> from a <see cref="TypeParameter"/>.
 /// </summary>
 public TypeParameterRef(TypeParameter declaration)
     : base(declaration, false)
 {
 }
 /// <summary>
 /// Create a <see cref="DocTypeParamRef"/>.
 /// </summary>
 public DocTypeParamRef(TypeParameter typeParameter)
     : base(typeParameter.CreateRef(), (string)null)
 {
 }
Example #20
0
 /// <summary>
 /// Construct a <see cref="TypeParameterRef"/> from a <see cref="TypeParameter"/>.
 /// </summary>
 public TypeParameterRef(TypeParameter declaration, bool isFirstOnLine)
     : base(declaration, isFirstOnLine)
 {
 }
 /// <summary>
 /// Create a <see cref="ConstraintClause"/>.
 /// </summary>
 public ConstraintClause(TypeParameter typeParameter, params TypeParameterConstraint[] constraints)
     : this(typeParameter.CreateRef(), constraints)
 {
 }