public ArrayCreationExpressionSyntax(TypeSyntax type, List<IExpressionSyntax> parameter) : base(type, parameter) { }
public static Accessibility DetermineAccessibilityConstraint( this SemanticModel semanticModel, TypeSyntax type, CancellationToken cancellationToken) { if (type == null) { return(Accessibility.Private); } type = GetOutermostType(type); // Interesting cases based on 3.5.4 Accessibility constraints in the language spec. // If any of the below hold, then we will override the default accessibility if the // constraint wants the type to be more accessible. i.e. if by default we generate // 'internal', but a constraint makes us 'public', then be public. // 1) The direct base class of a class type must be at least as accessible as the // class type itself. // // 2) The explicit base interfaces of an interface type must be at least as accessible // as the interface type itself. if (type != null) { if (type.Parent is BaseTypeSyntax && type.Parent.IsParentKind(SyntaxKind.BaseList) && ((BaseTypeSyntax)type.Parent).Type == type) { var containingType = semanticModel.GetDeclaredSymbol(type.GetAncestor <BaseTypeDeclarationSyntax>(), cancellationToken) as INamedTypeSymbol; if (containingType != null && containingType.TypeKind == TypeKind.Interface) { return(containingType.DeclaredAccessibility); } else if (((BaseListSyntax)type.Parent.Parent).Types[0] == type.Parent) { return(containingType.DeclaredAccessibility); } } } // 4) The type of a constant must be at least as accessible as the constant itself. // 5) The type of a field must be at least as accessible as the field itself. if (type.IsParentKind(SyntaxKind.VariableDeclaration) && type.Parent.IsParentKind(SyntaxKind.FieldDeclaration)) { var variableDeclaration = (VariableDeclarationSyntax)type.Parent; return(semanticModel.GetDeclaredSymbol( variableDeclaration.Variables[0], cancellationToken).DeclaredAccessibility); } // Also do the same check if we are in an object creation expression if (type.IsParentKind(SyntaxKind.ObjectCreationExpression) && type.Parent.IsParentKind(SyntaxKind.EqualsValueClause) && type.Parent.Parent.IsParentKind(SyntaxKind.VariableDeclarator) && type.Parent.Parent.Parent.IsParentKind(SyntaxKind.VariableDeclaration) && type.Parent.Parent.Parent.Parent.IsParentKind(SyntaxKind.FieldDeclaration)) { var variableDeclaration = (VariableDeclarationSyntax)type.Parent.Parent.Parent.Parent; return(semanticModel.GetDeclaredSymbol( variableDeclaration.Variables[0], cancellationToken).DeclaredAccessibility); } // 3) The return type of a delegate type must be at least as accessible as the // delegate type itself. // 6) The return type of a method must be at least as accessible as the method // itself. // 7) The type of a property must be at least as accessible as the property itself. // 8) The type of an event must be at least as accessible as the event itself. // 9) The type of an indexer must be at least as accessible as the indexer itself. // 10) The return type of an operator must be at least as accessible as the operator // itself. if (type.IsParentKind(SyntaxKind.DelegateDeclaration) || type.IsParentKind(SyntaxKind.MethodDeclaration) || type.IsParentKind(SyntaxKind.PropertyDeclaration) || type.IsParentKind(SyntaxKind.EventDeclaration) || type.IsParentKind(SyntaxKind.IndexerDeclaration) || type.IsParentKind(SyntaxKind.OperatorDeclaration)) { return(semanticModel.GetDeclaredSymbol( type.Parent, cancellationToken).DeclaredAccessibility); } // 3) The parameter types of a delegate type must be at least as accessible as the // delegate type itself. // 6) The parameter types of a method must be at least as accessible as the method // itself. // 9) The parameter types of an indexer must be at least as accessible as the // indexer itself. // 10) The parameter types of an operator must be at least as accessible as the // operator itself. // 11) The parameter types of an instance constructor must be at least as accessible // as the instance constructor itself. if (type.IsParentKind(SyntaxKind.Parameter) && type.Parent.IsParentKind(SyntaxKind.ParameterList)) { if (type.Parent.Parent.IsParentKind(SyntaxKind.DelegateDeclaration) || type.Parent.Parent.IsParentKind(SyntaxKind.MethodDeclaration) || type.Parent.Parent.IsParentKind(SyntaxKind.IndexerDeclaration) || type.Parent.Parent.IsParentKind(SyntaxKind.OperatorDeclaration)) { return(semanticModel.GetDeclaredSymbol( type.Parent.Parent.Parent, cancellationToken).DeclaredAccessibility); } if (type.Parent.Parent.IsParentKind(SyntaxKind.ConstructorDeclaration)) { var symbol = semanticModel.GetDeclaredSymbol(type.Parent.Parent.Parent, cancellationToken); if (!symbol.IsStatic) { return(symbol.DeclaredAccessibility); } } } // 8) The type of an event must be at least as accessible as the event itself. if (type.IsParentKind(SyntaxKind.VariableDeclaration) && type.Parent.IsParentKind(SyntaxKind.EventFieldDeclaration)) { var variableDeclaration = (VariableDeclarationSyntax)type.Parent; var symbol = semanticModel.GetDeclaredSymbol(variableDeclaration.Variables[0], cancellationToken); if (symbol != null) { return(symbol.DeclaredAccessibility); } } return(Accessibility.Private); }
public IEnumerable <StatementSyntax> AddComponent(TranslationContext context, ExpressionSyntax entity, ExpressionSyntax componentDeclaration, TypeSyntax componentTypeSyntax, bool isSharedComponent) { var expressionId = isSharedComponent ? nameof(EntityCommandBuffer.Concurrent.AddSharedComponent) : nameof(EntityCommandBuffer.Concurrent.AddComponent); var arguments = m_Concurrent ? new[] { Argument(IdentifierName(context.GetJobIndexParameterName())), Argument(entity), Argument(componentDeclaration) } : new[] { Argument(entity), Argument(componentDeclaration) }; yield return(ExpressionStatement(RoslynBuilder.MethodInvocation( expressionId, context.GetOrDeclareCommandBuffer(true), arguments, new[] { componentTypeSyntax }))); }
public static SpeculativeSyntaxTreeSemanticModel Create(SyntaxTreeSemanticModel parentSemanticModel, TypeSyntax root, Binder rootBinder, int position, SpeculativeBindingOption bindingOption) { return(CreateCore(parentSemanticModel, root, rootBinder, position, bindingOption)); }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, PropertyDeclarationSyntax propertyDeclaration) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkMemberAsStatic) && propertyDeclaration.Span.Contains(context.Span) && MarkMemberAsStaticRefactoring.CanRefactor(propertyDeclaration)) { context.RegisterRefactoring( "Mark property as static", cancellationToken => MarkMemberAsStaticRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken)); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MarkContainingClassAsAbstract) && propertyDeclaration.HeaderSpan().Contains(context.Span) && MarkContainingClassAsAbstractRefactoring.CanRefactor(propertyDeclaration)) { context.RegisterRefactoring( "Mark containing class as abstract", cancellationToken => MarkContainingClassAsAbstractRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken)); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplacePropertyWithMethod) && propertyDeclaration.HeaderSpan().Contains(context.Span)) { ReplacePropertyWithMethodRefactoring.ComputeRefactoring(context, propertyDeclaration); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemovePropertyInitializer) && RemovePropertyInitializerRefactoring.CanRefactor(context, propertyDeclaration)) { context.RegisterRefactoring( "Remove property initializer", cancellationToken => RemovePropertyInitializerRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken)); } if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ExpandProperty, RefactoringIdentifiers.ExpandPropertyAndAddBackingField) && propertyDeclaration.Span.Contains(context.Span) && ExpandPropertyRefactoring.CanRefactor(propertyDeclaration)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandProperty)) { context.RegisterRefactoring( "Expand property", cancellationToken => ExpandPropertyRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken)); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandPropertyAndAddBackingField)) { context.RegisterRefactoring( "Expand property and add backing field", cancellationToken => ExpandPropertyAndAddBackingFieldRefactoring.RefactorAsync(context.Document, propertyDeclaration, context.Settings.PrefixFieldIdentifierWithUnderscore, cancellationToken)); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.NotifyPropertyChanged) && await NotifyPropertyChangedRefactoring.CanRefactorAsync(context, propertyDeclaration).ConfigureAwait(false)) { context.RegisterRefactoring( "Notify property changed", cancellationToken => { return(NotifyPropertyChangedRefactoring.RefactorAsync( context.Document, propertyDeclaration, context.SupportsCSharp6, cancellationToken)); }); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) && propertyDeclaration.HeaderSpan().Contains(context.Span) && MakeMemberAbstractRefactoring.CanRefactor(propertyDeclaration)) { context.RegisterRefactoring( "Make property abstract", cancellationToken => MakeMemberAbstractRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken)); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) && propertyDeclaration.HeaderSpan().Contains(context.Span)) { await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, propertyDeclaration).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenamePropertyAccordingToTypeName)) { TypeSyntax type = propertyDeclaration.Type; if (type != null) { SyntaxToken identifier = propertyDeclaration.Identifier; if (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(identifier)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken); if (typeSymbol?.IsErrorType() == false) { string newName = NameGenerator.CreateName(typeSymbol); if (!string.IsNullOrEmpty(newName)) { string oldName = identifier.ValueText; newName = StringUtility.FirstCharToUpper(newName); if (!string.Equals(oldName, newName, StringComparison.Ordinal)) { ISymbol symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken); if (await NameGenerator.IsUniqueMemberNameAsync( newName, symbol, context.Solution, cancellationToken: context.CancellationToken).ConfigureAwait(false)) { context.RegisterRefactoring( $"Rename '{oldName}' to '{newName}'", cancellationToken => Renamer.RenameSymbolAsync(context.Solution, symbol, newName, default(OptionSet), cancellationToken)); } } } } } } } }
internal static TypeSyntax SkipRef(this TypeSyntax syntax, out RefKind refKind) { return(SkipRef(syntax, out refKind, allowScoped: true, diagnostics: null)); }
public static TRoot ReplaceType <TRoot>(this TRoot node, TypeSyntax type, TypeInfo typeInfo) where TRoot : SyntaxNode { TypeSyntax mappedType = type.GetMappedType(typeInfo); return(mappedType.ToString() == type.ToString() ? node : node.ReplaceNode(type, type.GetMappedType(typeInfo).WithTriviaFrom(type))); }
public static LocalDeclarationStatementSyntax LocalDeclarationStatement(TypeSyntax type, string identifier, ExpressionSyntax value = null) { return(LocalDeclarationStatement(type, Identifier(identifier), value)); }
public static TypeArgumentListSyntax TypeArgumentList(TypeSyntax argument) { return(SyntaxFactory.TypeArgumentList(SingletonSeparatedList(argument))); }
public static ObjectCreationExpressionSyntax ObjectCreationExpression(TypeSyntax type, ArgumentListSyntax argumentList) { return(SyntaxFactory.ObjectCreationExpression(type, argumentList, default(InitializerExpressionSyntax))); }
public static GenericNameSyntax GenericName(SyntaxToken identifier, TypeSyntax typeArgument) { return(SyntaxFactory.GenericName(identifier, TypeArgumentList(typeArgument))); }
public static PropertyDeclarationSyntax PropertyDeclaration(PropertyKind kind, SyntaxTokenList modifiers, TypeSyntax type, string name) { return(PropertyDeclaration( kind, default(SyntaxList <AttributeListSyntax>), modifiers, type, default(ExplicitInterfaceSpecifierSyntax), name)); }
public static PropertyDeclarationSyntax PropertyDeclaration(PropertyKind kind, TypeSyntax type, string name) { return(PropertyDeclaration( kind, default(SyntaxTokenList), type, name)); }
protected static ExpressionSyntax GetConstructorCallInitializer(TypeSyntax type) => ObjectCreationExpression(type) .WithArgumentList(ArgumentList());
public ForEachStatementSyntax Update(SyntaxToken forEachKeyword, SyntaxToken openParenToken, TypeSyntax type, SyntaxToken identifier, SyntaxToken inKeyword, ExpressionSyntax expression, SyntaxToken closeParenToken, StatementSyntax statement) { return Update(forEachKeyword, openParenToken, type, identifier, null, inKeyword, expression, closeParenToken, statement); }
public static FieldDeclarationSyntax FieldDeclaration(SyntaxTokenList modifiers, TypeSyntax type, string identifier, ExpressionSyntax value = null) { return(FieldDeclaration( modifiers, type, Identifier(identifier), value)); }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <param name="semanticModel"></param> /// <returns></returns> protected virtual TypeReference CreateTypeReferenceHelper(TypeSyntax node, SemanticModel semanticModel) { return(new TypeReference(node, semanticModel)); }
public static FieldDeclarationSyntax FieldDeclaration(SyntaxTokenList modifiers, TypeSyntax type, SyntaxToken identifier, ExpressionSyntax value = null) { return(SyntaxFactory.FieldDeclaration( default(SyntaxList <AttributeListSyntax>), modifiers, VariableDeclaration( type, identifier, (value != null) ? EqualsValueClause(value) : null))); }
internal static TypeSyntax SkipRef(this TypeSyntax syntax, out RefKind refKind, bool allowScoped, BindingDiagnosticBag?diagnostics) { Debug.Assert(allowScoped || diagnostics is { });
public static VariableDeclarationSyntax VariableDeclaration(TypeSyntax type, string identifier, EqualsValueClauseSyntax initializer) { return(VariableDeclaration(type, Identifier(identifier), initializer)); }
public static TypeSyntax GetMappedType(this TypeSyntax type, TypeInfo typeInfo) { TypeSyntax mappedType = typeInfo.GetMappedType(); return(mappedType.ToString() == type.ToString() ? type : mappedType.WithTriviaFrom(type)); }
public static VariableDeclarationSyntax VariableDeclaration(TypeSyntax type, SyntaxToken identifier, EqualsValueClauseSyntax initializer) { return(VariableDeclaration(type, VariableDeclarator(identifier, initializer))); }
public MethodDeclarationSyntax ToMethodDeclaration() { bool isMain = false; TypeSyntax mainReturnSyntax = null; TypeSyntax returnType = SF.PredefinedType( SF.Token(SyntaxKind.VoidKeyword)); var procedureName = canonicalName.value; if (procedureName == "main") { isMain = true; } if (procedureName != name.name.value || name.library != null) { Console.WriteLine($"Take a look at this! {name.library?.canonicalName?.value}.{name.name.value}"); } if (function.TryGetValue(out var functionNode)) { returnType = functionNode.returnType.ToTypeSyntax(); } var method = SF.MethodDeclaration(returnType, SF.Identifier(procedureName)); var modifiers = new List <SyntaxToken>(); if (flags.HasFlag(Flag.isConst)) { modifiers.Add(SF.Token(SyntaxKind.ConstKeyword)); } if (flags.HasFlag(Flag.isStatic)) { modifiers.Add(SF.Token(SyntaxKind.StaticKeyword)); } // if (isMain) modifiers.Add(SF.Token(SyntaxKind.PublicKeyword)); if (!procedureName.StartsWith('_')) { modifiers.Add(SF.Token(SyntaxKind.PublicKeyword)); } if (flags.HasFlag(Flag.isAbstract)) { throw new NotImplementedException(); } if (flags.HasFlag(Flag.isExternal)) { throw new NotImplementedException(); } if (flags.HasFlag(Flag.isForwardingStub)) { throw new NotImplementedException(); } if (flags.HasFlag(Flag.isForwardingSemiStub)) { throw new NotImplementedException(); } if (flags.HasFlag(Flag.isRedirectingFactoryConstructor)) { throw new NotImplementedException(); } if (flags.HasFlag(Flag.isNoSuchMethodForwarder)) { throw new NotImplementedException(); } if (modifiers.Count == 1) { method = method.WithModifiers(SF.TokenList(modifiers.First())); } else if (modifiers.Count != 0) { method = method.WithModifiers(SF.TokenList(modifiers.ToArray())); } // todo: in our test cases this is a dart Block which goes to BlockSyntax ~ is this always the case? // --> I'm guessing it's not for arrow functions??? if (functionNode.body.TryGetValue(out var body)) { // if 'isMain' we need to rewrite all return blocks var blockSyntax = body.ToStatementSyntax() as BlockSyntax; blockSyntax = SF.Block(blockSyntax.Statements.Add(SF.ReturnStatement(SF.LiteralExpression( SyntaxKind.DefaultLiteralExpression, SF.Token(SyntaxKind.DefaultKeyword))))); method = method.WithBody(blockSyntax); } return(method); }
public static VariableDeclarationSyntax VariableDeclaration(TypeSyntax type, VariableDeclaratorSyntax variable) { return(SyntaxFactory.VariableDeclaration(type, SingletonSeparatedList(variable))); }
private static MethodDeclarationSyntax GenerateMethodFromTemplate(AttributeSyntax attribute, SyntaxToken modifier, TypeSyntax returnType, string identifier, StatementSyntax[] statements) { return(SyntaxFactory.MethodDeclaration(returnType, identifier). AddModifiers(modifier). AddAttributeLists ( SyntaxFactory.AttributeList(SyntaxFactory.AttributeList().Attributes.Add(attribute)) ). AddBodyStatements(statements)); }
public static BinaryExpressionSyntax AsExpression(ExpressionSyntax expression, TypeSyntax type) { return(BinaryExpression(SyntaxKind.AsExpression, expression, type)); }
private static TypeSyntax GetOutermostType(TypeSyntax type) { return(type.GetAncestorsOrThis <TypeSyntax>().Last()); }
public static BinaryExpressionSyntax AsExpression(ExpressionSyntax expression, SyntaxToken operatorToken, TypeSyntax type) { return(BinaryExpression(SyntaxKind.AsExpression, expression, operatorToken, type)); }
private IType BindType(TypeSyntax syntax) { return _contextService.LookupType(syntax.TypeName.Value); }
public static void AnalyzeCastExpression(SyntaxNodeAnalysisContext context) { var castExpression = (CastExpressionSyntax)context.Node; if (castExpression.ContainsDiagnostics) { return; } if (!(castExpression.Parent is ParenthesizedExpressionSyntax parenthesizedExpression)) { return; } ExpressionSyntax accessedExpression = GetAccessedExpression(parenthesizedExpression.Parent); if (accessedExpression == null) { return; } TypeSyntax type = castExpression.Type; if (type == null) { return; } ExpressionSyntax expression = castExpression.Expression; if (expression == null) { return; } SemanticModel semanticModel = context.SemanticModel; CancellationToken cancellationToken = context.CancellationToken; ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, cancellationToken); if (typeSymbol?.IsErrorType() != false) { return; } ITypeSymbol expressionTypeSymbol = semanticModel.GetTypeSymbol(expression, cancellationToken); if (expressionTypeSymbol?.IsErrorType() != false) { return; } if (expressionTypeSymbol.TypeKind == TypeKind.Interface) { return; } if (typeSymbol.TypeKind != TypeKind.Interface && !typeSymbol.EqualsOrInheritsFrom(expressionTypeSymbol, includeInterfaces: true)) { return; } ISymbol accessedSymbol = semanticModel.GetSymbol(accessedExpression, cancellationToken); INamedTypeSymbol containingType = accessedSymbol?.ContainingType; if (containingType == null) { return; } if (typeSymbol.TypeKind == TypeKind.Interface) { if (!CheckExplicitImplementation(expressionTypeSymbol, accessedSymbol)) { return; } } else { if (!CheckAccessibility(expressionTypeSymbol.OriginalDefinition, accessedSymbol, expression.SpanStart, semanticModel, cancellationToken)) { return; } if (!expressionTypeSymbol.EqualsOrInheritsFrom(containingType, includeInterfaces: true)) { return; } } context.ReportDiagnostic( DiagnosticDescriptors.RemoveRedundantCast, Location.Create(castExpression.SyntaxTree, castExpression.ParenthesesSpan())); }
public virtual void Visit(TypeSyntax typeSyntax) { }
internal static RefKind GetRefKind(this TypeSyntax syntax) { syntax.SkipRef(out var refKind); return(refKind); }
public VariableDeclarationSyntax Update(TypeSyntax type, SeparatedSyntaxList<VariableDeclaratorSyntax> variables) { return Update(type, variables, null); }
public static async Task ComputeRefactoringAsync(RefactoringContext context, LocalDeclarationStatementSyntax localDeclaration) { VariableDeclarationSyntax declaration = localDeclaration.Declaration; TypeSyntax type = declaration?.Type; if (type?.IsVar != false) { return; } VariableDeclaratorSyntax declarator = declaration.Variables.FirstOrDefault(); if (declarator == null) { return; } if (context.Span.Start < type.SpanStart) { return; } SyntaxTriviaList triviaList = type.GetTrailingTrivia(); if (!triviaList.Any()) { return; } SyntaxTrivia trivia = triviaList .SkipWhile(f => f.IsWhitespaceTrivia()) .FirstOrDefault(); if (!trivia.IsEndOfLineTrivia()) { return; } if (context.Span.End > trivia.SpanStart) { return; } SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken); string name = NameGenerator.Default.CreateUniqueLocalName( typeSymbol, semanticModel, declarator.SpanStart, cancellationToken: context.CancellationToken); if (name == null) { return; } context.RegisterRefactoring( $"Add identifier '{name}'", c => RefactorAsync(context.Document, type, name, c), RefactoringIdentifiers.AddIdentifierToVariableDeclaration); }