private void AddTypeSigReference(TypeSig typeSig, INameReference <IDnlibDef> reference) { foreach (var type in typeSig.FindTypeRefs()) { var typeDef = type.ResolveTypeDefThrow(); if (Context.Modules.Contains((ModuleDefMD)typeDef.Module)) { NameService.ReduceRenameMode(typeDef, RenameMode.Letters); if (type is TypeRef) { NameService.AddReference(typeDef, new TypeRefReference((TypeRef)type, typeDef)); } NameService.AddReference(typeDef, reference); } } }
private Tuple <IDnlibDef, AttributeInfoRecord, TypeDef> AnalyzeAttributeReference(TypeDef declType, AttributeInfoRecord rec) { IDnlibDef retDef = null; ITypeDefOrRef retType = null; while (declType != null) { var property = declType.FindProperty(rec.Name); if (property != null) { retDef = property; retType = property.PropertySig.RetType.ToBasicTypeDefOrRef(); if (Context.Modules.Contains((ModuleDefMD)declType.Module)) { NameService.AddReference(property, new BAMLAttributeReference(property, rec)); } break; } var evt = declType.FindEvent(rec.Name); if (evt != null) { retDef = evt; retType = evt.EventType; if (Context.Modules.Contains((ModuleDefMD)declType.Module)) { NameService.AddReference(evt, new BAMLAttributeReference(evt, rec)); } break; } if (declType.BaseType == null) { break; } declType = declType.BaseType.ResolveTypeDefThrow(); } return(Tuple.Create(retDef, rec, retType == null ? null : retType.ResolveTypeDefThrow())); }
private void ProcessConverter(PropertyWithConverterRecord rec, TypeDef type) { var converter = ResolveType(rec.ConverterTypeId); if (converter.FullName == "System.ComponentModel.EnumConverter") { if (type != null && Context.Modules.Contains((ModuleDefMD)type.Module)) { var enumField = type.FindField(rec.Value); if (enumField != null) { NameService.AddReference(enumField, new BAMLEnumReference(enumField, rec)); } } } else if (converter.FullName == "System.Windows.Input.CommandConverter") { var cmd = rec.Value.Trim(); var index = cmd.IndexOf('.'); if (index != -1) { var typeName = cmd.Substring(0, index); string prefix; var sig = ResolveType(typeName, out prefix); if (sig != null) { var cmdName = cmd.Substring(index + 1); var typeDef = sig.ToBasicTypeDefOrRef().ResolveTypeDefThrow(); if (Context.Modules.Contains((ModuleDefMD)typeDef.Module)) { var property = typeDef.FindProperty(cmdName); if (property != null) { var reference = new BAMLConverterMemberReference(xmlnsCtx, sig, property, rec); AddTypeSigReference(sig, reference); NameService.ReduceRenameMode(property, RenameMode.Letters); NameService.AddReference(property, reference); } var field = typeDef.FindField(cmdName); if (field != null) { var reference = new BAMLConverterMemberReference(xmlnsCtx, sig, field, rec); AddTypeSigReference(sig, reference); NameService.ReduceRenameMode(field, RenameMode.Letters); NameService.AddReference(field, reference); } if (property == null && field == null) { Context.Logger.WarnFormat("Could not resolve command '{0}' in '{1}'.", cmd, CurrentBAMLName); } } } } } else if (converter.FullName == "System.Windows.Markup.DependencyPropertyConverter") { // Umm... Again nothing to do, DP already won't be renamed. } else if (converter.FullName == "System.Windows.PropertyPathConverter") { AnalyzePropertyPath(rec.Value); } else if (converter.FullName == "System.Windows.Markup.RoutedEventConverter") { ; } else if (converter.FullName == "System.Windows.Markup.TypeTypeConverter") { string prefix; var sig = ResolveType(rec.Value.Trim(), out prefix); if (sig != null && Context.Modules.Contains((ModuleDefMD)sig.ToBasicTypeDefOrRef().ResolveTypeDefThrow().Module)) { var reference = new BAMLConverterTypeReference(xmlnsCtx, sig, rec); AddTypeSigReference(sig, reference); } } var attrInfo = ResolveAttribute(rec.AttributeId); string attrName = null; if (attrInfo.Item1 != null) { attrName = attrInfo.Item1.Name; } else if (attrInfo.Item2 != null) { attrName = attrInfo.Item2.Name; } if (attrName == "DisplayMemberPath") { AnalyzePropertyPath(rec.Value); } else if (attrName == "Source") { string declType = null; if (attrInfo.Item1 is IMemberDef) { declType = ((IMemberDef)attrInfo.Item1).DeclaringType.FullName; } else if (attrInfo.Item2 != null) { declType = ResolveType(attrInfo.Item2.OwnerTypeId).FullName; } if (declType == "System.Windows.ResourceDictionary") { var src = rec.Value.ToUpperInvariant(); if (src.EndsWith(".BAML") || src.EndsWith(".XAML")) { var match = WPFAnalyzer.UriPattern.Match(src); if (match.Success) { src = match.Groups[1].Value; } else if (rec.Value.Contains("/")) { Context.Logger.WarnFormat("Fail to extract XAML name from '{0}'.", rec.Value); } if (!src.Contains("//")) { var rel = new Uri(new Uri(packScheme + "application:,,,/" + CurrentBAMLName), src); src = rel.LocalPath; } var reference = new BAMLPropertyReference(rec); src = src.TrimStart('/'); var baml = src.Substring(0, src.Length - 5) + ".BAML"; var xaml = src.Substring(0, src.Length - 5) + ".XAML"; var bamlRefs = NameService.FindRenamer <WPFAnalyzer>().bamlRefs; bamlRefs.AddListEntry(baml, reference); bamlRefs.AddListEntry(xaml, reference); bamlRefs.AddListEntry(Uri.EscapeUriString(baml), reference); bamlRefs.AddListEntry(Uri.EscapeUriString(xaml), reference); } } } }
private void ProcessElementBody(BamlElement root, BamlElement elem) { foreach (var rec in elem.Body) { // Resolve the type & property for simple property record too. TypeDef type = null; IDnlibDef attr = null; if (rec is PropertyRecord) { var propRec = (PropertyRecord)rec; var attrInfo = ResolveAttribute(propRec.AttributeId); type = attrInfo.Item3; attr = attrInfo.Item1; if (attr != null) { type = GetAttributeType(attr); } if (attrInfo.Item1 is EventDef) { var method = root.Type.FindMethod(propRec.Value); if (method == null) { Context.Logger.WarnFormat("Cannot resolve method '{0}' in '{1}'.", root.Type.FullName, propRec.Value); } else { var reference = new BAMLAttributeReference(method, propRec); NameService.AddReference(method, reference); } } if (rec is PropertyWithConverterRecord) { ProcessConverter((PropertyWithConverterRecord)rec, type); } } else if (rec is PropertyComplexStartRecord) { var attrInfo = ResolveAttribute(((PropertyComplexStartRecord)rec).AttributeId); type = attrInfo.Item3; attr = attrInfo.Item1; if (attr != null) { type = GetAttributeType(attr); } } else if (rec is ContentPropertyRecord) { var attrInfo = ResolveAttribute(((ContentPropertyRecord)rec).AttributeId); type = attrInfo.Item3; attr = attrInfo.Item1; if (elem.Attribute != null && attr != null) { type = GetAttributeType(attr); } foreach (var child in elem.Children) { child.Type = type; child.Attribute = attr; } } else if (rec is PropertyCustomRecord) { var customRec = (PropertyCustomRecord)rec; var attrInfo = ResolveAttribute(customRec.AttributeId); type = attrInfo.Item3; attr = attrInfo.Item1; if (elem.Attribute != null && attr != null) { type = GetAttributeType(attr); } if ((customRec.SerializerTypeId & ~0x4000) != 0 && (customRec.SerializerTypeId & ~0x4000) == 0x89) { // See BamlRecordReader.GetCustomDependencyPropertyValue. // Umm... Well, actually nothing to do, since this record only describe DP, which already won't be renamed. } } else if (rec is PropertyWithExtensionRecord) { var extRec = (PropertyWithExtensionRecord)rec; var attrInfo = ResolveAttribute(extRec.AttributeId); type = attrInfo.Item3; attr = attrInfo.Item1; if (elem.Attribute != null && attr != null) { type = GetAttributeType(attr); } if (extRec.Flags == 602) { if ((short)extRec.ValueId >= 0) { attrInfo = ResolveAttribute(extRec.ValueId); var attrTarget = attrInfo.Item1; if (attrTarget == null) { TypeSig declType; TypeDef declTypeDef; if (typeRefs.TryGetValue(attrInfo.Item2.OwnerTypeId, out declType)) { declTypeDef = declType.ToBasicTypeDefOrRef().ResolveTypeDefThrow(); } else { Debug.Assert((short)attrInfo.Item2.OwnerTypeId < 0); declTypeDef = things.Types((KnownTypes)(-(short)attrInfo.Item2.OwnerTypeId)); } attrTarget = declTypeDef.FindField(attrInfo.Item2.Name); } if (attrTarget != null) { NameService.AddReference(attrTarget, new BAMLAttributeReference(attrTarget, attrInfo.Item2)); } } } } else if (rec is TextRecord) { var txt = (TextRecord)rec; var value = txt.Value; if (txt is TextWithIdRecord) { value = strings[((TextWithIdRecord)txt).ValueId].Value; } string prefix; var sig = ResolveType(value.Trim(), out prefix); if (sig != null && Context.Modules.Contains((ModuleDefMD)sig.ToBasicTypeDefOrRef().ResolveTypeDefThrow().Module)) { var reference = new BAMLConverterTypeReference(xmlnsCtx, sig, txt); AddTypeSigReference(sig, reference); } else { AnalyzePropertyPath(value); } } } }