Ejemplo n.º 1
0
        private static IEnumerable <ErrorDiagnostic> GetArrayAssignmentDiagnostics(ITypeManager typeManager, ArraySyntax expression, ArrayType targetType, bool skipConstantCheck)
        {
            // if we have parse errors, no need to check assignability
            // we should not return the parse errors however because they will get double collected
            if (expression.HasParseErrors())
            {
                return(Enumerable.Empty <ErrorDiagnostic>());
            }

            return(expression.Items
                   .SelectMany(arrayItemSyntax => GetExpressionAssignmentDiagnosticsInternal(
                                   typeManager,
                                   arrayItemSyntax.Value,
                                   targetType.ItemType,
                                   (expectedType, actualType, errorExpression) => DiagnosticBuilder.ForPosition(errorExpression).ArrayTypeMismatch(expectedType.Name, actualType.Name),
                                   skipConstantCheck,
                                   skipTypeErrors: true)));
        }
Ejemplo n.º 2
0
        private static void SaveClass(Type type, DocumentSyntax document, KeySyntax key = null)
        {
            key ??= new KeySyntax(type.Name);

            var fields   = type.GetFields(StaticFlags);
            var children = type.GetNestedTypes(StaticFlags);

            var table      = new TableSyntax(key);
            var tableItems = table.Items;

            foreach (var field in fields)
            {
                if (field.IsPrivate || field.IsInitOnly || !field.IsStatic || field.IsLiteral)
                {
                    continue;
                }

                if (field.GetCustomAttribute <Config.ConfigIgnoreAttribute>() != null)
                {
                    continue;
                }

                ValueSyntax value      = null;
                object      fieldValue = field.GetValue(null);

                switch (fieldValue)
                {
                case string v:
                    value = new StringValueSyntax(v);
                    break;

                case sbyte v:
                    value = new IntegerValueSyntax(v);
                    break;

                case byte v:
                    value = new IntegerValueSyntax(v);
                    break;

                case short v:
                    value = new IntegerValueSyntax(v);
                    break;

                case ushort v:
                    value = new IntegerValueSyntax(v);
                    break;

                case int v:
                    value = new IntegerValueSyntax(v);
                    break;

                case uint v:
                    value = new IntegerValueSyntax(v);
                    break;

                case ulong v:
                    value = new IntegerValueSyntax(unchecked ((long)v));
                    break;

                case float v:
                    value = new FloatValueSyntax(v);
                    break;

                case double v:
                    value = new FloatValueSyntax(v);
                    break;

                case bool v:
                    value = new BooleanValueSyntax(v);
                    break;

                default:
                    if (fieldValue is List <string> slist)
                    {
                        value = new ArraySyntax(slist.ToArray());
                    }
                    else if (fieldValue is List <int> ilist)
                    {
                        value = new ArraySyntax(ilist.ToArray());
                    }
                    else if (fieldValue.GetType().IsEnum)
                    {
                        value = new StringValueSyntax(fieldValue.GetType().GetEnumName(fieldValue));
                    }
                    break;
                }

                if (value == null)
                {
                    continue;
                }

                var keyValue = new KeyValueSyntax(
                    field.Name,
                    value
                    );

                //if (field.GetAttribute<Config.CommentAttribute>(out var attribute)) {
                //keyValue.GetChildren(Math.Max(0, keyValue.ChildrenCount - 2)).AddComment(attribute.Message);
                //}

                tableItems.Add(keyValue);
            }

            if (table.Items.ChildrenCount != 0)
            {
                document.Tables.Add(table);
            }

            foreach (var child in children)
            {
                if (child.IsNestedPrivate)
                {
                    continue;
                }
                if (child.GetCustomAttribute <Config.ConfigIgnoreAttribute>() != null)
                {
                    continue;
                }
                var childKey  = new KeySyntax(typeof(Config).Name);
                var parentKey = key.ToString().Split('.');
                if (parentKey.Length != 0)
                {
                    parentKey[0] = null;
                }
                foreach (var subKey in parentKey)
                {
                    if (subKey == null)
                    {
                        continue;
                    }
                    childKey.DotKeys.Add(new DottedKeyItemSyntax(subKey));
                }
                childKey.DotKeys.Add(new DottedKeyItemSyntax(child.Name));
                SaveClass(child, document, childKey);
            }
        }
Ejemplo n.º 3
0
        private static TypeSymbol NarrowArrayAssignmentType(ITypeManager typeManager, ArraySyntax expression, ArrayType targetType, IDiagnosticWriter diagnosticWriter, bool skipConstantCheck)
        {
            // if we have parse errors, no need to check assignability
            // we should not return the parse errors however because they will get double collected
            if (expression.HasParseErrors())
            {
                return(targetType);
            }

            var arrayProperties = new List <TypeSymbol>();

            foreach (var arrayItemSyntax in expression.Items)
            {
                arrayProperties.Add(NarrowTypeInternal(
                                        typeManager,
                                        arrayItemSyntax.Value,
                                        targetType.Item.Type,
                                        diagnosticWriter,
                                        (expectedType, actualType, errorExpression) => DiagnosticBuilder.ForPosition(errorExpression).ArrayTypeMismatch(ShouldWarn(targetType), expectedType, actualType),
                                        skipConstantCheck,
                                        skipTypeErrors: true));
            }

            return(new TypedArrayType(UnionType.Create(arrayProperties), targetType.ValidationFlags));
        }