blob: c960d1e70d5fbbd49892c03f55bef30665ca9d02 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
using Microsoft.CodeAnalysis;
namespace Lucene.Net.CodeAnalysis.Dev.Utility
{
internal static class FloatingPoint
{
public static bool IsFloatingPointType(SymbolInfo symbolInfo)
{
if (symbolInfo.Symbol is Microsoft.CodeAnalysis.IFieldSymbol fieldInfo)
return IsSpecialTypeFloatingPoint(fieldInfo.Type.SpecialType);
if (symbolInfo.Symbol is Microsoft.CodeAnalysis.ILocalSymbol localInfo)
return IsSpecialTypeFloatingPoint(localInfo.Type.SpecialType);
if (symbolInfo.Symbol is Microsoft.CodeAnalysis.IPropertySymbol propertyInfo)
return IsSpecialTypeFloatingPoint(propertyInfo.Type.SpecialType);
if (symbolInfo.Symbol is Microsoft.CodeAnalysis.IParameterSymbol parameterInfo)
return IsSpecialTypeFloatingPoint(parameterInfo.Type.SpecialType);
//if (symbolInfo.Symbol is Microsoft.CodeAnalysis.ITypeParameterSymbol typeParameterInfo)
// return IsSpecialTypeFloatingPoint(typeParameterInfo.Type.SpecialType);
//if (symbolInfo.Symbol.Name == "Equals")
return false;
}
public static bool IsFloatingPointType(TypeInfo typeInfo)
{
if (IsFloatingPointType(typeInfo.Type))
return true;
if (IsFloatingPointType(typeInfo.ConvertedType))
return true;
return false;
}
public static bool IsFloatingPointType(ITypeSymbol? typeSymbol)
{
if (typeSymbol is null)
return false;
return IsSpecialTypeFloatingPoint(typeSymbol.SpecialType);
}
public static bool TryGetFloatingPointTypeName(TypeInfo typeInfo, out string typeName)
{
if (TryGetFloatingPointTypeName(typeInfo.Type, out typeName))
return true;
if (TryGetFloatingPointTypeName(typeInfo.ConvertedType, out typeName))
return true;
typeName = null!;
return false;
}
public static bool TryGetFloatingPointTypeName(ITypeSymbol? typeSymbol, out string typeName)
{
typeName = typeSymbol?.SpecialType switch
{
SpecialType.System_Single => "Single",
SpecialType.System_Double => "Double",
_ => null!
};
return typeName is not null;
}
private static bool IsSpecialTypeFloatingPoint(SpecialType specialType)
{
return specialType == SpecialType.System_Single || specialType == SpecialType.System_Double;
}
}
}