From c96f909ac7dc2ca0d3dcb363a489c935276971a3 Mon Sep 17 00:00:00 2001 From: yanghuan Date: Wed, 18 Dec 2019 18:25:23 +0800 Subject: [PATCH] update roslyn & fix warnning --- CSharp.lua/CSharp.lua.csproj | 2 +- CSharp.lua/LuaSyntaxGenerator.cs | 22 ++++++------ CSharp.lua/LuaSyntaxNodeTransform.Helper.cs | 31 +++++++--------- CSharp.lua/LuaSyntaxNodeTransform.Object.cs | 6 ++-- CSharp.lua/LuaSyntaxNodeTransform.cs | 15 ++++---- CSharp.lua/Utils.cs | 40 +++++++++------------ 6 files changed, 50 insertions(+), 66 deletions(-) diff --git a/CSharp.lua/CSharp.lua.csproj b/CSharp.lua/CSharp.lua.csproj index 99902605..a3c6253b 100644 --- a/CSharp.lua/CSharp.lua.csproj +++ b/CSharp.lua/CSharp.lua.csproj @@ -14,7 +14,7 @@ - + diff --git a/CSharp.lua/LuaSyntaxGenerator.cs b/CSharp.lua/LuaSyntaxGenerator.cs index 7e80d70e..bb4495e8 100644 --- a/CSharp.lua/LuaSyntaxGenerator.cs +++ b/CSharp.lua/LuaSyntaxGenerator.cs @@ -122,7 +122,7 @@ public int Indent { private readonly Dictionary> partialTypes_ = new Dictionary>(); private readonly HashSet monoBehaviourSpecialMethodNames_; private IMethodSymbol mainEntryPoint_; - private List assemblyAttributes_ = new List(); + private readonly List assemblyAttributes_ = new List(); public INamedTypeSymbol SystemExceptionTypeSymbol { get; } private readonly INamedTypeSymbol monoBehaviourTypeSymbol_; @@ -690,13 +690,13 @@ private LuaIdentifierNameSyntax InternalGetMemberName(ISymbol symbol) { } private static bool IsSameNameSymbol(ISymbol member, ISymbol symbol) { - if (member.Equals(symbol)) { + if (member.EQ(symbol)) { return true; } if (symbol.Kind == SymbolKind.Method) { var methodSymbol = (IMethodSymbol)symbol; - if (methodSymbol.PartialDefinitionPart != null && methodSymbol.PartialDefinitionPart.Equals(member)) { + if (methodSymbol.PartialDefinitionPart != null && methodSymbol.PartialDefinitionPart.EQ(member)) { return true; } } @@ -782,7 +782,7 @@ private LuaIdentifierNameSyntax GetStaticClassMemberName(ISymbol symbol) { int index = 0; foreach (ISymbol member in sameNameMembers) { LuaIdentifierNameSyntax identifierName = GetMethodNameFromIndex(symbol, index); - if (member.Equals(symbol)) { + if (member.EQ(symbol)) { symbolExpression = identifierName; } else { if (!memberNames_.ContainsKey(member)) { @@ -843,7 +843,7 @@ private List GetSameNameMembers(ISymbol symbol) { var rootType = symbol.ContainingType; var curTypeSymbol = rootType; while (true) { - AddSimilarNameMembers(curTypeSymbol, names, members, !rootType.Equals(curTypeSymbol)); + AddSimilarNameMembers(curTypeSymbol, names, members, !rootType.EQ(curTypeSymbol)); var baseTypeSymbol = curTypeSymbol.BaseType; if (baseTypeSymbol != null) { curTypeSymbol = baseTypeSymbol; @@ -950,7 +950,7 @@ private int MemberSymbolComparison(ISymbol a, ISymbol b) { if (countOfA == 1) { var implementationOfA = a.InterfaceImplementations().First(); var implementationOfB = b.InterfaceImplementations().First(); - if (implementationOfA.Equals(implementationOfB)) { + if (implementationOfA.EQ(implementationOfB)) { throw new CompilationErrorException($"{a} is conflict with {b}"); } @@ -976,7 +976,7 @@ private int MemberSymbolComparison(ISymbol a, ISymbol b) { } private int MemberSymbolCommonComparison(ISymbol a, ISymbol b) { - if (a.ContainingType.Equals(b.ContainingType)) { + if (a.ContainingType.EQ(b.ContainingType)) { var type = a.ContainingType; var names = GetSymbolNames(a); List members = new List(); @@ -1306,7 +1306,7 @@ private void CheckImplicitInterfaceImplementation(INamedTypeSymbol type) { } var implementationType = implementationMember.ContainingType; - if (!implementationType.Equals(type)) { + if (!implementationType.EQ(type)) { if (!implementationType.AllInterfaces.Contains(baseInterface)) { generator_.AddImplicitInterfaceImplementation(implementationMember, interfaceMember); generator_.TryAddExtend(baseInterface, implementationType); @@ -1372,7 +1372,7 @@ private string GetTypeOrNamespaceNewName(IEnumerable allSymbols, ISymbo } private static bool CheckTypeNameExists(IEnumerable all, ISymbol type, string newName) { - return all.Where(i => i.ContainingNamespace.Equals(type.ContainingNamespace)).Any(i => i.Name == newName); + return all.Where(i => i.ContainingNamespace.EQ(type.ContainingNamespace)).Any(i => i.Name == newName); } private void CheckNamespace() { @@ -1599,12 +1599,12 @@ public bool IsMoreThanLocalVariables(ISymbol symbol) { int index = 0; switch (symbol.Kind) { case SymbolKind.Method: { - index = methods.FindIndex(i => i.Equals(symbol)); + index = methods.FindIndex(i => i.EQ(symbol)); break; } case SymbolKind.Property: case SymbolKind.Event: { - index = methods.FindIndex(i => i.Kind == SymbolKind.Method && symbol.Equals(((IMethodSymbol)i).AssociatedSymbol)) + 1; + index = methods.FindIndex(i => i.Kind == SymbolKind.Method && symbol.EQ(((IMethodSymbol)i).AssociatedSymbol)) + 1; break; } } diff --git a/CSharp.lua/LuaSyntaxNodeTransform.Helper.cs b/CSharp.lua/LuaSyntaxNodeTransform.Helper.cs index 0c39addd..4d868f36 100644 --- a/CSharp.lua/LuaSyntaxNodeTransform.Helper.cs +++ b/CSharp.lua/LuaSyntaxNodeTransform.Helper.cs @@ -873,7 +873,7 @@ internal bool AddGenericImport(LuaInvocationExpressionSyntax invocationExpressio } internal void ImportGenericTypeName(ref LuaExpressionSyntax luaExpression, ITypeSymbol symbol) { - if (!IsNoImportTypeName && !CurTypeSymbol.Equals(symbol) && !IsCurMethodTypeArgument(symbol)) { + if (!IsNoImportTypeName && !CurTypeSymbol.EQ(symbol) && !IsCurMethodTypeArgument(symbol)) { var invocationExpression = (LuaInvocationExpressionSyntax)luaExpression; string newName = GetGenericTypeImportName(invocationExpression, out var argumentTypeNames); if (!IsLocalVarExistsInCurMethod(newName)) { @@ -1094,8 +1094,7 @@ private void CheckValueTypeClone(ITypeSymbol typeSymbol, IdentifierNameSyntax no } } - var symbol = semanticModel_.GetSymbolInfo(argument.Parent.Parent).Symbol as IMethodSymbol; - if (symbol != null) { + if (semanticModel_.GetSymbolInfo(argument.Parent.Parent).Symbol is IMethodSymbol symbol) { if (symbol.IsFromAssembly() && !symbol.ContainingType.IsCollectionType()) { break; } @@ -1266,7 +1265,7 @@ private void CheckConversion(ExpressionSyntax node, ref LuaExpressionSyntax expr private LuaExpressionSyntax GetOperatorMemberAccessExpression(IMethodSymbol methodSymbol) { var methodName = GetMemberName(methodSymbol); - if (CurTypeSymbol.Equals(methodSymbol.ContainingType)) { + if (CurTypeSymbol.EQ(methodSymbol.ContainingType)) { return methodName; } @@ -1392,7 +1391,7 @@ public SymbolAssignmentSearcher(LuaSyntaxGenerator generator, ISymbol symbol) { public override void VisitAssignmentExpression(AssignmentExpressionSyntax node) { var semanticModel = generator_.GetSemanticModel(node.SyntaxTree); var symbol = semanticModel.GetSymbolInfo(node.Left).Symbol; - if (symbol_.Equals(symbol)) { + if (symbol_.EQ(symbol)) { Found(); } @@ -1411,7 +1410,7 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node) foreach (var argument in node.ArgumentList.Arguments) { if (argument.RefKindKeyword.IsOutOrRef()) { var symbol = semanticModel.GetSymbolInfo(argument.Expression).Symbol; - if (symbol.Equals(symbol_)) { + if (symbol.EQ(symbol_)) { Found(); } } @@ -1751,7 +1750,7 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node) var methodSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(node).Symbol; if (methodSymbol != null) { - if (methodSymbol.Equals(symbol_)) { + if (methodSymbol.EQ(symbol_)) { Found(); } } @@ -1776,8 +1775,7 @@ private bool InliningInvocationExpression(SyntaxNode root, IMethodSymbol symbol, BlockSyntax bodyNode; ArrowExpressionClauseSyntax expressionBodyNode; if (symbol.MethodKind == MethodKind.PropertyGet) { - var propertyDeclaration = symbol.AssociatedSymbol.GetDeclaringSyntaxNode() as PropertyDeclarationSyntax; - if (propertyDeclaration == null) { + if (!(symbol.AssociatedSymbol.GetDeclaringSyntaxNode() is PropertyDeclarationSyntax propertyDeclaration)) { goto Fail; } @@ -1787,8 +1785,7 @@ private bool InliningInvocationExpression(SyntaxNode root, IMethodSymbol symbol, expressionBodyNode = accessor.ExpressionBody; parameterList = null; } else { - var methodDeclaration = symbol.GetDeclaringSyntaxNode() as MethodDeclarationSyntax; - if (methodDeclaration == null) { + if (!(symbol.GetDeclaringSyntaxNode() is MethodDeclarationSyntax methodDeclaration)) { goto Fail; } @@ -1945,13 +1942,11 @@ private LuaExpressionSyntax CompressionInliningBlock(SyntaxNode root, LuaBlockSt } } - var expressionStatement = block.Statements.Last() as LuaExpressionStatementSyntax; - if (expressionStatement == null) { + if (!(block.Statements.Last() is LuaExpressionStatementSyntax expressionStatement)) { return null; } - var assignment = expressionStatement.Expression as LuaAssignmentExpressionSyntax; - if (assignment == null) { + if (!(expressionStatement.Expression is LuaAssignmentExpressionSyntax assignment)) { return null; } @@ -2038,8 +2033,7 @@ private bool IsWantInline(IPropertySymbol symbol) { return false; } - var propertyDeclaration = symbol.GetDeclaringSyntaxNode() as PropertyDeclarationSyntax; - if (propertyDeclaration == null || propertyDeclaration.AccessorList == null) { + if (!(symbol.GetDeclaringSyntaxNode() is PropertyDeclarationSyntax propertyDeclaration) || propertyDeclaration.AccessorList == null) { return false; } @@ -2050,8 +2044,7 @@ private bool IsWantInline(IPropertySymbol symbol) { return false; } - var returnStatement = accessor.Body.Statements.First() as ReturnStatementSyntax; - if (returnStatement == null) { + if (!(accessor.Body.Statements.First() is ReturnStatementSyntax returnStatement)) { return false; } diff --git a/CSharp.lua/LuaSyntaxNodeTransform.Object.cs b/CSharp.lua/LuaSyntaxNodeTransform.Object.cs index 0f8cf831..d986b1e6 100644 --- a/CSharp.lua/LuaSyntaxNodeTransform.Object.cs +++ b/CSharp.lua/LuaSyntaxNodeTransform.Object.cs @@ -560,7 +560,7 @@ private LuaTryAdapterExpressionSyntax VisitTryCatchesExpress(SyntaxList(this); var typeSymbol = semanticModel_.GetTypeInfo(catchNode.Declaration.Type).Type; - if (!typeSymbol.Equals(generator_.SystemExceptionTypeSymbol)) { + if (!typeSymbol.EQ(generator_.SystemExceptionTypeSymbol)) { var mathcTypeInvocation = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Is, temp, typeName); if (ifCondition != null) { ifCondition = ifCondition.And(mathcTypeInvocation); @@ -781,7 +781,7 @@ private BaseVisitType CheckBaseVisitType(T symbol, Func overridde if (generator_.IsSealed(curTypeSymbol)) { bool exists = curTypeSymbol.GetMembers().OfType().Any(i => { var overriddenSymbol = overriddenFunc(i); - return overriddenSymbol != null && overriddenSymbol.OriginalDefinition.Equals(symbol.OriginalDefinition); + return overriddenSymbol != null && overriddenSymbol.OriginalDefinition.EQ(symbol.OriginalDefinition); }); return exists ? BaseVisitType.UseBase : BaseVisitType.UseThis; } else { @@ -1191,7 +1191,7 @@ public override LuaSyntaxNode VisitArrowExpressionClause(ArrowExpressionClauseSy } public override LuaSyntaxNode VisitLocalFunctionStatement(LocalFunctionStatementSyntax node) { - var result = BuildMethodDeclaration(node, default, node.ParameterList, node.TypeParameterList, node.Body, node.ExpressionBody, node.ReturnType); + var result = BuildMethodDeclaration(node, default, node.ParameterList, node.TypeParameterList, node.Body, node.ExpressionBody); if (node.Modifiers.IsStatic() && IsStaticLocalMethodEnableAddToType(result.Symbol)) { CurTypeDeclaration.TypeDeclaration.AddMethod(result.Name, result.Function, true, result.Document); return LuaStatementSyntax.Empty; diff --git a/CSharp.lua/LuaSyntaxNodeTransform.cs b/CSharp.lua/LuaSyntaxNodeTransform.cs index 3b4235cd..b4eb9d4e 100644 --- a/CSharp.lua/LuaSyntaxNodeTransform.cs +++ b/CSharp.lua/LuaSyntaxNodeTransform.cs @@ -58,7 +58,7 @@ public TypeDeclarationInfo(INamedTypeSymbol typeSymbol, LuaTypeDeclarationSyntax } public bool CheckTypeName(INamedTypeSymbol getNameTypeSymbol, out LuaIdentifierNameSyntax name) { - if (getNameTypeSymbol.Equals(TypeSymbol)) { + if (getNameTypeSymbol.EQ(TypeSymbol)) { TypeDeclaration.IsClassUsed = true; name = LuaIdentifierNameSyntax.Class; return true; @@ -624,8 +624,7 @@ private MethodDeclarationResult BuildMethodDeclaration( ParameterListSyntax parameterList, TypeParameterListSyntax typeParameterList, BlockSyntax body, - ArrowExpressionClauseSyntax expressionBody, - TypeSyntax returnType) { + ArrowExpressionClauseSyntax expressionBody) { IMethodSymbol symbol = (IMethodSymbol)semanticModel_.GetDeclaredSymbol(node); var refOrOutParameters = new List(); MethodInfo methodInfo = new MethodInfo(symbol, refOrOutParameters); @@ -720,7 +719,7 @@ private bool IsCurTypeSerializable { public override LuaSyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) { if ((node.Body != null || node.ExpressionBody != null) && !node.HasCSharpLuaAttribute(LuaDocumentStatement.AttributeFlags.Ignore)) { - var result = BuildMethodDeclaration(node, node.AttributeLists, node.ParameterList, node.TypeParameterList, node.Body, node.ExpressionBody, node.ReturnType); + var result = BuildMethodDeclaration(node, node.AttributeLists, node.ParameterList, node.TypeParameterList, node.Body, node.ExpressionBody); bool isMoreThanLocalVariables = IsMoreThanLocalVariables(result.Symbol); CurType.AddMethod(result.Name, result.Function, result.IsPrivate, result.Document, isMoreThanLocalVariables, result.Symbol.IsInterfaceDefaultMethod()); if (IsCurTypeExportMetadataAll || result.Attributes.Count > 0 || result.IsMetadata) { @@ -1278,7 +1277,7 @@ public LuaSyntaxNode Visit(LuaSyntaxNodeTransform transfor) { return new LuaShortCommentStatement(commentContent); } case SyntaxKind.MultiLineCommentTrivia: { - string commentContent = content.Substring(kCommentCharCount, content.Length - kCommentCharCount - kCommentCharCount); + string commentContent = content[kCommentCharCount..^kCommentCharCount]; commentContent = commentContent.ReplaceNewline(); if (CheckInsertLuaCodeTemplate(commentContent, out var codeStatement)) { return codeStatement; @@ -2864,7 +2863,7 @@ private void CheckDelegateBind(IMethodSymbol symbol, CSharpSyntaxNode node, ref const int kReturnParameterIndex = int.MaxValue; if (symbol.IsGenericMethod) { var originalDefinition = symbol.OriginalDefinition; - if (!originalDefinition.Equals(symbol)) { + if (!originalDefinition.EQ(symbol)) { var targetMethodSymbol = GetDelegateTargetMethodSymbol(node); var targetTypeParameters = new List(); foreach (var typeArgument in targetMethodSymbol.ContainingType.TypeArguments) { @@ -4339,7 +4338,7 @@ private void ChecktIncrementExpression(ExpressionSyntax operand, ref LuaExpressi var symbol = semanticModel_.GetTypeInfo(operand).Type; if (!symbol.IsNumberType()) { var op_Implicits = symbol.GetMembers("op_Implicit").OfType(); - var methodSymbol = op_Implicits.FirstOrDefault(i => isAddOrAssignment ? i.ReturnType.IsIntegerType() : i.ReturnType.Equals(symbol)); + var methodSymbol = op_Implicits.FirstOrDefault(i => isAddOrAssignment ? i.ReturnType.IsIntegerType() : i.ReturnType.EQ(symbol)); if (methodSymbol != null) { expression = BuildConversionExpression(methodSymbol, expression); } @@ -4615,7 +4614,7 @@ private void CheckForeachCast(LuaIdentifierNameSyntax identifier, ForEachStateme bool hasCast = false; var elementType = !isAsync ? sourceType.GetIEnumerableElementType() : sourceType.GetIAsyncEnumerableElementType(); if (elementType != null) { - if (!elementType.Equals(targetType) && !elementType.Is(targetType)) { + if (!elementType.EQ(targetType) && !elementType.Is(targetType)) { hasCast = true; } } else { diff --git a/CSharp.lua/Utils.cs b/CSharp.lua/Utils.cs index 38b7aa4b..bdad5292 100644 --- a/CSharp.lua/Utils.cs +++ b/CSharp.lua/Utils.cs @@ -62,18 +62,6 @@ public BugErrorException(SyntaxNode node, Exception e) } } - public sealed class ArgumentNullException : System.ArgumentNullException { - public ArgumentNullException(string paramName) : base(paramName) { - Contract.Assert(false); - } - } - - public sealed class InvalidOperationException : System.InvalidOperationException { - public InvalidOperationException() { - Contract.Assert(false); - } - } - public enum PropertyMethodKind { Field = 0, Both = 1, @@ -304,6 +292,10 @@ public static bool IsOutOrRef(this SyntaxToken token) { return token.IsKind(SyntaxKind.OutKeyword) || token.IsKind(SyntaxKind.RefKeyword); } + public static bool EQ(this ISymbol a, ISymbol b) { + return SymbolEqualityComparer.Default.Equals(a, b); + } + public static bool IsBasicType(this ITypeSymbol type) { return type.SpecialType >= SpecialType.System_Enum && type.SpecialType <= SpecialType.System_Double; } @@ -474,7 +466,7 @@ public static bool IsOverridden(this ISymbol symbol, ISymbol superSymbol) { ISymbol overriddenSymbol = symbol.OverriddenSymbol(); if (overriddenSymbol != null) { CheckOriginalDefinition(ref overriddenSymbol); - if (overriddenSymbol.Equals(superSymbol)) { + if (overriddenSymbol.EQ(superSymbol)) { return true; } symbol = overriddenSymbol; @@ -576,12 +568,12 @@ public static bool IsSubclassOf(this ITypeSymbol child, ITypeSymbol parent) { } ITypeSymbol p = child; - if (p.Equals(parent)) { + if (p.EQ(parent)) { return false; } while (p != null) { - if (p.Equals(parent)) { + if (p.EQ(parent)) { return true; } p = p.BaseType; @@ -596,7 +588,7 @@ private static bool IsImplementInterface(this ITypeSymbol implementType, ITypeSy while (t != null) { var interfaces = implementType.AllInterfaces; foreach (var i in interfaces) { - if (i.Equals(interfaceType) || i.IsImplementInterface(interfaceType)) { + if (i.EQ(interfaceType) || i.IsImplementInterface(interfaceType)) { return true; } } @@ -611,7 +603,7 @@ private static bool IsBaseNumberType(this SpecialType specialType) { public static bool IsNumberTypeAssignableFrom(this ITypeSymbol left, ITypeSymbol right) { if (left.SpecialType.IsBaseNumberType() && right.SpecialType.IsBaseNumberType()) { - if (left.Equals(right)) { + if (left.EQ(right)) { return true; } @@ -651,7 +643,7 @@ public static bool IsNumberTypeAssignableFrom(this ITypeSymbol left, ITypeSymbol } public static bool Is(this ITypeSymbol left, ITypeSymbol right) { - if (left.Equals(right)) { + if (left.EQ(right)) { return true; } @@ -675,7 +667,7 @@ private static void CheckSymbolDefinition(ref T symbol) where T : class, ISym public static void CheckMethodDefinition(ref IMethodSymbol symbol) { if (symbol.IsExtensionMethod) { - if (symbol.ReducedFrom != null && !symbol.ReducedFrom.Equals(symbol)) { + if (symbol.ReducedFrom != null && !symbol.ReducedFrom.EQ(symbol)) { symbol = symbol.ReducedFrom; } else { CheckSymbolDefinition(ref symbol); @@ -689,7 +681,7 @@ public static void CheckOriginalDefinition(ref ISymbol symbol) { if (symbol.Kind == SymbolKind.Method) { IMethodSymbol methodSymbol = (IMethodSymbol)symbol; CheckMethodDefinition(ref methodSymbol); - if (!methodSymbol.Equals(symbol)) { + if (!methodSymbol.EQ(symbol)) { symbol = methodSymbol; } } else { @@ -720,7 +712,7 @@ public static bool IsExtendSelf(INamedTypeSymbol typeSymbol, INamedTypeSymbol ba if (baseTypeSymbol.IsGenericType) { foreach (var baseTypeArgument in baseTypeSymbol.TypeArguments) { if (baseTypeSymbol.Kind != SymbolKind.TypeParameter) { - if (!baseTypeArgument.Equals(typeSymbol) && baseTypeArgument.Is(typeSymbol)) { + if (!baseTypeArgument.EQ(typeSymbol) && baseTypeArgument.Is(typeSymbol)) { return true; } } @@ -1013,7 +1005,7 @@ public static bool IsTypeParameterExists(this ITypeSymbol symbol, ITypeSymbol ma break; } case SymbolKind.TypeParameter: { - return matchType == null || symbol.Equals(matchType); + return matchType == null || symbol.EQ(matchType); } case SymbolKind.PointerType: { var pointType = (IPointerTypeSymbol)symbol; @@ -1072,7 +1064,7 @@ public static bool IsMemberExists(this ITypeSymbol symbol, string memberName, bo } public static bool IsContainsInternalSymbol(this INamedTypeSymbol type, ISymbol symbol) { - if (type.Equals(symbol.ContainingType)) { + if (type.EQ(symbol.ContainingType)) { return true; } @@ -1162,7 +1154,7 @@ public static bool IsCombineImplicitlyCtorMethod(this IMethodSymbol symbol, out int index = ctor.FindNotNullParameterIndex(); if (index != -1) { notNullParameterIndex = index; - return symbol.Equals(ctor); + return symbol.EQ(ctor); } } }