public static Enumeration GenerateEnumFromMacros(this ASTContext context, string name, params string[] macros) { var @enum = new Enumeration { Name = name }; var pattern = string.Join("|", macros); var regex = new Regex(pattern); int maxItems = 0; TranslationUnit unitToAttach = null; ulong maxValue = 0; foreach (var unit in context.TranslationUnits) { int numItems = 0; foreach (var macro in unit.PreprocessedEntities.OfType <MacroDefinition>()) { var match = regex.Match(macro.Name); if (!match.Success) { continue; } if (macro.Enumeration != null) { continue; } // Skip this macro if the enum already has an item with same name. if (@enum.Items.Exists(it => it.Name == macro.Name)) { continue; } var item = @enum.GenerateEnumItemFromMacro(macro); @enum.AddItem(item); macro.Enumeration = @enum; maxValue = Math.Max(maxValue, item.Value); numItems++; } if (numItems > maxItems) { maxItems = numItems; unitToAttach = unit; } } if (@enum.Items.Count > 0) { @enum.Namespace = unitToAttach; unitToAttach.Declarations.Add(@enum); } return(@enum); }
public static Enumeration GenerateEnumFromMacros(this Library library, string name, params string[] macros) { var @enum = new Enumeration { Name = name }; var pattern = string.Join("|", macros); var regex = new Regex(pattern); foreach (var unit in library.TranslationUnits) { foreach (var macro in unit.Macros) { var match = regex.Match(macro.Name); if (!match.Success) { continue; } var item = GenerateEnumItemFromMacro(library, macro); @enum.AddItem(item); } if (@enum.Items.Count > 0) { unit.Enums.Add(@enum); break; } } return(@enum); }
public Enumeration VisitEnum(TypeInfo type) { var underlyingType = type.GetEnumUnderlyingType(); var @enum = new Enumeration { Name = UnmangleTypeName(type.Name), BuiltinType = VisitType(underlyingType).Type as BuiltinType }; HandleNamespace(type, @enum); if (Options.GeneratorKind == GeneratorKind.CPlusPlus) { @enum.Modifiers |= Enumeration.EnumModifiers.Scoped; } bool flags = type.HasCustomAttribute("System", "FlagsAttribute"); if (flags) { @enum.Modifiers |= Enumeration.EnumModifiers.Flags; } foreach (var item in type.DeclaredFields) { if (!item.IsLiteral) { continue; } var enumItem = new Enumeration.Item { Namespace = @enum, Name = item.Name, ExplicitValue = true }; var rawValue = item.GetRawConstantValue(); if (@enum.BuiltinType.IsUnsigned) { enumItem.Value = Convert.ToUInt64(rawValue); } else { enumItem.Value = (ulong)Convert.ToInt64(rawValue); } @enum.AddItem(enumItem); } ManagedNames[@enum] = type.FullName; return(@enum); }
public static Enumeration GenerateEnumFromMacros(this ASTContext context, string name, params string[] macros) { var @enum = new Enumeration { Name = name }; var pattern = string.Join("|", macros); var regex = new Regex(pattern); foreach (var unit in context.TranslationUnits) { foreach (var macro in unit.PreprocessedEntities.OfType <MacroDefinition>()) { var match = regex.Match(macro.Name); if (!match.Success) { continue; } if (macro.Enumeration != null) { continue; } // Skip this macro if the enum already has an item with same entry. if (@enum.Items.Exists(it => it.Name == macro.Name)) { continue; } // Set the namespace to the namespace we found the 1st item in if (@enum.Namespace == null) { @enum.Namespace = unit; } var item = @enum.GenerateEnumItemFromMacro(macro); @enum.AddItem(item); macro.Enumeration = @enum; } if (@enum.Items.Count > 0) { unit.Declarations.Add(@enum); break; } } return(@enum); }
public Enumeration VisitEnum(TypeInfo type) { var underlyingType = type.GetEnumUnderlyingType(); var @enum = new Enumeration { Name = UnmangleTypeName(type.Name), Type = VisitType(underlyingType).Type }; HandleNamespace(type, @enum); if (Options.GeneratorKind == GeneratorKind.CPlusPlus) { @enum.Modifiers = Enumeration.EnumModifiers.Scoped; } var builtinType = @enum.Type as BuiltinType; foreach (var item in type.DeclaredFields) { if (!item.IsLiteral) { continue; } var enumItem = new Enumeration.Item { Namespace = @enum, Name = item.Name, ExplicitValue = true }; var rawValue = item.GetRawConstantValue(); if (builtinType.IsUnsigned) { enumItem.Value = Convert.ToUInt64(rawValue); } else { enumItem.Value = (ulong)Convert.ToInt64(rawValue); } @enum.AddItem(enumItem); } return(@enum); }
public static Enumeration GenerateEnumFromMacros(this ASTContext context, string name, params string[] macros) { var @enum = new Enumeration { Name = name }; var pattern = string.Join("|", macros); var regex = new Regex(pattern); foreach (var unit in context.TranslationUnits) { foreach (var macro in unit.PreprocessedEntities.OfType <MacroDefinition>()) { var match = regex.Match(macro.Name); if (!match.Success) { continue; } if (macro.Enumeration != null) { continue; } var item = GenerateEnumItemFromMacro(context, macro); @enum.AddItem(item); macro.Enumeration = @enum; } if (@enum.Items.Count > 0) { unit.Enums.Add(@enum); break; } } return(@enum); }
public Enumeration VisitEnum(TypeInfo type) { var underlyingType = type.GetEnumUnderlyingType(); var @enum = new Enumeration { Name = UnmangleTypeName(type.Name), Type = VisitType(underlyingType).Type }; HandleNamespace(type, @enum); if (Options.Language == GeneratorKind.CPlusPlus) { @enum.Modifiers = Enumeration.EnumModifiers.Scoped; } foreach (var item in type.DeclaredFields) { if (!item.IsLiteral) { continue; } var @value = Convert.ToUInt64(item.GetRawConstantValue()); var enumItem = new Enumeration.Item { Name = item.Name, Value = @value, ExplicitValue = true }; @enum.AddItem(enumItem); } return(@enum); }
public static Enumeration GenerateEnumFromMacros(this ASTContext context, string name, params string[] macros) { var @enum = new Enumeration { Name = name }; var pattern = string.Join("|", macros); var regex = new Regex(pattern); foreach (var unit in context.TranslationUnits) { foreach (var macro in unit.PreprocessedEntities.OfType<MacroDefinition>()) { var match = regex.Match(macro.Name); if (!match.Success) continue; if (macro.Enumeration != null) continue; // Skip this macro if the enum already has an item with same entry. if (@enum.Items.Exists(it => it.Name == macro.Name)) continue; var item = GenerateEnumItemFromMacro(context, macro); @enum.AddItem(item); macro.Enumeration = @enum; } if (@enum.Items.Count > 0) { unit.Enums.Add(@enum); break; } } return @enum; }
public static Enumeration GenerateEnumFromMacros(this ASTContext context, string name, params string[] macros) { var @enum = new Enumeration { Name = name }; var pattern = string.Join("|", macros); var regex = new Regex(pattern); foreach (var unit in context.TranslationUnits) { foreach (var macro in unit.Macros) { var match = regex.Match(macro.Name); if (!match.Success) continue; var item = GenerateEnumItemFromMacro(context, macro); @enum.AddItem(item); } if (@enum.Items.Count > 0) { unit.Enums.Add(@enum); break; } } return @enum; }
public static Enumeration GenerateEnumFromMacros(this ASTContext context, string name, params string[] values) { TranslationUnit GetUnitFromItems(List <Enumeration.Item> list) { var units = list.Select(item => item.TranslationUnit) .GroupBy(x => x) .Select(y => new { Element = y.Key, Counter = y.Count() }); var translationUnit = units.OrderByDescending(u => u.Counter) .FirstOrDefault()?.Element ?? null; return(translationUnit); } var @enum = new Enumeration { Name = name }; var regexPattern = string.Join("|", values.Select(pattern => $"({pattern}$)")); var regex = new Regex(regexPattern); var items = FindMatchingEnumItems(context, regex); foreach (var item in items) { @enum.AddItem(item); } TranslationUnit macroUnit = null; var macroDefs = FindMatchingMacros(context, regex, ref macroUnit); foreach (var macro in macroDefs) { // Skip this macro if the enum already has an item with same name. if (@enum.Items.Exists(it => it.Name == macro.Name)) { continue; } var item = @enum.GenerateEnumItemFromMacro(macro); @enum.AddItem(item); macro.Enumeration = @enum; } if (@enum.Items.Count <= 0) { return(@enum); } var maxValue = @enum.Items.Max(i => i.Value); @enum.BuiltinType = new BuiltinType(GetUnderlyingTypeForEnumValue(maxValue)); @enum.Type = @enum.BuiltinType; var unit = macroUnit ?? GetUnitFromItems(items); @enum.Namespace = unit; unit.Declarations.Add(@enum); return(@enum); }