private GetUserType ( Module module, string typeString, |
||
module | Module | The module. |
typeString | string | The type string. |
userType | The found user type. | |
Résultat | bool |
/// <summary> /// Updates the template arguments (symbols and user types). /// </summary> /// <param name="factory">The user type factory.</param> /// <returns><c>true</c> if all template arguments are resolved as user types.</returns> public bool UpdateTemplateArguments(UserTypeFactory factory) { bool result = true; templateArgumentsAsSymbols = ParseTemplateArguments(factory, Module, Symbol.Namespaces.Last()).ToList(); templateArgumentsAsUserTypes.Clear(); foreach (Symbol symbol in templateArgumentsAsSymbols) { // Try to get user type for the symbol UserType specializationUserType = null; if (!factory.GetUserType(symbol, out specializationUserType)) { if (symbol.Tag != CodeTypeTag.Enum && symbol.Tag != CodeTypeTag.Class && symbol.Tag != CodeTypeTag.Structure && symbol.Tag != CodeTypeTag.Union) { try { var typeString = GetSymbolTypeTree(symbol, factory).GetTypeString(); specializationUserType = new TemplateArgumentUserType(typeString, symbol); } catch { } } } templateArgumentsAsUserTypes.Add(specializationUserType); result = result && specializationUserType != null; } // Enumerate all template arguments as strings IEnumerable <Symbol> allTemplateArguments = Enumerable.Empty <Symbol>(); foreach (string symbolName in Symbol.Namespaces) { allTemplateArguments = allTemplateArguments.Concat(ParseTemplateArguments(factory, Module, symbolName)); } AllTemplateArguments = allTemplateArguments.Select(s => s.Name).ToList(); // TODO: Unused types should be removed return(result); }
/// <summary> /// Extracts all fields from the user type. /// </summary> /// <param name="factory">The user type factory.</param> /// <param name="generationFlags">The user type generation flags.</param> protected override IEnumerable <UserTypeField> ExtractFields(UserTypeFactory factory, UserTypeGenerationFlags generationFlags) { ExportStaticFields = true; var fields = Symbol.Fields.OrderBy(s => s.Name).ToArray(); bool useThisClass = generationFlags.HasFlag(UserTypeGenerationFlags.UseClassFieldsFromDiaSymbolProvider); string previousName = ""; foreach (var field in fields) { if (string.IsNullOrEmpty(field.Type.Name)) { continue; } if (IsFieldFiltered(field) || field.Name == previousName) { continue; } if (field.Name.Contains("@")) { // Skip names contaings '@' continue; } // Skip fields that have same name as the type UserType userType; factory.GetUserType(field.Type.Module, field.Type.Name, out userType); if (userType == null) { continue; } // Skip fields that are actual values of enum values if (field.Type.Tag == Dia2Lib.SymTagEnum.SymTagEnum && field.Type.GetEnumValues().Any(t => t.Item1 == field.Name)) { continue; } var userField = ExtractField(field, factory, generationFlags, forceIsStatic: true); if (field.Type.Tag == Dia2Lib.SymTagEnum.SymTagPointerType) { // Do not use const values for pointers. // We do not allow user type implicit conversion from integers. userField.ConstantValue = string.Empty; } userField.FieldName = userField.FieldName.Replace("?", "_").Replace("$", "_").Replace("@", "_").Replace(":", "_").Replace(" ", "_").Replace("<", "_").Replace(">", "_").Replace("*", "_").Replace(",", "_"); userField.PropertyName = userField.PropertyName.Replace("?", "_").Replace("$", "_").Replace("@", "_").Replace(":", "_").Replace(" ", "_").Replace("<", "_").Replace(">", "_").Replace("*", "_").Replace(",", "_"); yield return(userField); previousName = field.Name; } foreach (var field in GetAutoGeneratedFields(false, useThisClass)) { yield return(field); } }
/// <summary> /// Updates the template arguments (symbols and user types). /// </summary> /// <param name="factory">The user type factory.</param> /// <returns><c>true</c> if all template arguments are resolved as user types.</returns> public bool UpdateTemplateArguments(UserTypeFactory factory) { bool result = true; templateArgumentsAsSymbols = ParseTemplateArguments(factory, Module, Symbol.Namespaces.Last()).ToList(); templateArgumentsAsUserTypes.Clear(); foreach (Symbol symbol in templateArgumentsAsSymbols) { // Try to get user type for the symbol UserType specializationUserType = null; if (!factory.GetUserType(symbol, out specializationUserType)) { if (symbol.Tag != Dia2Lib.SymTagEnum.SymTagEnum && symbol.Tag != Dia2Lib.SymTagEnum.SymTagUDT) { var typeString = GetSymbolTypeTree(symbol, factory).GetTypeString(); specializationUserType = new TemplateArgumentUserType(typeString, symbol); } } templateArgumentsAsUserTypes.Add(specializationUserType); result = result && specializationUserType != null; } // Enumerate all template arguments as strings IEnumerable<Symbol> allTemplateArguments = Enumerable.Empty<Symbol>(); foreach (string symbolName in Symbol.Namespaces) { allTemplateArguments = allTemplateArguments.Concat(ParseTemplateArguments(factory, Module, symbolName)); } AllTemplateArguments = allTemplateArguments.Select(s => s.Name).ToList(); // TODO: Unused types should be removed return result; }
/// <summary> /// Initializes a new instance of the <see cref="TemplateTypeTree"/> class. /// </summary> /// <param name="templateSpecialization">The template specialization user type.</param> /// <param name="factory">The user type factory.</param> public TemplateTypeTree(UserType templateSpecialization, UserTypeFactory factory) : base(templateSpecialization) { // Get all "parent" types UserType type = templateSpecialization; List<UserType> declaredInList = new List<UserType>(); while (type != null) { declaredInList.Add(type); type = type.DeclaredInType; } declaredInList.Reverse(); DeclaredInTypeHierarchy = declaredInList.ToArray(); // Extract all template types and check if we can instantiate this instance CanInstantiate = true; SpecializedArguments = new TypeTree[DeclaredInTypeHierarchy.Length][]; for (int j = 0; j < DeclaredInTypeHierarchy.Length; j++) { // Check if current type in hierarchy is template type TemplateUserType templateType = DeclaredInTypeHierarchy[j] as TemplateUserType; if (templateType == null) continue; // Try to find specialized arguments for template type IReadOnlyList<Symbol> arguments = templateType.TemplateArgumentsAsSymbols; TypeTree[] specializedArguments = new TypeTree[arguments.Count]; for (int i = 0; i < arguments.Count; i++) { UserType userType; factory.GetUserType(arguments[i], out userType); if (userType != null) { specializedArguments[i] = UserTypeTree.Create(userType, factory); TemplateTypeTree templateTypeTree = specializedArguments[i] as TemplateTypeTree; if (templateTypeTree != null && !templateTypeTree.CanInstantiate) CanInstantiate = false; } else { // TODO: Check why do we go one more round trip through module for getting argument symbol Symbol symbol = templateSpecialization.Symbol.Module.GetSymbol(arguments[i].Name); if (symbol.Tag != SymTagEnum.SymTagBaseType) { // Base Types (Primitive Types) can be used for specialization CanInstantiate = false; } // #fixme can't deal with it specializedArguments[i] = templateType.GetSymbolTypeTree(arguments[i], factory); } } SpecializedArguments[j] = specializedArguments; } }
/// <summary> /// Updates the template arguments (symbols and user types). /// </summary> /// <param name="factory">The user type factory.</param> /// <returns><c>true</c> if all template arguments are resolved as user types.</returns> public bool UpdateTemplateArguments(UserTypeFactory factory) { string symbolName = Symbol.Namespaces.Last(); int templateStart = symbolName.IndexOf('<'); bool result = true; templateArgumentsAsSymbols.Clear(); templateArgumentsAsUserTypes.Clear(); if (templateStart > 0) { // Parse template arguments List <string> arguments = new List <string>(); for (int i = templateStart + 1; i < symbolName.Length && symbolName[i] != '>'; i++) { string originalyExtractedType = XmlTypeTransformation.ExtractType(symbolName, i); string extractedType = originalyExtractedType.Trim(); i += originalyExtractedType.Length; if (string.IsNullOrEmpty(extractedType)) { // This can happen only when list is empty if (arguments.Count > 0) { throw new NotImplementedException("Unexpected empty template argument in symbol " + symbolName); } break; } arguments.Add(extractedType); // Try to see if argument is number (constants are removed from the template arguments as they cannot be used in C#) double constant; if (!double.TryParse(extractedType, out constant)) { // Check if type is existing type (symbol) Symbol symbol = GlobalCache.GetSymbol(extractedType, Module); if (symbol == null) { throw new Exception("Wrongly formed template argument"); } templateArgumentsAsSymbols.Add(symbol); // Try to get user type for the symbol UserType specializationUserType = null; if (!factory.GetUserType(symbol, out specializationUserType)) { if (symbol.Tag != Dia2Lib.SymTagEnum.SymTagEnum && symbol.Tag != Dia2Lib.SymTagEnum.SymTagUDT) { var typeString = GetSymbolTypeTree(symbol, factory).GetTypeString(); specializationUserType = new TemplateArgumentUserType(typeString, symbol); } } templateArgumentsAsUserTypes.Add(specializationUserType); result = result && specializationUserType != null; } } } // TODO: Unused types should be removed return(result); }