private void TypeChecks(TypeSymbol type, BaseFieldDeclarationSyntax fieldSyntax, VariableDeclaratorSyntax declarator, DiagnosticBag diagnostics) { if (type.IsStatic) { // Cannot declare a variable of static type '{0}' diagnostics.Add(ErrorCode.ERR_VarDeclIsStaticClass, this.Location, type); } else if (type.SpecialType == SpecialType.System_Void) { diagnostics.Add(ErrorCode.ERR_FieldCantHaveVoidType, fieldSyntax.Declaration.Type.Location); } else if (type.IsRestrictedType()) { diagnostics.Add(ErrorCode.ERR_FieldCantBeRefAny, fieldSyntax.Declaration.Type.Location, type); } else if (IsConst && !type.CanBeConst()) { SyntaxToken constToken = default(SyntaxToken); foreach (var modifier in fieldSyntax.Modifiers) { if (modifier.CSharpKind() == SyntaxKind.ConstKeyword) { constToken = modifier; break; } } Debug.Assert(constToken.CSharpKind() == SyntaxKind.ConstKeyword); diagnostics.Add(ErrorCode.ERR_BadConstType, constToken.GetLocation(), type); } else { if (ContainingType.TypeKind == TypeKind.Struct && !IsStatic && !IsConst) { var initializerOpt = declarator.Initializer; if (initializerOpt != null) { // '{0}': cannot have instance field initializers in structs diagnostics.Add(ErrorCode.ERR_FieldInitializerInStruct, this.Location, this); } } if (IsVolatile && !type.IsValidVolatileFieldType()) { // '{0}': a volatile field cannot be of the type '{1}' diagnostics.Add(ErrorCode.ERR_VolatileStruct, this.Location, this, type); } } HashSet<DiagnosticInfo> useSiteDiagnostics = null; if (!this.IsNoMoreVisibleThan(type, ref useSiteDiagnostics)) { // Inconsistent accessibility: field type '{1}' is less accessible than field '{0}' diagnostics.Add(ErrorCode.ERR_BadVisFieldType, this.Location, this, type); } diagnostics.Add(this.Location, useSiteDiagnostics); }
private static List<BaseFieldDeclarationSyntax> SplitDeclaration(BaseFieldDeclarationSyntax baseFieldDeclaration) { var fieldDeclaration = baseFieldDeclaration as FieldDeclarationSyntax; if (fieldDeclaration != null) { VariableDeclarationSyntax declaration = fieldDeclaration.Declaration; SeparatedSyntaxList<VariableDeclaratorSyntax> variables = declaration.Variables; VariableDeclaratorSyntax first = variables.First(); var newFieldDeclarations = new List<BaseFieldDeclarationSyntax>(variables.Count); foreach (VariableDeclaratorSyntax variable in variables) { var variableDeclarator = SyntaxFactory.SingletonSeparatedList(variable); var newFieldDeclaration = fieldDeclaration.WithDeclaration(declaration.WithVariables(variableDeclarator)); if (variable != first) { var triviaList = newFieldDeclaration.GetLeadingTrivia().WithoutDirectiveTrivia(); newFieldDeclaration = newFieldDeclaration.WithLeadingTrivia(triviaList); } newFieldDeclarations.Add(newFieldDeclaration); } return newFieldDeclarations; } var eventFieldDeclaration = baseFieldDeclaration as EventFieldDeclarationSyntax; if (eventFieldDeclaration != null) { VariableDeclarationSyntax declaration = eventFieldDeclaration.Declaration; SeparatedSyntaxList<VariableDeclaratorSyntax> variables = declaration.Variables; var first = variables.First(); var newEventFieldDeclarations = new List<BaseFieldDeclarationSyntax>(variables.Count); foreach (VariableDeclaratorSyntax variable in variables) { var variableDeclarator = SyntaxFactory.SingletonSeparatedList(variable); var newEventFieldDeclaration = eventFieldDeclaration.WithDeclaration(declaration.WithVariables(variableDeclarator)); if (variable != first) { var triviaList = newEventFieldDeclaration.GetLeadingTrivia().WithoutDirectiveTrivia(); newEventFieldDeclaration = newEventFieldDeclaration.WithLeadingTrivia(triviaList); } newEventFieldDeclarations.Add(newEventFieldDeclaration); } return newEventFieldDeclarations; } return null; }
public static bool IsNoCompile(this SemanticModel model, BaseFieldDeclarationSyntax syntax) { foreach (var attrListSyntax in syntax.AttributeLists) { foreach (var attr in attrListSyntax.Attributes) { var type = model.GetTypeInfo(attr); if (type.Type.IsSameType(nameof(JavaScript), nameof(NoCompileAttribute))) return true; } } return false; }
private static List<BaseFieldDeclarationSyntax> SplitDeclaration(Document document, BaseFieldDeclarationSyntax baseFieldDeclaration) { var fieldDeclaration = baseFieldDeclaration as FieldDeclarationSyntax; if (fieldDeclaration != null) { return DeclarationSplitter( document, fieldDeclaration.Declaration, fieldDeclaration.WithDeclaration, fieldDeclaration.SemicolonToken.TrailingTrivia); } var eventFieldDeclaration = baseFieldDeclaration as EventFieldDeclarationSyntax; if (eventFieldDeclaration != null) { return DeclarationSplitter( document, eventFieldDeclaration.Declaration, eventFieldDeclaration.WithDeclaration, eventFieldDeclaration.SemicolonToken.TrailingTrivia); } return null; }
private void ClassifyFieldInsert(BaseFieldDeclarationSyntax field) { ClassifyModifiedMemberInsert(field.Modifiers); }
private void ClassifyUpdate(BaseFieldDeclarationSyntax oldNode, BaseFieldDeclarationSyntax newNode) { if (oldNode.Kind() != newNode.Kind()) { ReportError(RudeEditKind.FieldKindUpdate); return; } Debug.Assert(!SyntaxFactory.AreEquivalent(oldNode.Modifiers, newNode.Modifiers)); ReportError(RudeEditKind.ModifiersUpdate); return; }
/// <summary> /// Given a base field declaration syntax, get the corresponding symbols. /// </summary> /// <param name="declarationSyntax">The syntax node that declares one or more fields or events.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The field symbols that were declared.</returns> internal override ImmutableArray<ISymbol> GetDeclaredSymbols(BaseFieldDeclarationSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)) { CheckSyntaxNode(declarationSyntax); var builder = new ArrayBuilder<ISymbol>(); foreach (var declarator in declarationSyntax.Declaration.Variables) { var field = this.GetDeclaredSymbol(declarator, cancellationToken) as ISymbol; if (field != null) { builder.Add(field); } } return builder.ToImmutableAndFree(); }
private bool CompareFieldDeclarations( BaseFieldDeclarationSyntax oldField, BaseFieldDeclarationSyntax newField, SyntaxNode newNodeParent, CodeModelEventQueue eventQueue) { Debug.Assert(oldField != null && newField != null); bool same = true; same &= CompareChildren( CompareVariableDeclarators, oldField.Declaration.Variables.AsReadOnlyList(), newField.Declaration.Variables.AsReadOnlyList(), newNodeParent, CodeModelEventType.Unknown, eventQueue); // If modifiers have changed enqueue a element changed (unknown change) node if ((oldField.Kind() != newField.Kind()) || !CompareModifiers(oldField, newField)) { EnqueueChangeEvent(newField, newNodeParent, CodeModelEventType.Unknown, eventQueue); same = false; } // If type had changed enqueue a element changed (typeref changed) node if (!CompareTypes(oldField.Declaration.Type, newField.Declaration.Type)) { EnqueueChangeEvent(newField, newNodeParent, CodeModelEventType.TypeRefChange, eventQueue); same = false; } same &= CompareChildren( CompareAttributeLists, oldField.AttributeLists.AsReadOnlyList(), newField.AttributeLists.AsReadOnlyList(), newField, CodeModelEventType.Unknown, eventQueue); return same; }
/// <summary> /// Given a base field declaration syntax, get the corresponding symbols. /// </summary> /// <param name="declarationSyntax">The syntax node that declares one or more fields or events.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The symbols that were declared.</returns> internal abstract ImmutableArray<ISymbol> GetDeclaredSymbols(BaseFieldDeclarationSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken));
internal override ImmutableArray<ISymbol> GetDeclaredSymbols(BaseFieldDeclarationSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)) { // Can't define field inside member. return ImmutableArray.Create<ISymbol>(); }
internal static DeclarationModifiers MakeModifiers(NamedTypeSymbol containingType, BaseFieldDeclarationSyntax fieldSyntax, DiagnosticBag diagnostics, out bool modifierErrors) { DeclarationModifiers defaultAccess = (containingType.IsInterface) ? DeclarationModifiers.Public : DeclarationModifiers.Private; DeclarationModifiers allowedModifiers = DeclarationModifiers.AccessibilityMask | DeclarationModifiers.Const | DeclarationModifiers.New | DeclarationModifiers.ReadOnly | DeclarationModifiers.Static | DeclarationModifiers.Volatile | DeclarationModifiers.Fixed | DeclarationModifiers.Unsafe | DeclarationModifiers.Abstract; // filtered out later var firstIdentifier = fieldSyntax.Declaration.Variables[0].Identifier; var errorLocation = new SourceLocation(firstIdentifier); DeclarationModifiers result = ModifierUtils.MakeAndCheckNontypeMemberModifiers( fieldSyntax.Modifiers, defaultAccess, allowedModifiers, errorLocation, diagnostics, out modifierErrors); if ((result & DeclarationModifiers.Abstract) != 0) { diagnostics.Add(ErrorCode.ERR_AbstractField, errorLocation); result &= ~DeclarationModifiers.Abstract; } if ((result & DeclarationModifiers.Const) != 0) { if ((result & DeclarationModifiers.Static) != 0) { // The constant '{0}' cannot be marked static diagnostics.Add(ErrorCode.ERR_StaticConstant, errorLocation, firstIdentifier.ValueText); } if ((result & DeclarationModifiers.ReadOnly) != 0) { // The modifier 'readonly' is not valid for this item diagnostics.Add(ErrorCode.ERR_BadMemberFlag, errorLocation, SyntaxFacts.GetText(SyntaxKind.ReadOnlyKeyword)); } if ((result & DeclarationModifiers.Volatile) != 0) { // The modifier 'volatile' is not valid for this item diagnostics.Add(ErrorCode.ERR_BadMemberFlag, errorLocation, SyntaxFacts.GetText(SyntaxKind.VolatileKeyword)); } if ((result & DeclarationModifiers.Unsafe) != 0) { // The modifier 'unsafe' is not valid for this item diagnostics.Add(ErrorCode.ERR_BadMemberFlag, errorLocation, SyntaxFacts.GetText(SyntaxKind.UnsafeKeyword)); } result |= DeclarationModifiers.Static; // "constants are considered static members" } else { // NOTE: always cascading on a const, so suppress. // NOTE: we're being a bit sneaky here - we're using the containingType rather than this symbol // to determine whether or not unsafe is allowed. Since this symbol and the containing type are // in the same compilation, it won't make a difference. We do, however, have to pass the error // location explicitly. containingType.CheckUnsafeModifier(result, errorLocation, diagnostics); } return result; }
private static List <BaseFieldDeclarationSyntax> SplitDeclaration(Document document, BaseFieldDeclarationSyntax baseFieldDeclaration) { var fieldDeclaration = baseFieldDeclaration as FieldDeclarationSyntax; if (fieldDeclaration != null) { return(DeclarationSplitter( document, fieldDeclaration.Declaration, fieldDeclaration.WithDeclaration, fieldDeclaration.SemicolonToken.TrailingTrivia)); } var eventFieldDeclaration = baseFieldDeclaration as EventFieldDeclarationSyntax; if (eventFieldDeclaration != null) { return(DeclarationSplitter( document, eventFieldDeclaration.Declaration, eventFieldDeclaration.WithDeclaration, eventFieldDeclaration.SemicolonToken.TrailingTrivia)); } return(null); }
public static PropertyDeclarationSyntax BuildPropertyWithSynchronizedBackingField(PropertyDeclarationSyntax property, BaseFieldDeclarationSyntax backingField) { var synchronizedProperty = SyntaxFactory.PropertyDeclaration(property.Type, property.Identifier.Text) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword).WithTrailingTrivia(SyntaxTriviaList.Create(SyntaxFactory.Space))) .AddAccessorListAccessors(GetDefaultGetter(backingField), GetDefaultSetter(backingField) ); return(synchronizedProperty); }
private static bool IsSuitableDeclaredField(BaseFieldDeclarationSyntax z, ArgumentSyntax y) { return new[] { z.Declaration.Variables.FirstOrDefault()?.Identifier.Text + ".Object", z.Declaration.Variables.FirstOrDefault()?.Identifier.Text }.Contains(y.Expression.GetText().ToString().Trim()); }
private void VisitBaseFieldDeclaration(BaseFieldDeclarationSyntax node) { if (node == null) throw new ArgumentNullException("node"); WriteLeadingTrivia(node); node.Validate(); _writer.WriteIndent(); WriteAttributes( node, _writer.Configuration.LineBreaksAndWrapping.Other.PlaceFieldAttributeOnSameLine ); WriteMemberModifiers(node.Modifiers); if (node.SyntaxKind == SyntaxKind.EventFieldDeclaration) { _writer.WriteKeyword(PrinterKeyword.Event); _writer.WriteSpace(); } if ((node.Modifiers & Modifiers.Const) != 0) { _writer.WriteKeyword(PrinterKeyword.Const); _writer.WriteSpace(); } node.Declaration.Accept(this); _writer.EndStatement(); WriteTrailingTrivia(node); }
private static TextSpan?TryCreateSpanForFieldDeclaration(BaseFieldDeclarationSyntax fieldDeclaration, int position) => TryCreateSpanForVariableDeclaration(fieldDeclaration.Declaration, fieldDeclaration.Modifiers, fieldDeclaration.SemicolonToken, position);
public static bool IsPublic(this BaseFieldDeclarationSyntax node) { return(node.Modifiers.Any(m => m.IsKind(SyntaxKind.PublicKeyword)) || node.Parent.IsKind(SyntaxKind.InterfaceDeclaration)); }