private TypeSymbol GetStringType(TypeManagerContext context, StringSyntax @string) { if (@string.IsInterpolated() == false) { // uninterpolated strings have a known type return(LanguageConstants.String); } var errors = new List <ErrorDiagnostic>(); foreach (var interpolatedExpression in @string.Expressions) { var expressionType = this.GetTypeInfoInternal(context, interpolatedExpression); CollectErrors(errors, expressionType); } if (errors.Any()) { return(new ErrorTypeSymbol(errors)); } // normally we would also do an assignability check, but we allow "any" type in string interpolation expressions // so the assignability check cannot possibly fail (we already collected type errors from the inner expressions at this point) return(LanguageConstants.String); }
public override void VisitStringSyntax(StringSyntax syntax) => AssignTypeWithCaching(syntax, () => { if (!syntax.IsInterpolated()) { // uninterpolated strings have a known type return(new StringLiteralType(syntax.GetLiteralValue())); } var errors = new List <ErrorDiagnostic>(); foreach (var interpolatedExpression in syntax.Expressions) { var expressionType = VisitAndReturnType(interpolatedExpression); CollectErrors(errors, expressionType); } if (errors.Any()) { return(new ErrorTypeSymbol(errors)); } // normally we would also do an assignability check, but we allow "any" type in string interpolation expressions // so the assignability check cannot possibly fail (we already collected type errors from the inner expressions at this point) return(LanguageConstants.String); });
private LanguageExpression ConvertString(StringSyntax syntax) { if (!syntax.IsInterpolated()) { // no need to build a format string return(new JTokenExpression(syntax.GetLiteralValue()));; } var formatArgs = new LanguageExpression[syntax.Expressions.Length + 1]; var formatString = StringFormatConverter.BuildFormatString(syntax); formatArgs[0] = new JTokenExpression(formatString); for (var i = 0; i < syntax.Expressions.Length; i++) { formatArgs[i + 1] = ConvertExpression(syntax.Expressions[i]); } return(new FunctionExpression("format", formatArgs, Array.Empty <LanguageExpression>())); }
/// <summary> /// Try to get the string literal value for a syntax node. Returns null if the string is interpolated. /// </summary> /// <param name="syntax">The string syntax node</param> public static string?TryGetLiteralValue(this StringSyntax syntax) => syntax.IsInterpolated() ? null : syntax.SegmentValues[0];