/// <summary> /// Replces the Value of the given constant with the value referenced by the [c.Reference, c.Value] pair. /// </summary> /// <param name="c">The Constant to translate</param> /// <param name="enums">The list of enums to check.</param> /// <param name="auxEnums">The list of auxilliary enums to check.</param> /// <returns>True if the reference was found; false otherwise.</returns> public static bool TranslateConstantWithReference(Constant c, EnumCollection enums, EnumCollection auxEnums) { if (!String.IsNullOrEmpty(c.Reference)) { Constant referenced_constant; if (enums.ContainsKey(c.Reference) && enums[c.Reference].ConstantCollection.ContainsKey(c.Value)) { TranslateConstantWithReference(enums[c.Reference].ConstantCollection[c.Value] as Constant, enums, auxEnums); referenced_constant = (enums[c.Reference].ConstantCollection[c.Value] as Constant); } else if (auxEnums.ContainsKey(c.Reference) && auxEnums[c.Reference].ConstantCollection.ContainsKey(c.Value)) { TranslateConstantWithReference(auxEnums[c.Reference].ConstantCollection[c.Value] as Constant, enums, auxEnums); referenced_constant = (auxEnums[c.Reference].ConstantCollection[c.Value] as Constant); } else { Console.WriteLine("[Warning] Reference {0} not found for token {1}.", c.Reference, c); return(false); } //else throw new InvalidOperationException(String.Format("Unknown Enum \"{0}\" referenced by Constant \"{1}\"", // c.Reference, c.ToString())); c.Value = referenced_constant.Value; c.Reference = null; c.Unchecked = referenced_constant.Unchecked; } return(true); }
/// <summary> /// Replces the Value of the given constant with the value referenced by the [c.Reference, c.Value] pair. /// </summary> /// <param name="c">The Constant to translate</param> /// <param name="enums">The list of enums to check.</param> /// <param name="auxEnums">The list of auxilliary enums to check.</param> /// <returns>True if the reference was found; false otherwise.</returns> public static bool TranslateConstantWithReference(Constant c, EnumCollection enums) { if (c == null) { throw new ArgumentNullException("c"); } if (enums == null) { throw new ArgumentNullException("enums"); } if (!String.IsNullOrEmpty(c.Reference)) { // Resolve the referenced Constant. Be careful // to avoid loops in the definitions. Constant reference = c; do { reference = enums.ContainsKey(reference.Reference) && enums[reference.Reference].ConstantCollection.ContainsKey(reference.Value) ? enums[reference.Reference].ConstantCollection[reference.Value] : null; } while (reference != null && reference.Reference != null && reference.Reference != c.Reference); // If we haven't managed to locate the reference, do // a brute-force search through all enums. if (reference == null || reference.Reference != null) { reference = enums.Values.Select(e => e.ConstantCollection.Values.FirstOrDefault(t => t.Reference == null && t.Name == c.Name)) .FirstOrDefault(t => t != null); } // Resolve the value for this Constant if (reference != null) { c.Value = reference.Value; c.Reference = null; return(true); } else { Trace.WriteLine(String.Format("[Warning] Failed to resolve token: {0}", c)); return(false); } } return(true); }
public virtual void Translate(XPathNavigator overrides, string category, EnumCollection enums) { Enum @enum; string s; category = EnumProcessor.TranslateEnumName(category); // Try to find out if it is an enum. If the type exists in the normal GLEnums list, use this. // Otherwise, try to find it in the aux enums list. If it exists in neither, it is not an enum. // Special case for Boolean - it is an enum, but it is dumb to use that instead of the 'bool' type. bool normal = enums.TryGetValue(CurrentType, out @enum); //bool aux = enums.TryGetValue(EnumProcessor.TranslateEnumName(CurrentType), out @enum); // Translate enum types if ((normal /*|| aux*/) && @enum.Name != "GLenum" && @enum.Name != "Boolean") { if ((Settings.Compatibility & Settings.Legacy.ConstIntEnums) != Settings.Legacy.None) { QualifiedType = "int"; } else { // Some functions and enums have the same names. // Make sure we reference the enums rather than the functions. if (normal) { QualifiedType = CurrentType.Insert(0, String.Format("{0}.", Settings.EnumsOutput)); } } } else if (GLTypes.TryGetValue(CurrentType, out s)) { // Check if the parameter is a generic GLenum. If it is, search for a better match, // otherwise fallback to Settings.CompleteEnumName (named 'All' by default). if (s.Contains("GLenum") /*&& !String.IsNullOrEmpty(category)*/) { if ((Settings.Compatibility & Settings.Legacy.ConstIntEnums) != Settings.Legacy.None) { QualifiedType = "int"; } else { // Better match: enum.Name == function.Category (e.g. GL_VERSION_1_1 etc) if (enums.ContainsKey(category)) { QualifiedType = String.Format("{0}{1}{2}", Settings.EnumsOutput, Settings.NamespaceSeparator, EnumProcessor.TranslateEnumName(category)); } else { QualifiedType = String.Format("{0}{1}{2}", Settings.EnumsOutput, Settings.NamespaceSeparator, Settings.CompleteEnumName); } } } else { // A few translations for consistency switch (CurrentType.ToLower()) { case "string": QualifiedType = "String"; break; } QualifiedType = s; } } CurrentType = CSTypes.ContainsKey(CurrentType) ? CSTypes[CurrentType] : CurrentType; // Make sure that enum parameters follow enum overrides, i.e. // if enum ErrorCodes is overriden to ErrorCode, then parameters // of type ErrorCodes should also be overriden to ErrorCode. XPathNavigator enum_override = overrides.SelectSingleNode(String.Format("/signatures/replace/enum[@name='{0}']/name", CurrentType)); if (enum_override != null) { // For consistency - many overrides use string instead of String. if (enum_override.Value == "string") { QualifiedType = "String"; } else if (enum_override.Value == "StringBuilder") { QualifiedType = "StringBuilder"; } else { CurrentType = enum_override.Value; } } if (CurrentType == "IntPtr" && String.IsNullOrEmpty(PreviousType)) { Pointer = 0; } }
/// <summary> /// Replces the Value of the given constant with the value referenced by the [c.Reference, c.Value] pair. /// </summary> /// <param name="c">The Constant to translate</param> /// <param name="enums">The list of enums to check.</param> /// <param name="auxEnums">The list of auxilliary enums to check.</param> /// <returns>True if the reference was found; false otherwise.</returns> public static bool TranslateConstantWithReference(Constant c, EnumCollection enums, EnumCollection auxEnums) { if (c == null) { throw new ArgumentNullException("c"); } if (enums == null) { throw new ArgumentNullException("enums"); } if (++CurrentReferenceDepth >= MaxReferenceDepth) { throw new InvalidOperationException("Enum specification contains cycle"); } if (!String.IsNullOrEmpty(c.Reference)) { Constant referenced_constant; if (enums.ContainsKey(c.Reference) && enums[c.Reference].ConstantCollection.ContainsKey(c.Value)) { // Transitively translate the referenced token // Todo: this may cause loops if two tokens reference each other. // Add a max reference depth and bail out? TranslateConstantWithReference(enums[c.Reference].ConstantCollection[c.Value], enums, auxEnums); referenced_constant = (enums[c.Reference].ConstantCollection[c.Value]); } else if (auxEnums != null && auxEnums.ContainsKey(c.Reference) && auxEnums[c.Reference].ConstantCollection.ContainsKey(c.Value)) { // Legacy from previous generator incarnation. // Todo: merge everything into enums and get rid of auxEnums. TranslateConstantWithReference(auxEnums[c.Reference].ConstantCollection[c.Value], enums, auxEnums); referenced_constant = (auxEnums[c.Reference].ConstantCollection[c.Value]); } else if (enums.ContainsKey(Settings.CompleteEnumName) && enums[Settings.CompleteEnumName].ConstantCollection.ContainsKey(c.Value)) { // Try the All enum var reference = enums[Settings.CompleteEnumName].ConstantCollection[c.Value]; if (reference.Reference == null) { referenced_constant = (enums[Settings.CompleteEnumName].ConstantCollection[c.Value]); } else { --CurrentReferenceDepth; return(false); } } else { --CurrentReferenceDepth; return(false); } //else throw new InvalidOperationException(String.Format("Unknown Enum \"{0}\" referenced by Constant \"{1}\"", // c.Reference, c.ToString())); c.Value = referenced_constant.Value; c.Reference = null; } --CurrentReferenceDepth; return(true); }