Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used null-coalescing operator for nullable assignments #464

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Cesium.CodeGen/Extensions/TranslationUnitEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,8 @@ private static IEnumerable<IBlockItem> GetTopLevelDeclarations(Ast.SymbolDeclara
int currentValue = -1;
foreach (var enumeratorDeclaration in enumType.Members)
{
var enumeratorName = enumeratorDeclaration.Declaration.Identifier;
if (enumeratorName is null)
{
throw new CompilationException(
var enumeratorName = enumeratorDeclaration.Declaration.Identifier ?? throw new CompilationException(
$"Enum type {enumType.Identifier} has enumerator without name");
}

if (enumeratorDeclaration.Initializer is null)
{
currentValue++;
Expand Down
17 changes: 6 additions & 11 deletions Cesium.CodeGen/Extensions/TypeSystemEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ private static bool Match(
{
var declParamCount = parameters switch
{
{IsVoid: true} => 0,
{IsVarArg: true} => parameters.Parameters.Count + 1,
{ IsVoid: true } => 0,
{ IsVarArg: true } => parameters.Parameters.Count + 1,
_ => parameters.Parameters.Count
};

Expand Down Expand Up @@ -188,8 +188,8 @@ public static IType GetCommonNumericType(this CTypeSystem ts, IType a, IType b)

// Otherwise, if both operands have signed integer types or both have unsigned integer types,
// the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
var signedTypes = new[] {ts.SignedChar, ts.Short, ts.Int, ts.Long, ts.NativeInt};
var unsignedTypes = new[] { ts.Char, ts.UnsignedChar, ts.UnsignedShort, ts.UnsignedInt, ts.UnsignedLong, ts.NativeUInt};
var signedTypes = new[] { ts.SignedChar, ts.Short, ts.Int, ts.Long, ts.NativeInt };
var unsignedTypes = new[] { ts.Char, ts.UnsignedChar, ts.UnsignedShort, ts.UnsignedInt, ts.UnsignedLong, ts.NativeUInt };
// TODO[#381]: Move NativeInt and NativeUInt accordingly or consider them properly based on the current architecture.

var aSignedRank = RankOf(a, signedTypes);
Expand Down Expand Up @@ -227,7 +227,7 @@ public static IType GetCommonNumericType(this CTypeSystem ts, IType a, IType b)

int? RankOf(IType t, IType[] family)
{
for(var i = 0; i < family.Length; i++)
for (var i = 0; i < family.Length; i++)
if (t.IsEqualTo(family[i]))
return i;
return null;
Expand All @@ -243,12 +243,7 @@ public static TypeDefinition GetRuntimeHelperType(this TranslationUnitContext co
public static MethodReference GetRuntimeHelperMethod(this TranslationUnitContext context, string helperMethod)
{
var runtimeHelpersType = context.GetRuntimeHelperType();
var method = runtimeHelpersType.FindMethod(helperMethod);
if (method == null)
{
throw new AssertException($"RuntimeHelper {helperMethod} cannot be found.");
}

var method = runtimeHelpersType.FindMethod(helperMethod) ?? throw new AssertException($"RuntimeHelper {helperMethod} cannot be found.");
return context.Module.ImportReference(method);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ public void EmitTo(IEmitScope scope)
WriteInitializer(stream, i);
}

var targetSize = ((InPlaceArrayType)_type).GetSizeInBytes(scope.AssemblyContext.ArchitectureSet);

if (targetSize is null)
throw new NotImplementedException("Cannot calculate size of target array");

int targetSize = ((InPlaceArrayType)_type).GetSizeInBytes(scope.AssemblyContext.ArchitectureSet) ?? throw new NotImplementedException("Cannot calculate size of target array");
if (stream.Position < targetSize)
{
stream.Write(new byte[targetSize.Value - (int)stream.Position]);
stream.Write(new byte[targetSize - (int)stream.Position]);
}

var constantData = stream.ToArray();
Expand Down
7 changes: 1 addition & 6 deletions Cesium.CodeGen/Ir/Expressions/FunctionCallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ public override IExpression Lower(IDeclarationScope scope)
}).ToList());
}

var callee = scope.GetFunctionInfo(functionName);
if (callee is null)
{
throw new CompilationException($"Function \"{functionName}\" was not found.");
}

var callee = scope.GetFunctionInfo(functionName) ?? throw new CompilationException($"Function \"{functionName}\" was not found.");
int firstVarArgArgument = 0;
if (callee.Parameters?.IsVarArg == true)
{
Expand Down
7 changes: 1 addition & 6 deletions Cesium.CodeGen/Ir/Expressions/Values/LValueArrayElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public IType GetValueType()

private PrimitiveType GetElementType()
{
InPlaceArrayType? type = GetValueType() as InPlaceArrayType;
if (type == null)
{
throw new AssertException("Array type expected.");
}

InPlaceArrayType? type = GetValueType() as InPlaceArrayType ?? throw new AssertException("Array type expected.");
var primitiveType = (PrimitiveType)GetBaseType(type);
return primitiveType;
}
Expand Down
Loading
Loading