/// <summary> /// Creates a generated expression for a default argument value. /// </summary> public static Expression?CreateGenerated(Context cx, IParameterSymbol parameter, IExpressionParentEntity parent, int childIndex, Extraction.Entities.Location location) { if (!parameter.HasExplicitDefaultValue) { return(null); } var defaultValue = parameter.ExplicitDefaultValue; if (parameter.Type is INamedTypeSymbol nt && nt.EnumUnderlyingType is not null) { // = (MyEnum)1, = MyEnum.Value1, = default(MyEnum), = new MyEnum() // we're generating a (MyEnum)value cast expression: defaultValue ??= 0; Action <Expression, int> createChild = (parent, index) => Literal.CreateGenerated(cx, parent, index, nt.EnumUnderlyingType, defaultValue, location); return(Cast.CreateGenerated(cx, parent, childIndex, parameter.Type, defaultValue, createChild, location)); } if (defaultValue is null) { // = null, = default, = default(T), = new MyStruct() // we're generating a default expression: return(Default.CreateGenerated(cx, parent, childIndex, location, parameter.Type.IsReferenceType ? ValueAsString(null) : null)); } // const literal: return(Literal.CreateGenerated(cx, parent, childIndex, parameter.Type, defaultValue, location)); }
LocalVariable(Context cx, ISymbol init, Expression parent, bool isVar, Extraction.Entities.Location declLocation) : base(cx, init) { Parent = parent; IsVar = isVar; DeclLocation = declLocation; }
internal Expression(IExpressionInfo info) : base(info.Context) { Location = info.Location; Kind = info.Kind; Type = info.Type; if (Type.Type is null) { Type = NullType.Create(cx); } cx.Emit(Tuples.expressions(this, Kind, Type.Type.TypeRef)); if (info.Parent.IsTopLevelParent) { cx.Emit(Tuples.expr_parent_top_level(this, info.Child, info.Parent)); } else { cx.Emit(Tuples.expr_parent(this, info.Child, info.Parent)); } cx.Emit(Tuples.expr_location(this, Location)); if (info.IsCompilerGenerated) { cx.Emit(Tuples.expr_compiler_generated(this)); } if (info.ExprValue is string value) { cx.Emit(Tuples.expr_value(this, value)); } Type.Type.ExtractGenerics(); }
public ExpressionInfo(Context cx, AnnotatedType type, Extraction.Entities.Location location, ExprKind kind, IExpressionParentEntity parent, int child, bool isCompilerGenerated, string value) { Context = cx; Type = type; Location = location; Kind = kind; Parent = parent; Child = child; ExprValue = value; IsCompilerGenerated = isCompilerGenerated; }
private static ExprKind GetExprKind(ITypeSymbol?type, ExpressionSyntax?expr, Extraction.Entities.Location loc, Context context) { switch (type?.SpecialType) { case SpecialType.System_Boolean: return(ExprKind.BOOL_LITERAL); case SpecialType.System_Int16: case SpecialType.System_Int32: case SpecialType.System_Byte: // ?? return(ExprKind.INT_LITERAL); case SpecialType.System_Char: return(ExprKind.CHAR_LITERAL); case SpecialType.System_Decimal: return(ExprKind.DECIMAL_LITERAL); case SpecialType.System_Double: return(ExprKind.DOUBLE_LITERAL); case SpecialType.System_Int64: return(ExprKind.LONG_LITERAL); case SpecialType.System_Single: return(ExprKind.FLOAT_LITERAL); case SpecialType.System_String: return(ExprKind.STRING_LITERAL); case SpecialType.System_UInt16: case SpecialType.System_UInt32: case SpecialType.System_SByte: // ?? return(ExprKind.UINT_LITERAL); case SpecialType.System_UInt64: return(ExprKind.ULONG_LITERAL); case null: default: var kind = type?.SpecialType.ToString() ?? "null"; if (expr is not null) { context.ModelError(expr, $"Unhandled literal type {kind}"); } else { context.ModelError(loc, $"Unhandled literal type {kind}"); } return(ExprKind.UNKNOWN); } }
internal Expression(IExpressionInfo info) : base(info.Context) { Info = info; Location = info.Location; Kind = info.Kind; Type = info.Type; if (Type.Type is null) { Type = NullType.Create(cx); } TryPopulate(); }
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value, Extraction.Entities.Location location) { var info = new ExpressionInfo( cx, AnnotatedTypeSymbol.CreateNotAnnotated(type), location, GetExprKind(type, null, cx), parent, childIndex, true, ValueAsString(value)); return(new Expression(info)); }
/// <summary> /// Creates a generated expression for a default argument value. /// </summary> public static Expression?CreateGenerated(Context cx, IParameterSymbol parameter, IExpressionParentEntity parent, int childIndex, Extraction.Entities.Location location) { if (!parameter.HasExplicitDefaultValue || parameter.Type is IErrorTypeSymbol) { return(null); } var defaultValue = parameter.ExplicitDefaultValue; var type = parameter.Type; if (type.IsBoundNullable() && type is INamedTypeSymbol named) { type = named.TypeArguments[0]; } if (type is INamedTypeSymbol nt && nt.EnumUnderlyingType is not null) { // = (MyEnum)1, = MyEnum.Value1, = default(MyEnum), = new MyEnum() // we're generating a (MyEnum)value cast expression: defaultValue ??= 0; Action <Expression, int> createChild = (parent, index) => Literal.CreateGenerated(cx, parent, index, nt.EnumUnderlyingType, defaultValue, location); return(Cast.CreateGenerated(cx, parent, childIndex, parameter.Type, defaultValue, createChild, location)); } if (defaultValue is null) { // = null, = default, = default(T), = new MyStruct() // we're generating a default expression: return(Default.CreateGenerated(cx, parent, childIndex, location, parameter.Type.IsReferenceType ? ValueAsString(null) : null)); } if (parameter.Type.SpecialType is SpecialType.System_Object) { // this can happen in VB.NET cx.ExtractionError($"Extracting default argument value 'object {parameter.Name} = default' instead of 'object {parameter.Name} = {defaultValue}'. The latter is not supported in C#.", null, null, severity: Util.Logging.Severity.Warning); // we're generating a default expression: return(Default.CreateGenerated(cx, parent, childIndex, location, ValueAsString(null))); } // const literal: return(Literal.CreateGenerated(cx, parent, childIndex, type, defaultValue, location)); }
/// <summary> /// Creates a generated expression from a typed constant. /// </summary> public static Expression?CreateGenerated(Context cx, TypedConstant constant, IExpressionParentEntity parent, int childIndex, Extraction.Entities.Location location) { if (constant.IsNull || constant.Type is null) { return(Literal.CreateGeneratedNullLiteral(cx, parent, childIndex, location)); } switch (constant.Kind) { case TypedConstantKind.Primitive: return(Literal.CreateGenerated(cx, parent, childIndex, constant.Type, constant.Value, location)); case TypedConstantKind.Enum: // Enum value is generated in the following format: (Enum)value Action <Expression, int> createChild = (parent, index) => Literal.CreateGenerated(cx, parent, index, ((INamedTypeSymbol)constant.Type).EnumUnderlyingType !, constant.Value, location); var cast = Cast.CreateGenerated(cx, parent, childIndex, constant.Type !, constant.Value, createChild, location); return(cast); case TypedConstantKind.Type: var type = ((ITypeSymbol)constant.Value !).OriginalDefinition; return(TypeOf.CreateGenerated(cx, parent, childIndex, type, location)); case TypedConstantKind.Array: // Single dimensional arrays are in the following format: // * new Type[N] { item1, item2, ..., itemN } // * new Type[0] // // itemI is generated recursively. return(NormalArrayCreation.CreateGenerated(cx, parent, childIndex, constant.Type, constant.Values, location)); case TypedConstantKind.Error: default: cx.ExtractionError("Couldn't extract constant in attribute", constant.ToString(), location); return(null); } }
private Expression AddInitializerAssignment(TextWriter trapFile, ExpressionSyntax initializer, Extraction.Entities.Location loc, string?constValue, ref int child) { var type = Symbol.GetAnnotatedType(); var simpleAssignExpr = new Expression(new ExpressionInfo(Context, type, loc, ExprKind.SIMPLE_ASSIGN, this, child++, false, constValue)); Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer, simpleAssignExpr, 0)); var access = new Expression(new ExpressionInfo(Context, type, Location, ExprKind.FIELD_ACCESS, simpleAssignExpr, 1, false, constValue)); trapFile.expr_access(access, this); return(access); }
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int index, Extraction.Entities.Location location) { var info = new ExpressionInfo( cx, null, location, ExprKind.ARRAY_INIT, parent, index, true, null); return(new Expression(info)); }
public static Expression CreateGeneratedNullLiteral(Context cx, IExpressionParentEntity parent, int childIndex, Extraction.Entities.Location location) { var info = new ExpressionInfo( cx, null, location, ExprKind.NULL_LITERAL, parent, childIndex, true, ValueAsString(null)); return(new Expression(info)); }
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, Microsoft.CodeAnalysis.ITypeSymbol type, Extraction.Entities.Location location) { var typeAccessInfo = new ExpressionInfo( cx, AnnotatedTypeSymbol.CreateNotAnnotated(type), location, ExprKind.TYPE_ACCESS, parent, childIndex, true, null); return(new Expression(typeAccessInfo)); }
public static This CreateImplicit(Context cx, ITypeSymbol @class, Extraction.Entities.Location loc, IExpressionParentEntity parent, int child) => new This(new ExpressionInfo(cx, AnnotatedTypeSymbol.CreateNotAnnotated(@class), loc, Kinds.ExprKind.THIS_ACCESS, parent, child, true, null));
public static This CreateImplicit(Context cx, Type @class, Extraction.Entities.Location loc, IExpressionParentEntity parent, int child) => new This(new ExpressionInfo(cx, new AnnotatedType(@class, NullableAnnotation.None), loc, Kinds.ExprKind.THIS_ACCESS, parent, child, true, null));
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, Microsoft.CodeAnalysis.ITypeSymbol type, object?value, Action <Expression, int> createChild, Extraction.Entities.Location location) { var info = new ExpressionInfo( cx, AnnotatedTypeSymbol.CreateNotAnnotated(type), location, ExprKind.CAST, parent, childIndex, true, ValueAsString(value)); var ret = new Expression(info); createChild(ret, ExpressionIndex); TypeAccess.CreateGenerated(cx, ret, TypeAccessIndex, type, location); return(ret); }
public static LocalVariable Create(Context cx, ISymbol local, Expression parent, bool isVar, Extraction.Entities.Location declLocation) { return(LocalVariableFactory.Instance.CreateEntity(cx, local, parent, isVar, declLocation)); }
public static VariableDeclaration Create(Context cx, ISymbol symbol, AnnotatedType type, TypeSyntax optionalSyntax, Extraction.Entities.Location exprLocation, Extraction.Entities.Location declLocation, bool isVar, IExpressionParentEntity parent, int child) { var ret = new VariableDeclaration(new ExpressionInfo(cx, type, exprLocation, ExprKind.LOCAL_VAR_DECL, parent, child, false, null)); cx.Try(null, null, () => { LocalVariable.Create(cx, symbol, ret, isVar, declLocation); if (optionalSyntax != null) { TypeMention.Create(cx, optionalSyntax, parent, type); } }); return(ret); }
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, Extraction.Entities.Location location, string?value) { var info = new ExpressionInfo( cx, null, location, ExprKind.DEFAULT, parent, childIndex, true, value); return(new Expression(info)); }