Exemplo n.º 1
0
        public void Write(FileWriter writer, ConstantsType constantsType, string[] attributes)
        {
            docWriter.WriteSummary(writer, constantsType.Documentation, constantsType.RawName);
            foreach (var attr in attributes)
            {
                writer.WriteLine(attr);
            }
            var pub = constantsType.IsPublic ? "pub " : "pub(crate) ";

            writer.WriteLine($"{pub}struct {constantsType.Name(idConverter)};");
            if (constantsType.IsPublic && constantsType.IsMissingDocs)
            {
                writer.WriteLine(RustConstants.AttributeAllowMissingDocs);
            }
            foreach (var attr in attributes.Where(a => a.StartsWith(RustConstants.FeaturePrefix, StringComparison.Ordinal)))
            {
                writer.WriteLine(attr);
            }
            if (!constantsType.IsPublic)
            {
                writer.WriteLine(RustConstants.AttributeAllowDeadCode);
            }
            writer.WriteLine($"impl {constantsType.Name(idConverter)} {{");

            var sb = new StringBuilder();

            using (writer.Indent()) {
                foreach (var constant in constantsType.Constants)
                {
                    if (ShouldIgnore(constant))
                    {
                        continue;
                    }
                    docWriter.WriteSummary(writer, constant.Documentation, constantsType.RawName);
                    deprecatedWriter.WriteDeprecated(writer, constant);
                    sb.Clear();
                    sb.Append(constant.IsPublic ? "pub " : "pub(crate) ");
                    sb.Append("const ");
                    sb.Append(constant.Name(idConverter));
                    sb.Append(": ");
                    sb.Append(GetType(constant.Kind));
                    sb.Append(" = ");
                    sb.Append(GetValue(constant));
                    sb.Append(';');
                    writer.WriteLine(sb.ToString());
                }
            }

            writer.WriteLine("}");
        }
Exemplo n.º 2
0
        void WriteConstants(FileWriter writer, ConstantsType constantsType)
        {
            docWriter.WriteSummary(writer, constantsType.Documentation, constantsType.RawName);
            var pub = constantsType.IsPublic ? "public " : string.Empty;

            writer.WriteLine($"{pub}static class {constantsType.Name(idConverter)} {{");

            using (writer.Indent()) {
                foreach (var constant in constantsType.Constants)
                {
                    docWriter.WriteSummary(writer, constant.Documentation, constantsType.RawName);
                    deprecatedWriter.WriteDeprecated(writer, constant);
                    writer.Write(constant.IsPublic ? "public " : "internal ");
                    writer.Write("const ");
                    writer.Write(GetType(constant.Kind));
                    writer.Write(" ");
                    writer.Write(constant.Name(idConverter));
                    writer.Write(" = ");
                    writer.Write(GetValue(constant));
                    writer.WriteLine(";");
                }
            }

            writer.WriteLine("}");
        }
Exemplo n.º 3
0
        void WriteConstants(FileWriter writer, ConstantsType constantsType, bool isPartialClass)
        {
            docWriter.WriteSummary(writer, constantsType.Documentation.GetComment(TargetLanguage.CSharp), constantsType.RawName);
            var pub     = constantsType.IsPublic ? "public " : string.Empty;
            var partial = isPartialClass ? " partial" : string.Empty;

            writer.WriteLine($"{pub}static{partial} class {constantsType.Name(idConverter)} {{");

            using (writer.Indent()) {
                foreach (var constant in constantsType.Constants)
                {
                    docWriter.WriteSummary(writer, constant.Documentation.GetComment(TargetLanguage.CSharp), constantsType.RawName);
                    deprecatedWriter.WriteDeprecated(writer, constant);
                    writer.Write(constant.IsPublic ? "public " : "internal ");
                    writer.Write("const ");
                    writer.Write(GetType(constant.Kind));
                    writer.Write(" ");
                    writer.Write(constant.Name(idConverter));
                    writer.Write(" = ");
                    writer.Write(GetValue(constant));
                    writer.WriteLine(";");
                }
            }

            writer.WriteLine("}");
        }
Exemplo n.º 4
0
 void WriteMacro(FileWriter writer, ConstantsType newType, ConstantsType origType)
 {
     writer.WriteLine("lazy_static! {");
     using (writer.Indent()) {
         writer.WriteLine($"pub(super) static ref {idConverter.Constant("To" + newType.RawName)}: HashMap<&'static str, u32> = {{");
         using (writer.Indent()) {
             if (newType.Constants.Length == 0)
             {
                 writer.WriteLine($"let h = HashMap::new();");
             }
             else
             {
                 writer.WriteLine($"let mut h = HashMap::with_capacity({newType.Constants.Length});");
             }
             var origConstants = origType.Constants;
             var newConstants  = newType.Constants;
             if (origConstants.Length != newConstants.Length)
             {
                 throw new InvalidOperationException();
             }
             for (int i = 0; i < origConstants.Length; i++)
             {
                 var origConstant = origConstants[i];
                 if (origConstant.Kind != ConstantKind.String)
                 {
                     throw new InvalidOperationException();
                 }
                 var newConstant = newConstants[i];
                 if (newConstant.Kind != ConstantKind.UInt32)
                 {
                     throw new InvalidOperationException();
                 }
                 writer.WriteLine($"h.insert(\"{(string)origConstant.RefValue!}\", {newType.Name(idConverter)}::{newConstant.Name(idConverter)});");
             }
             writer.WriteLine("h");
         }
         writer.WriteLine("};");
     }
     writer.WriteLine("}");
 }