internal CatchStatement(CsTokenList tokens, Microsoft.StyleCop.CSharp.TryStatement tryStatement, Expression classExpression, BlockStatement embeddedStatement) : base(StatementType.Catch, tokens) { this.tryStatement = tryStatement; this.catchExpression = classExpression; this.embeddedStatement = embeddedStatement; if (classExpression != null) { base.AddExpression(classExpression); if (classExpression != null) { if (classExpression.ExpressionType == ExpressionType.Literal) { this.classType = ((LiteralExpression) classExpression).Token as TypeToken; } else if (classExpression.ExpressionType == ExpressionType.VariableDeclaration) { VariableDeclarationExpression expression = (VariableDeclarationExpression) classExpression; this.classType = expression.Type; foreach (VariableDeclaratorExpression expression2 in expression.Declarators) { this.identifier = expression2.Identifier.Text; break; } } } } base.AddStatement(embeddedStatement); }
/// <summary> /// Initializes a new instance of the Field class. /// </summary> /// <param name="document">The documenent that contains the element.</param> /// <param name="parent">The parent of the element.</param> /// <param name="header">The Xml header for this element.</param> /// <param name="attributes">The list of attributes attached to this element.</param> /// <param name="declaration">The declaration code for this element.</param> /// <param name="fieldType">The type of the field.</param> /// <param name="unsafeCode">Indicates whether the element resides within a block of unsafe code.</param> /// <param name="generated">Indicates whether the code element was generated or written by hand.</param> internal Field( CsDocument document, CsElement parent, XmlHeader header, ICollection<Attribute> attributes, Declaration declaration, TypeToken fieldType, bool unsafeCode, bool generated) : base(document, parent, ElementType.Field, "field " + declaration.Name, header, attributes, declaration, unsafeCode, generated) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(parent, "parent"); Param.Ignore(header); Param.Ignore(attributes); Param.AssertNotNull(declaration, "declaration"); Param.AssertNotNull(fieldType, "fieldType"); Param.Ignore(unsafeCode); Param.Ignore(generated); this.type = fieldType; // Determine whether the item is const or readonly. this.isConst = this.Declaration.ContainsModifier(CsTokenType.Const); this.isReadOnly = this.Declaration.ContainsModifier(CsTokenType.Readonly); }
internal Method(CsDocument document, CsElement parent, XmlHeader header, ICollection<Microsoft.StyleCop.CSharp.Attribute> attributes, Declaration declaration, TypeToken returnType, IList<Parameter> parameters, ICollection<TypeParameterConstraintClause> typeConstraints, bool unsafeCode, bool generated) : base(document, parent, ElementType.Method, "method " + declaration.Name, header, attributes, declaration, unsafeCode, generated) { this.returnType = returnType; this.parameters = parameters; this.typeConstraints = typeConstraints; if ((this.parameters.Count > 0) && base.Declaration.ContainsModifier(new CsTokenType[] { CsTokenType.Static })) { foreach (Parameter parameter in this.parameters) { if ((parameter.Modifiers & ParameterModifiers.This) != ParameterModifiers.None) { this.extensionMethod = true; } break; } } base.QualifiedName = CodeParser.AddQualifications(this.parameters, base.QualifiedName); if ((base.Declaration.Name.IndexOf(".", StringComparison.Ordinal) > -1) && !base.Declaration.Name.StartsWith("this.", StringComparison.Ordinal)) { base.Declaration.AccessModifierType = AccessModifierType.Public; } if (typeConstraints != null) { foreach (TypeParameterConstraintClause clause in typeConstraints) { clause.ParentElement = this; } } }
internal Field(CsDocument document, CsElement parent, XmlHeader header, ICollection<Microsoft.StyleCop.CSharp.Attribute> attributes, Declaration declaration, TypeToken fieldType, bool unsafeCode, bool generated) : base(document, parent, ElementType.Field, "field " + declaration.Name, header, attributes, declaration, unsafeCode, generated) { this.type = fieldType; this.isConst = base.Declaration.ContainsModifier(new CsTokenType[] { CsTokenType.Const }); this.isReadOnly = base.Declaration.ContainsModifier(new CsTokenType[] { CsTokenType.Readonly }); }
/// <summary> /// Initializes a new instance of the GenericTypeParameter class. /// </summary> /// <param name="type">The generic type parameter.</param> /// <param name="modifiers">Optional modifiers.</param> internal GenericTypeParameter(TypeToken type, ParameterModifiers modifiers) { Param.AssertNotNull(type, "type"); Param.Ignore(modifiers); this.type = type; this.modifiers = modifiers; }
internal Variable(TypeToken type, string name, VariableModifiers modifiers, CodePoint location, bool generated) { this.type = type; this.name = name; this.modifiers = modifiers; this.location = location; this.generated = generated; }
internal IsExpression(CsTokenList tokens, Expression value, LiteralExpression type) : base(ExpressionType.Is, tokens) { this.value = value; this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type); base.AddExpression(value); base.AddExpression(type); }
internal Property(CsDocument document, CsElement parent, XmlHeader header, ICollection<Microsoft.StyleCop.CSharp.Attribute> attributes, Declaration declaration, TypeToken returnType, bool unsafeCode, bool generated) : base(document, parent, ElementType.Property, "property " + declaration.Name, header, attributes, declaration, unsafeCode, generated) { this.returnType = returnType; if ((base.Declaration.Name.IndexOf(".", StringComparison.Ordinal) > -1) && !base.Declaration.Name.StartsWith("this.", StringComparison.Ordinal)) { base.Declaration.AccessModifierType = AccessModifierType.Public; } }
internal Parameter(TypeToken type, string name, ParameterModifiers modifiers, CodeLocation location, CsTokenList tokens, bool generated) { this.type = type; this.name = name; this.modifiers = modifiers; this.location = location; this.tokens = tokens; this.generated = generated; }
/// <summary> /// Initializes a new instance of the DefaultValueExpression class. /// </summary> /// <param name="tokens">The list of tokens that form the expression.</param> /// <param name="type">The type to obtain the default value of.</param> internal DefaultValueExpression(CsTokenList tokens, LiteralExpression type) : base(ExpressionType.DefaultValue, tokens) { Param.AssertNotNull(tokens, "tokens"); Param.AssertNotNull(type, "type"); this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type); this.AddExpression(type); }
internal Indexer(CsDocument document, CsElement parent, XmlHeader header, ICollection<Microsoft.StyleCop.CSharp.Attribute> attributes, Declaration declaration, TypeToken returnType, IList<Parameter> parameters, bool unsafeCode, bool generated) : base(document, parent, ElementType.Indexer, "indexer " + declaration.Name, header, attributes, declaration, unsafeCode, generated) { this.returnType = returnType; this.parameters = parameters; base.QualifiedName = CodeParser.AddQualifications(this.parameters, base.QualifiedName); if ((base.Declaration.Name.IndexOf(".", StringComparison.Ordinal) > -1) && !base.Declaration.Name.StartsWith("this.", StringComparison.Ordinal)) { base.Declaration.AccessModifierType = AccessModifierType.Public; } }
internal VariableDeclarationExpression(CsTokenList tokens, LiteralExpression type, ICollection<VariableDeclaratorExpression> declarators) : base(ExpressionType.VariableDeclaration, tokens) { this.declarators = declarators; base.AddExpression(type); this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type); foreach (VariableDeclaratorExpression expression in declarators) { base.AddExpression(expression); expression.ParentVariable = this; } }
/// <summary> /// Initializes a new instance of the Delegate class. /// </summary> /// <param name="document">The document that contains the element.</param> /// <param name="parent">The parent of the element.</param> /// <param name="header">The Xml header for this element.</param> /// <param name="attributes">The list of attributes attached to this element.</param> /// <param name="declaration">The declaration code for this element.</param> /// <param name="returnType">The return type.</param> /// <param name="parameters">The parameters to the delegate.</param> /// <param name="typeConstraints">The list of type constraints on the element.</param> /// <param name="unsafeCode">Indicates whether the element resides within a block of unsafe code.</param> /// <param name="generated">Indicates whether this is generated code.</param> internal Delegate( CsDocument document, CsElement parent, XmlHeader header, ICollection<Attribute> attributes, Declaration declaration, TypeToken returnType, IList<Parameter> parameters, ICollection<TypeParameterConstraintClause> typeConstraints, bool unsafeCode, bool generated) : base(document, parent, ElementType.Delegate, "delegate " + declaration.Name, header, attributes, declaration, unsafeCode, generated) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(parent, "parent"); Param.Ignore(header); Param.Ignore(attributes); Param.AssertNotNull(declaration, "declaration"); Param.AssertNotNull(returnType, "returnType"); Param.AssertNotNull(parameters, "parameters"); Param.Ignore(typeConstraints); Param.Ignore(unsafeCode); Param.Ignore(generated); this.returnType = returnType; this.typeConstraints = typeConstraints; this.parameters = parameters; Debug.Assert(parameters.IsReadOnly, "The parameters collection should be read-only."); // Add the qualifications this.QualifiedName = CodeParser.AddQualifications(this.parameters, this.QualifiedName); // Set the parent of the type constraint clauses. if (typeConstraints != null) { Debug.Assert(typeConstraints.IsReadOnly, "The collection of type constraints should be read-only."); foreach (TypeParameterConstraintClause constraint in typeConstraints) { constraint.ParentElement = this; } } }
/// <summary> /// Initializes a new instance of the AsExpression class. /// </summary> /// <param name="tokens">The list of tokens that form the expression.</param> /// <param name="value">The value to convert.</param> /// <param name="type">The type of the conversion.</param> internal AsExpression(CsTokenList tokens, Expression value, LiteralExpression type) : base(ExpressionType.As, tokens) { Param.AssertNotNull(tokens, "tokens"); Param.AssertNotNull(value, "value"); Param.AssertNotNull(type, "type"); this.value = value; this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type); this.AddExpression(value); this.AddExpression(type); }
/// <summary> /// Initializes a new instance of the CastExpression class. /// </summary> /// <param name="tokens">The list of tokens that form the expression.</param> /// <param name="type">The cast type.</param> /// <param name="castedExpression">The expression being casted.</param> internal CastExpression(CsTokenList tokens, LiteralExpression type, Expression castedExpression) : base(ExpressionType.Cast, tokens) { Param.AssertNotNull(tokens, "tokens"); Param.AssertNotNull(type, "type"); Param.AssertNotNull(castedExpression, "castedExpression"); this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type); this.castedExpression = castedExpression; this.AddExpression(type); this.AddExpression(castedExpression); }
/// <summary> /// Initializes a new instance of the Variable class. /// </summary> /// <param name="type">The type of the variable.</param> /// <param name="name">The name of the variable.</param> /// <param name="modifiers">Modifers applied to this variable.</param> /// <param name="location">The location of the variable.</param> /// <param name="parent">The parent code part.</param> /// <param name="generated">Indicates whethre the variable is located within a block of generated code.</param> internal Variable(TypeToken type, string name, VariableModifiers modifiers, CodeLocation location, Reference<ICodePart> parent, bool generated) { Param.Ignore(type); Param.AssertValidString(name, "name"); Param.Ignore(modifiers); Param.AssertNotNull(location, "location"); Param.AssertNotNull(parent, "parent"); Param.Ignore(generated); this.type = type; this.name = name; this.modifiers = modifiers; this.location = location; this.parent = parent; this.generated = generated; }
/// <summary> /// Initializes a new instance of the Indexer class. /// </summary> /// <param name="document">The documenent that contains the element.</param> /// <param name="parent">The parent of the element.</param> /// <param name="header">The Xml header for this element.</param> /// <param name="attributes">The list of attributes attached to this element.</param> /// <param name="declaration">The declaration code for this element.</param> /// <param name="returnType">The return type of the indexer.</param> /// <param name="parameters">The parameters to the indexer.</param> /// <param name="unsafeCode">Indicates whether the element resides within a block of unsafe code.</param> /// <param name="generated">Indicates whether the code element was generated or written by hand.</param> internal Indexer( CsDocument document, CsElement parent, XmlHeader header, ICollection<Attribute> attributes, Declaration declaration, TypeToken returnType, IList<Parameter> parameters, bool unsafeCode, bool generated) : base(document, parent, ElementType.Indexer, "indexer " + declaration.Name, header, attributes, declaration, unsafeCode, generated) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(parent, "parent"); Param.Ignore(header); Param.Ignore(attributes); Param.AssertNotNull(declaration, "declaration"); Param.Ignore(returnType); Param.AssertNotNull(parameters, "parameters"); Param.Ignore(unsafeCode); Param.Ignore(generated); this.returnType = returnType; this.parameters = parameters; Debug.Assert(parameters.IsReadOnly, "The parameters collection should be read-only."); // Add the qualifications this.QualifiedName = CodeParser.AddQualifications(this.parameters, this.QualifiedName); // If this is an explicit interface member implementation and our access modifier // is currently set to private because we don't have one, then it should be public instead. if (this.Declaration.Name.IndexOf(".", StringComparison.Ordinal) > -1 && !this.Declaration.Name.StartsWith("this.", StringComparison.Ordinal)) { this.Declaration.AccessModifierType = AccessModifierType.Public; } }
/// <summary> /// Initializes a new instance of the Property class. /// </summary> /// <param name="document">The documenent that contains the element.</param> /// <param name="parent">The parent of the element.</param> /// <param name="header">The Xml header for this element.</param> /// <param name="attributes">The list of attributes attached to this element.</param> /// <param name="declaration">The declaration code for this element.</param> /// <param name="returnType">The property return type.</param> /// <param name="unsafeCode">Indicates whether the element resides within a block of unsafe code.</param> /// <param name="generated">Indicates whether the code element was generated or written by hand.</param> internal Property( CsDocument document, CsElement parent, XmlHeader header, ICollection<Attribute> attributes, Declaration declaration, TypeToken returnType, bool unsafeCode, bool generated) : base(document, parent, ElementType.Property, "property " + declaration.Name, header, attributes, declaration, unsafeCode, generated) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(parent, "parent"); Param.Ignore(header); Param.Ignore(attributes); Param.AssertNotNull(declaration, "declaration"); Param.AssertNotNull(returnType, "returnType"); Param.Ignore(unsafeCode); Param.Ignore(generated); this.returnType = returnType; // If this is an explicit interface member implementation and our access modifier // is currently set to private because we don't have one, then it should be public instead. if (this.Declaration.Name.IndexOf(".", StringComparison.Ordinal) > -1 && !this.Declaration.Name.StartsWith("this.", StringComparison.Ordinal)) { this.Declaration.AccessModifierType = AccessModifierType.Public; } }
/// <summary> /// Initializes a new instance of the VariableDeclarationExpression class. /// </summary> /// <param name="tokens">The list of tokens that form the statement.</param> /// <param name="type">The type of the variable or variables being declared.</param> /// <param name="declarators">The list of declarators in the expression.</param> internal VariableDeclarationExpression( CsTokenList tokens, LiteralExpression type, ICollection<VariableDeclaratorExpression> declarators) : base(ExpressionType.VariableDeclaration, tokens) { Param.AssertNotNull(tokens, "tokens"); Param.AssertNotNull(type, "type"); Param.AssertNotNull(declarators, "declarators"); this.declarators = declarators; Debug.Assert(declarators.IsReadOnly, "The declarators collection should be read-only."); this.AddExpression(type); this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type); foreach (VariableDeclaratorExpression expression in declarators) { this.AddExpression(expression); expression.ParentVariable = this; } }
private IList<VariableDeclaratorExpression> ParseFieldDeclarators(bool unsafeCode, TypeToken fieldType) { List<VariableDeclaratorExpression> list = new List<VariableDeclaratorExpression>(); Symbol nextSymbol = this.GetNextSymbol(); while (nextSymbol.SymbolType != SymbolType.Semicolon) { CsToken elementNameToken = this.GetElementNameToken(unsafeCode, true); Microsoft.StyleCop.Node<CsToken> firstItemNode = this.tokens.InsertLast(elementNameToken); Expression initializer = null; if (this.GetNextSymbol().SymbolType == SymbolType.Equals) { this.tokens.Add(this.GetOperatorToken(OperatorType.Equals)); if (this.GetNextSymbol().SymbolType == SymbolType.OpenCurlyBracket) { if ((fieldType.Text == "var") || (((fieldType.Text != "Array") && (fieldType.Text != "System.Array")) && !fieldType.Text.Contains("["))) { initializer = this.GetAnonymousTypeInitializerExpression(unsafeCode); } else { initializer = this.GetArrayInitializerExpression(unsafeCode); } } else { initializer = this.GetNextExpression(ExpressionPrecedence.None, unsafeCode); } if (initializer == null) { throw this.CreateSyntaxException(); } } list.Add(new VariableDeclaratorExpression(new CsTokenList(this.tokens, firstItemNode, this.tokens.Last), new LiteralExpression(this.tokens, firstItemNode), initializer)); nextSymbol = this.GetNextSymbol(); if (nextSymbol.SymbolType == SymbolType.Comma) { this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma)); nextSymbol = this.GetNextSymbol(); } } return list.ToArray(); }
/// <summary> /// Fills in the details of the set accessor. /// </summary> /// <param name="parent">The parent of the accessor.</param> private void FillSetAccessorDetails(CsElement parent) { Param.AssertNotNull(parent, "parent"); var accessorReference = new Reference<ICodePart>(this); Property property = parent as Property; if (property != null) { // Set accessors on properties do not have a return type but have an // implied input parameter. this.returnType = CreateVoidTypeToken(accessorReference); this.parameters = new Parameter[] { new Parameter( property.ReturnType, "value", accessorReference, ParameterModifiers.None, null, CodeLocation.Empty, null, property.ReturnType.Generated) }; } else { // Set accessors on indexers do not have a return type but, but have the input // parameters of the parent indexer. Indexer indexer = (Indexer)parent as Indexer; this.returnType = CreateVoidTypeToken(accessorReference); Parameter[] tempParameters = new Parameter[indexer.Parameters.Count + 1]; int i = 0; foreach (Parameter parameter in indexer.Parameters) { tempParameters[i++] = parameter; } // Add the implicit value parameter since this is a set accessor. tempParameters[i] = new Parameter( indexer.ReturnType, "value", accessorReference, ParameterModifiers.None, null, CodeLocation.Empty, null, indexer.ReturnType.Generated); this.parameters = tempParameters; } }
/// <summary> /// Fills in the details of the get accessor. /// </summary> /// <param name="parent">The parent of the accessor.</param> private void FillGetAccessorDetails(CsElement parent) { Param.AssertNotNull(parent, "parent"); Property property = parent as Property; if (property != null) { // Get accessors on properties have the return type of their parent property, // and have no input parameters. this.returnType = property.ReturnType; } else { // Get accessors on indexers have the return type of their parent indexer, // and have the input parameters of the parent indexer. Indexer indexer = (Indexer)parent; this.returnType = indexer.ReturnType; this.parameters = indexer.Parameters; } }
/// <summary> /// Fills in the details of the accessor based on its type. /// </summary> /// <param name="parent">The parent of the accessor.</param> private void FillDetails(CsElement parent) { Param.AssertNotNull(parent, "parent"); // Set the return type and parameters. if (this.accessorType == AccessorType.Get) { this.FillGetAccessorDetails(parent); } else if (this.accessorType == AccessorType.Set) { this.FillSetAccessorDetails(parent); } else { // Add and remove accessors have no return type but have an implied // parameter based on the type of the event handler. Event parentEvent = (Event)parent; var accessorReference = new Reference<ICodePart>(this); this.returnType = CreateVoidTypeToken(accessorReference); this.parameters = new Parameter[] { new Parameter( parentEvent.EventHandlerType, "value", accessorReference, ParameterModifiers.None, null, CodeLocation.Empty, null, parentEvent.EventHandlerType.Generated) }; } }
/// <summary> /// Parses and returns the declarators for a field. /// </summary> /// <param name="fieldReference">A reference to the field.</param> /// <param name="unsafeCode">Indicates whether the code is marked as unsafe.</param> /// <param name="fieldType">The field type.</param> /// <returns>Returns the declarators.</returns> private IList<VariableDeclaratorExpression> ParseFieldDeclarators(Reference<ICodePart> fieldReference, bool unsafeCode, TypeToken fieldType) { Param.AssertNotNull(fieldReference, "fieldReference"); Param.Ignore(unsafeCode); Param.AssertNotNull(fieldType, "fieldType"); List<VariableDeclaratorExpression> declarators = new List<VariableDeclaratorExpression>(); Symbol symbol = this.GetNextSymbol(fieldReference); while (symbol.SymbolType != SymbolType.Semicolon) { var expressionReference = new Reference<ICodePart>(); // Get the identifier. CsToken identifier = this.GetElementNameToken(expressionReference, unsafeCode, true); Node<CsToken> identifierTokenNode = this.tokens.InsertLast(identifier); Expression initialization = null; // Check whether there is an equals sign. symbol = this.GetNextSymbol(expressionReference); if (symbol.SymbolType == SymbolType.Equals) { this.tokens.Add(this.GetOperatorToken(OperatorType.Equals, expressionReference)); // Get the expression after the equals sign. If the expression starts with an // opening curly bracket, then this is an initialization expression or an // anonymous type initialization expression. symbol = this.GetNextSymbol(expressionReference); if (symbol.SymbolType == SymbolType.OpenCurlyBracket) { // Determine whether this is an array or an anonymous type. if (fieldType.Text == "var" || ( fieldType.Text != "Array" && fieldType.Text != "System.Array" && !fieldType.Text.Contains("["))) { initialization = this.GetAnonymousTypeInitializerExpression(expressionReference, unsafeCode); } else { initialization = this.GetArrayInitializerExpression(unsafeCode); } } else { initialization = this.GetNextExpression(ExpressionPrecedence.None, expressionReference, unsafeCode); } if (initialization == null) { throw this.CreateSyntaxException(); } } var variableDeclarationExpression = new VariableDeclaratorExpression( new CsTokenList(this.tokens, identifierTokenNode, this.tokens.Last), new LiteralExpression(this.tokens, identifierTokenNode), initialization); expressionReference.Target = variableDeclarationExpression; declarators.Add(variableDeclarationExpression); // If the next symbol is a comma, continue. symbol = this.GetNextSymbol(fieldReference); if (symbol.SymbolType == SymbolType.Comma) { this.tokens.Add(this.GetToken(CsTokenType.Comma, SymbolType.Comma, fieldReference)); symbol = this.GetNextSymbol(fieldReference); } } // Return the declarators as a read-only collection. return declarators.ToArray(); }
/// <summary> /// Checks for the presence of a return value tag on an element. /// </summary> /// <param name="element">The element to parse.</param> /// <param name="returnType">The return type.</param> /// <param name="formattedDocs">The formatted Xml document that comprises the header.</param> private void CheckHeaderReturnValue(CsElement element, TypeToken returnType, XmlDocument formattedDocs) { Param.AssertNotNull(element, "element"); Param.Ignore(returnType); Param.AssertNotNull(formattedDocs, "formattedDocs"); if (returnType != null) { XmlNode returnNode = formattedDocs.SelectSingleNode("root/returns"); if (returnType.Text != "void") { if (null == returnNode) { this.AddViolation(element, Rules.ElementReturnValueMustBeDocumented); } else if (returnNode.InnerText == null || returnNode.InnerText.Length == 0) { this.AddViolation(element, Rules.ElementReturnValueDocumentationMustHaveText); } else { this.CheckDocumentationValidity(element, element.LineNumber, returnNode, "return"); } } else { if (null != returnNode) { this.AddViolation(element, Rules.VoidReturnValueMustNotBeDocumented); } } } }
internal DefaultValueExpression(CsTokenList tokens, LiteralExpression type) : base(ExpressionType.DefaultValue, tokens) { this.type = CodeParser.ExtractTypeTokenFromLiteralExpression(type); base.AddExpression(type); }
/// <summary> /// Initializes a new instance of the Parameter class. /// </summary> /// <param name="type">The type of the parameter.</param> /// <param name="name">The name of the parameter.</param> /// <param name="parent">The parent of the parameter.</param> /// <param name="modifiers">Modifers applied to this parameter.</param> /// <param name="defaultArgument">The optional default argument for the parameter.</param> /// <param name="location">The location of the parameter in the code.</param> /// <param name="tokens">The tokens that form the parameter.</param> /// <param name="generated">Indicates whether the parameter is located within a block of generated code.</param> internal Parameter( TypeToken type, string name, Reference<ICodePart> parent, ParameterModifiers modifiers, Expression defaultArgument, CodeLocation location, CsTokenList tokens, bool generated) { Param.Ignore(type); Param.AssertValidString(name, "name"); Param.AssertNotNull(parent, "parent"); Param.Ignore(modifiers); Param.Ignore(defaultArgument); Param.AssertNotNull(location, "location"); Param.Ignore(tokens); Param.Ignore(generated); this.type = type; this.name = name; this.parent = parent; this.modifiers = modifiers; this.defaultArgument = defaultArgument; this.location = location; this.tokens = tokens; this.generated = generated; }
private LiteralExpression ConvertTypeExpression(Expression expression) { Microsoft.StyleCop.Node<CsToken> first = expression.Tokens.First; Microsoft.StyleCop.Node<CsToken> last = expression.Tokens.Last; List<CsToken> items = new List<CsToken>(); foreach (CsToken token in expression.Tokens) { items.Add(token); } if ((first != null) && (expression.Tokens.First != null)) { Microsoft.StyleCop.Node<CsToken> next = first.Next; if (!expression.Tokens.OutOfBounds(next)) { this.tokens.RemoveRange(next, expression.Tokens.Last); } } MasterList<CsToken> childTokens = new MasterList<CsToken>(items); TypeToken newItem = new TypeToken(childTokens, CodeLocation.Join<CsToken>(first, last), first.Value.Generated); return new LiteralExpression(new CsTokenList(this.tokens, first, first), this.tokens.Replace(first, newItem)); }
/// <summary> /// Converts the given expression into a literal expression containing a type token. /// </summary> /// <param name="expression">The expression to convert.</param> /// <returns>Returns the converted expression.</returns> private LiteralExpression ConvertTypeExpression(Expression expression) { Param.AssertNotNull(expression, "expression"); // Get the first and last token in the expression. Node<CsToken> firstTokenNode = expression.Tokens.First; Node<CsToken> lastTokenNode = expression.Tokens.Last; var typeTokenReference = new Reference<ICodePart>(); var expressionReference = new Reference<ICodePart>(); // Create a new token list containing these tokens. List<CsToken> tokenList = new List<CsToken>(); foreach (CsToken token in expression.Tokens) { tokenList.Add(token); token.ParentRef = typeTokenReference; } // Remove the extra tokens from the master list. if (firstTokenNode != null && expression.Tokens.First != null) { Node<CsToken> temp = firstTokenNode.Next; if (!expression.Tokens.OutOfBounds(temp)) { this.tokens.RemoveRange(temp, expression.Tokens.Last); } } // Add the child tokens to a token list. MasterList<CsToken> childTokens = new MasterList<CsToken>(tokenList); // Create the new type token. TypeToken typeToken = new TypeToken( childTokens, CsToken.JoinLocations(firstTokenNode, lastTokenNode), expressionReference, firstTokenNode.Value.Generated); typeTokenReference.Target = typeToken; // Insert the new token. Node<CsToken> typeTokenNode = this.tokens.Replace(firstTokenNode, typeToken); // Create the literal expression. var literalExpression = new LiteralExpression( new CsTokenList(this.tokens, firstTokenNode, firstTokenNode), typeTokenNode); expressionReference.Target = literalExpression; return literalExpression; }
/// <summary> /// Gets a token representing a type identifier. /// </summary> /// <param name="typeTokenReference">A reference to the type token.</param> /// <param name="parentReference">The parent code unit.</param> /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param> /// <param name="includeArrayBrackets">Indicates whether to include array brackets in the type token.</param> /// <param name="isExpression">Indicates whether this type token comes at the end of an 'is' expression.</param> /// <param name="startIndex">The start position in the symbol list of the first symbol in the type token.</param> /// <param name="endIndex">Returns the index of the last symbol in the type token.</param> /// <returns>Returns the token.</returns> private TypeToken GetTypeTokenAux( Reference<ICodePart> typeTokenReference, Reference<ICodePart> parentReference, bool unsafeCode, bool includeArrayBrackets, bool isExpression, int startIndex, out int endIndex) { Param.AssertNotNull(typeTokenReference, "typeTokenReference"); Param.AssertNotNull(parentReference, "parentReference"); Param.Ignore(unsafeCode); Param.Ignore(includeArrayBrackets); Param.Ignore(isExpression); Param.AssertGreaterThanOrEqualToZero(startIndex, "startIndex"); // Get the next symbol and make sure it is an unknown word. Symbol symbol = this.symbols.Peek(startIndex); Debug.Assert(symbol != null && symbol.SymbolType == SymbolType.Other, "Expected a text symbol"); // Create a token list to store all the tokens forming the type. MasterList<CsToken> typeTokens = new MasterList<CsToken>(); // Get the name of the type token plus any generic symbols and types. GenericType generic; this.GetTypeTokenBaseName(typeTokenReference, ref typeTokens, ref startIndex, out generic, unsafeCode); bool allowNullableType = true; // Add dereference symbols if they exist. if (unsafeCode) { if (this.GetTypeTokenDereferenceSymbols(typeTokenReference, typeTokens, ref startIndex)) { allowNullableType = false; } } // Now look for the nullable type symbol, if needed. if (allowNullableType) { this.GetTypeTokenNullableTypeSymbol(typeTokenReference, typeTokens, isExpression, ref startIndex); } // Get the array brackets if they exist. if (includeArrayBrackets) { this.GetTypeTokenArrayBrackets(typeTokenReference, typeTokens, ref startIndex); } if (typeTokens.Count == 0) { throw this.CreateSyntaxException(); } // Set the end index. endIndex = startIndex - 1; // If the type is a generic type, determine whether to just return the generic type directly. if (generic != null && typeTokens.Count == 1) { // This type is only composed of the generic type and nothing else. Just return the generic type. generic.ParentRef = parentReference; return generic; } // The type is either not generic, or else it is composed of a more complex type which includes a generic // (for example, an array of a generic type). Return the more complex type. CodeLocation location = CsToken.JoinLocations(typeTokens.First, typeTokens.Last); var typeToken = new TypeToken(typeTokens, location, parentReference, this.symbols.Generated); typeTokenReference.Target = typeToken; return typeToken; }