public override TypeInfo GetLoadType(ILGeneratorContext context) { if (IsConst(context.Module)) { // see if we can just fold this into a const object obj = VisitConst(context.Module); if (obj != null) { return(TypeUtility.GetConstType(obj, context.Context)); } } TypeInfo lhs = LHS.GetLoadType(context); TypeInfo rhs = RHS.GetLoadType(context); if (lhs is IntegerTypeInfo lhs_i && rhs is IntegerTypeInfo rhs_i) { IntegerTypeInfo normalized = TypeUtility.NormalizeInts(context, lhs_i, rhs_i); return(getOpType(context, normalized)); }
public static IntegerTypeInfo NormalizeInts(ILGeneratorContext context, IntegerTypeInfo lhs, IntegerTypeInfo rhs) { IntegerWidth width = lhs.Width > rhs.Width ? lhs.Width : rhs.Width; bool signed = lhs.Signed | rhs.Signed; switch (width) { case IntegerWidth.I8: return((IntegerTypeInfo)(signed ? context.Context.GlobalTypes.GetType("sbyte") : context.Context.GlobalTypes.GetType("byte"))); case IntegerWidth.I16: return((IntegerTypeInfo)(signed ? context.Context.GlobalTypes.GetType("short") : context.Context.GlobalTypes.GetType("ushort"))); case IntegerWidth.I32: return((IntegerTypeInfo)(signed ? context.Context.GlobalTypes.GetType("int") : context.Context.GlobalTypes.GetType("uint"))); case IntegerWidth.I64: return((IntegerTypeInfo)(signed ? context.Context.GlobalTypes.GetType("long") : context.Context.GlobalTypes.GetType("ulong"))); } throw new System.InvalidOperationException(); }
public ParseStatus ParseTypeSymbol( ITypeSymbol typeSymbol, bool isReadonly, out ITypeInfo typeInfo) { this.ParseNullable_(ref typeSymbol, out var isNullable); var primitiveType = SchemaPrimitiveTypesUtil.GetPrimitiveTypeFromTypeSymbol( typeSymbol); if (primitiveType != SchemaPrimitiveType.UNDEFINED) { switch (primitiveType) { case SchemaPrimitiveType.BOOLEAN: { typeInfo = new BoolTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.BYTE: case SchemaPrimitiveType.SBYTE: case SchemaPrimitiveType.INT16: case SchemaPrimitiveType.UINT16: case SchemaPrimitiveType.INT32: case SchemaPrimitiveType.UINT32: case SchemaPrimitiveType.INT64: case SchemaPrimitiveType.UINT64: { typeInfo = new IntegerTypeInfo( typeSymbol, SchemaTypeKind.INTEGER, SchemaPrimitiveTypesUtil.ConvertNumberToInt( SchemaPrimitiveTypesUtil .ConvertPrimitiveToNumber(primitiveType)), isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.SN8: case SchemaPrimitiveType.UN8: case SchemaPrimitiveType.SN16: case SchemaPrimitiveType.UN16: case SchemaPrimitiveType.SINGLE: case SchemaPrimitiveType.DOUBLE: { typeInfo = new FloatTypeInfo( typeSymbol, SchemaTypeKind.FLOAT, SchemaPrimitiveTypesUtil .ConvertPrimitiveToNumber(primitiveType), isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.CHAR: { typeInfo = new CharTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.ENUM: { typeInfo = new EnumTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } default: throw new ArgumentOutOfRangeException(); } } if (typeSymbol.SpecialType == SpecialType.System_String) { typeInfo = new StringTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } if (typeSymbol.SpecialType is SpecialType .System_Collections_Generic_IReadOnlyList_T) { var listTypeSymbol = typeSymbol as INamedTypeSymbol; var containedTypeSymbol = listTypeSymbol.TypeArguments[0]; var containedParseStatus = this.ParseTypeSymbol( containedTypeSymbol, true, out var containedTypeInfo); if (containedParseStatus != ParseStatus.SUCCESS) { typeInfo = default; return(containedParseStatus); } typeInfo = new SequenceTypeInfo( typeSymbol, isReadonly, isNullable, false, isReadonly, containedTypeInfo); return(ParseStatus.SUCCESS); } if (typeSymbol.TypeKind is TypeKind.Array) { var arrayTypeSymbol = typeSymbol as IArrayTypeSymbol; var containedTypeSymbol = arrayTypeSymbol.ElementType; var containedParseStatus = this.ParseTypeSymbol( containedTypeSymbol, false, out var containedTypeInfo); if (containedParseStatus != ParseStatus.SUCCESS) { typeInfo = default; return(containedParseStatus); } typeInfo = new SequenceTypeInfo( typeSymbol, isReadonly, isNullable, true, isReadonly, containedTypeInfo); return(ParseStatus.SUCCESS); } if (typeSymbol.SpecialType is SpecialType .System_Collections_Generic_IList_T) { var listTypeSymbol = typeSymbol as INamedTypeSymbol; var containedTypeSymbol = listTypeSymbol.TypeArguments[0]; var containedParseStatus = this.ParseTypeSymbol( containedTypeSymbol, false, out var containedTypeInfo); if (containedParseStatus != ParseStatus.SUCCESS) { typeInfo = default; return(containedParseStatus); } typeInfo = new SequenceTypeInfo( typeSymbol, isReadonly, isNullable, false, false, containedTypeInfo); return(ParseStatus.SUCCESS); } if (typeSymbol is INamedTypeSymbol namedTypeSymbol) { typeInfo = new StructureTypeInfo( namedTypeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } if (typeSymbol is ITypeParameterSymbol typeParameterSymbol) { var constraintTypeInfos = typeParameterSymbol .ConstraintTypes .Select(constraintType => { var parseStatus = this.ParseTypeSymbol( constraintType, isReadonly, out var constraintTypeInfo); Asserts.Equal(ParseStatus.SUCCESS, parseStatus); return(constraintTypeInfo); }) .ToArray(); typeInfo = new GenericTypeInfo( constraintTypeInfos, typeParameterSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } typeInfo = default; return(ParseStatus.NOT_IMPLEMENTED); }