public override void WriteTo(ITextOutput output, MemberMapping memberMapping) { var startLoc = output.Location; if (IsFilter) { output.Write("filter", TextTokenType.Keyword); output.WriteSpace(); output.WriteReference(ExceptionVariable.Name, ExceptionVariable, TextTokenType.Local); } else if (ExceptionType != null) { output.Write("catch", TextTokenType.Keyword); output.WriteSpace(); output.WriteReference(ExceptionType.FullName, ExceptionType, TextTokenHelper.GetTextTokenType(ExceptionType)); if (ExceptionVariable != null) { output.WriteSpace(); output.WriteReference(ExceptionVariable.Name, ExceptionVariable, TextTokenType.Local); } } else { output.Write("handler", TextTokenType.Keyword); output.WriteSpace(); output.WriteReference(ExceptionVariable.Name, ExceptionVariable, TextTokenType.Local); } UpdateMemberMapping(memberMapping, startLoc, output.Location, StlocILRanges); output.WriteSpace(); base.WriteTo(output, memberMapping); }
public static ITextOutput Write(ITextOutput output, MethodDef method, Language language) { output.Write(CleanUpIdentifier(method.Name), TextTokenHelper.GetTextTokenType(method)); output.Write('(', TextTokenType.Operator); for (int i = 0; i < method.Parameters.Count; i++) { if (method.Parameters[i].IsHiddenThisParameter) { continue; } if (method.Parameters[i].MethodSigIndex > 0) { output.Write(',', TextTokenType.Operator); output.WriteSpace(); } language.TypeToString(output, method.Parameters[i].Type.ToTypeDefOrRef(), false, method.Parameters[i].ParamDef); } if (method.CallingConvention == CallingConvention.VarArg || method.CallingConvention == CallingConvention.NativeVarArg) { if (method.MethodSig.GetParamCount() > 0) { output.Write(',', TextTokenType.Operator); output.WriteSpace(); } output.Write("...", TextTokenType.Operator); } output.Write(')', TextTokenType.Operator); output.WriteSpace(); output.Write(':', TextTokenType.Operator); output.WriteSpace(); language.TypeToString(output, method.ReturnType.ToTypeDefOrRef(), false, method.Parameters.ReturnParameter.ParamDef); method.MDToken.WriteSuffixString(output); return(output); }
public virtual void FormatPropertyName(ITextOutput output, PropertyDef property, bool?isIndexer = null) { if (property == null) { throw new ArgumentNullException("property"); } output.Write(IdentifierEscaper.Escape(property.Name), TextTokenHelper.GetTextTokenType(property)); }
public virtual void FormatTypeName(ITextOutput output, TypeDef type) { if (type == null) { throw new ArgumentNullException("type"); } output.Write(IdentifierEscaper.Escape(type.Name), TextTokenHelper.GetTextTokenType(type)); }
public override void WriteIdentifier(Identifier identifier, TextTokenType tokenType) { if (tokenType == TextTokenType.Text) { tokenType = TextTokenHelper.GetTextTokenType(identifier.AnnotationVT <TextTokenType>() ?? identifier.Annotation <object>()); } if (tokenType != TextTokenType.Keyword && (identifier.IsVerbatim || CSharpOutputVisitor.IsKeyword(identifier.Name, identifier))) { output.Write('@', TextTokenType.Operator); } var definition = GetCurrentDefinition(identifier); if (definition != null) { output.WriteDefinition(IdentifierEscaper.Escape(identifier.Name), definition, tokenType, false); return; } object memberRef = GetCurrentMemberReference(); if (memberRef != null) { output.WriteReference(IdentifierEscaper.Escape(identifier.Name), memberRef, tokenType); return; } definition = GetCurrentLocalDefinition(); if (definition != null) { output.WriteDefinition(IdentifierEscaper.Escape(identifier.Name), definition, tokenType); return; } memberRef = GetCurrentLocalReference(); if (memberRef != null) { output.WriteReference(IdentifierEscaper.Escape(identifier.Name), memberRef, tokenType, true); return; } if (firstUsingDeclaration) { output.MarkFoldStart(defaultCollapsed: true); firstUsingDeclaration = false; } var s = identifier.Name; if (identifier.Annotation <IdentifierFormatted>() == null) { s = IdentifierEscaper.Escape(s); } output.Write(s, tokenType); }
void StartKeywordBlock(ITextOutput output, string keyword, IMemberDef member) { output.Write(keyword, TextTokenType.Keyword); output.WriteSpace(); output.WriteDefinition(IdentifierEscaper.Escape(member.Name), member, TextTokenHelper.GetTextTokenType(member), false); output.WriteSpace(); output.WriteLeftBrace(); output.WriteLine(); output.Indent(); }
public static ITextOutput Write(ITextOutput output, EventDef ev, Language language) { output.Write(UIUtils.CleanUpIdentifier(ev.Name), TextTokenHelper.GetTextTokenType(ev)); output.WriteSpace(); output.Write(':', TextTokenType.Operator); output.WriteSpace(); language.TypeToString(output, ev.EventType, false, ev); ev.MDToken.WriteSuffixString(output); return(output); }
public static ITextOutput Write(ITextOutput output, FieldDef field, Language language) { output.Write(UIUtils.CleanUpIdentifier(field.Name), TextTokenHelper.GetTextTokenType(field)); output.WriteSpace(); output.Write(':', TextTokenType.Operator); output.WriteSpace(); language.TypeToString(output, field.FieldType.ToTypeDefOrRef(), false, field); field.MDToken.WriteSuffixString(output); return(output); }
public static AstType Create(Type type) { switch (Type.GetTypeCode(type)) { case TypeCode.Object: return(new PrimitiveType("Object")); case TypeCode.Boolean: return(new PrimitiveType("Boolean")); case TypeCode.Char: return(new PrimitiveType("Char")); case TypeCode.SByte: return(new PrimitiveType("SByte")); case TypeCode.Byte: return(new PrimitiveType("Byte")); case TypeCode.Int16: return(new PrimitiveType("Short")); case TypeCode.UInt16: return(new PrimitiveType("UShort")); case TypeCode.Int32: return(new PrimitiveType("Integer")); case TypeCode.UInt32: return(new PrimitiveType("UInteger")); case TypeCode.Int64: return(new PrimitiveType("Long")); case TypeCode.UInt64: return(new PrimitiveType("ULong")); case TypeCode.Single: return(new PrimitiveType("Single")); case TypeCode.Double: return(new PrimitiveType("Double")); case TypeCode.Decimal: return(new PrimitiveType("Decimal")); case TypeCode.String: return(new PrimitiveType("String")); case TypeCode.DateTime: return(new PrimitiveType("Date")); } return(new SimpleType(TextTokenHelper.GetTextTokenType(type), type.FullName)); // TODO: implement this correctly }
public static void WriteFieldTo(this IField field, ITextOutput writer) { if (field == null || field.FieldSig == null) { return; } field.FieldSig.Type.WriteTo(writer, ILNameSyntax.SignatureNoNamedTypeParameters); writer.WriteSpace(); field.DeclaringType.WriteTo(writer, ILNameSyntax.TypeName); writer.Write("::", TextTokenType.Operator); writer.WriteReference(Escape(field.Name), field, TextTokenHelper.GetTextTokenType(field)); }
public virtual void TypeToString(ITextOutput output, ITypeDefOrRef type, bool includeNamespace, IHasCustomAttribute typeAttributes = null) { if (type == null) { return; } if (includeNamespace) { output.Write(IdentifierEscaper.Escape(type.FullName), TextTokenHelper.GetTextTokenType(type)); } else { output.Write(IdentifierEscaper.Escape(type.Name), TextTokenHelper.GetTextTokenType(type)); } }
public override void WriteIdentifier(Identifier identifier, TextTokenType tokenType) { if (tokenType == TextTokenType.Text) { tokenType = TextTokenHelper.GetTextTokenType(identifier.AnnotationVT <TextTokenType>() ?? identifier.Annotation <object>()); } var definition = GetCurrentDefinition(identifier); if (definition != null) { output.WriteDefinition(IdentifierEscaper.Escape(identifier.Name), definition, tokenType, false); return; } object memberRef = GetCurrentMemberReference(); if (memberRef != null) { output.WriteReference(IdentifierEscaper.Escape(identifier.Name), memberRef, tokenType); return; } definition = GetCurrentLocalDefinition(); if (definition != null) { output.WriteDefinition(IdentifierEscaper.Escape(identifier.Name), definition, tokenType); return; } memberRef = GetCurrentLocalReference(); if (memberRef != null) { output.WriteReference(IdentifierEscaper.Escape(identifier.Name), memberRef, tokenType, true); return; } if (firstUsingDeclaration) { output.MarkFoldStart(defaultCollapsed: true); firstUsingDeclaration = false; } output.Write(IdentifierEscaper.Escape(identifier.Name), tokenType); }
/// <summary> /// Converts a member signature to a string. /// This is used for displaying the tooltip on a member reference. /// </summary> public virtual void WriteToolTip(ITextOutput output, IMemberRef member, IHasCustomAttribute typeAttributes) { if (member is ITypeDefOrRef) { TypeToString(output, (ITypeDefOrRef)member, true, typeAttributes); } else if (member is GenericParam) { var gp = (GenericParam)member; output.Write(IdentifierEscaper.Escape(gp.Name), TextTokenHelper.GetTextTokenType(gp)); output.WriteSpace(); output.Write("in", TextTokenType.Text); output.WriteSpace(); WriteToolTip(output, gp.Owner, typeAttributes); } else { //TODO: This should be escaped but since it contains whitespace, parens, etc, // we can't pass it to IdentifierEscaper.Escape(). output.Write(member.ToString(), TextTokenHelper.GetTextTokenType(member)); } }
public IEnumerable <NRefactory.VB.Ast.InterfaceMemberSpecifier> CreateMemberSpecifiersForInterfaces(IEnumerable <NRefactory.VB.Ast.AstType> interfaces) { foreach (var type in interfaces) { var def = type.Annotation <ITypeDefOrRef>().ResolveTypeDef(); if (def == null) { continue; } foreach (var method in def.Methods.Where(m => !m.Name.StartsWith("get_") && !m.Name.StartsWith("set_"))) { yield return(new NRefactory.VB.Ast.InterfaceMemberSpecifier((NRefactory.VB.Ast.AstType)type.Clone(), method.Name, TextTokenHelper.GetTextTokenType(method))); } foreach (var property in def.Properties) { yield return(new NRefactory.VB.Ast.InterfaceMemberSpecifier((NRefactory.VB.Ast.AstType)type.Clone(), property.Name, TextTokenHelper.GetTextTokenType(property))); } } }
void CreateUI(ITextOutput output, object o, bool includeNamespace) { var ns = o as NamespaceSearchResult; if (ns != null) { output.WriteNamespace(ns.Namespace); return; } var td = o as TypeDef; if (td != null) { Debug.Assert(Language != null); Language.TypeToString(output, td, includeNamespace); return; } var md = o as MethodDef; if (md != null) { output.Write(IdentifierEscaper.Escape(md.Name), TextTokenHelper.GetTextTokenType(md)); return; } var fd = o as FieldDef; if (fd != null) { output.Write(IdentifierEscaper.Escape(fd.Name), TextTokenHelper.GetTextTokenType(fd)); return; } var pd = o as PropertyDef; if (pd != null) { output.Write(IdentifierEscaper.Escape(pd.Name), TextTokenHelper.GetTextTokenType(pd)); return; } var ed = o as EventDef; if (ed != null) { output.Write(IdentifierEscaper.Escape(ed.Name), TextTokenHelper.GetTextTokenType(ed)); return; } var asm = o as AssemblyDef; if (asm != null) { output.Write(asm); return; } var mod = o as ModuleDef; if (mod != null) { output.WriteModule(mod.FullName); return; } var asmRef = o as AssemblyRef; if (asmRef != null) { output.Write(asmRef); return; } var modRef = o as ModuleRef; if (modRef != null) { output.WriteModule(modRef.FullName); return; } // non-.NET file var file = o as DnSpyFile; if (file != null) { output.Write(file.ShortName, TextTokenType.Text); return; } var resNode = o as ResourceTreeNode; if (resNode != null) { output.WriteFilename(resNode.Name); return; } var resElNode = o as ResourceElementTreeNode; if (resElNode != null) { output.WriteFilename(resElNode.Name); return; } var s = o as string; if (s != null) { output.Write(s, TextTokenType.Text); return; } Debug.Assert(s == null); }
public static void WriteOperand(ITextOutput writer, object operand, MethodDef method = null) { Instruction targetInstruction = operand as Instruction; if (targetInstruction != null) { WriteOffsetReference(writer, targetInstruction, method); return; } IList <Instruction> targetInstructions = operand as IList <Instruction>; if (targetInstructions != null) { WriteLabelList(writer, targetInstructions, method); return; } Local variable = operand as Local; if (variable != null) { if (string.IsNullOrEmpty(variable.Name)) { writer.WriteReference(variable.Index.ToString(), variable, TextTokenType.Number); } else { writer.WriteReference(Escape(variable.Name), variable, TextTokenType.Local); } return; } Parameter paramRef = operand as Parameter; if (paramRef != null) { if (string.IsNullOrEmpty(paramRef.Name)) { if (paramRef.IsHiddenThisParameter) { writer.WriteReference("<hidden-this>", paramRef, TextTokenType.Parameter); } else { writer.WriteReference(paramRef.MethodSigIndex.ToString(), paramRef, TextTokenType.Parameter); } } else { writer.WriteReference(Escape(paramRef.Name), paramRef, TextTokenType.Parameter); } return; } MemberRef memberRef = operand as MemberRef; if (memberRef != null) { if (memberRef.IsMethodRef) { memberRef.WriteMethodTo(writer); } else { memberRef.WriteFieldTo(writer); } return; } MethodDef methodDef = operand as MethodDef; if (methodDef != null) { methodDef.WriteMethodTo(writer); return; } FieldDef fieldDef = operand as FieldDef; if (fieldDef != null) { fieldDef.WriteFieldTo(writer); return; } ITypeDefOrRef typeRef = operand as ITypeDefOrRef; if (typeRef != null) { typeRef.WriteTo(writer, ILNameSyntax.TypeName); return; } IMethod m = operand as IMethod; if (m != null) { m.WriteMethodTo(writer); return; } MethodSig sig = operand as MethodSig; if (sig != null) { sig.WriteTo(writer); return; } string s = operand as string; if (s != null) { writer.Write("\"" + NRefactory.CSharp.TextWriterTokenWriter.ConvertString(s) + "\"", TextTokenType.String); } else if (operand is char) { writer.Write(((int)(char)operand).ToString(), TextTokenType.Number); } else if (operand is float) { float val = (float)operand; if (val == 0) { if (1 / val == float.NegativeInfinity) { // negative zero is a special case writer.Write("-0.0", TextTokenType.Number); } else { writer.Write("0.0", TextTokenType.Number); } } else if (float.IsInfinity(val) || float.IsNaN(val)) { byte[] data = BitConverter.GetBytes(val); writer.Write('(', TextTokenType.Operator); for (int i = 0; i < data.Length; i++) { if (i > 0) { writer.WriteSpace(); } writer.Write(data[i].ToString("X2"), TextTokenType.Number); } writer.Write(')', TextTokenType.Operator); } else { writer.Write(val.ToString("R", System.Globalization.CultureInfo.InvariantCulture), TextTokenType.Number); } } else if (operand is double) { double val = (double)operand; if (val == 0) { if (1 / val == double.NegativeInfinity) { // negative zero is a special case writer.Write("-0.0", TextTokenType.Number); } else { writer.Write("0.0", TextTokenType.Number); } } else if (double.IsInfinity(val) || double.IsNaN(val)) { byte[] data = BitConverter.GetBytes(val); writer.Write('(', TextTokenType.Operator); for (int i = 0; i < data.Length; i++) { if (i > 0) { writer.WriteSpace(); } writer.Write(data[i].ToString("X2"), TextTokenType.Number); } writer.Write(')', TextTokenType.Operator); } else { writer.Write(val.ToString("R", System.Globalization.CultureInfo.InvariantCulture), TextTokenType.Number); } } else if (operand is bool) { writer.Write((bool)operand ? "true" : "false", TextTokenType.Keyword); } else { s = ToInvariantCultureString(operand); writer.Write(s, TextTokenHelper.GetTextTokenType(operand)); } }
public static void WriteTo(this ITypeDefOrRef type, ITextOutput writer, ILNameSyntax syntax, int depth) { if (depth++ > MAX_CONVERTTYPE_DEPTH || type == null) { return; } var ts = type as TypeSpec; if (ts != null && !(ts.TypeSig is FnPtrSig)) { WriteTo(((TypeSpec)type).TypeSig, writer, syntax, depth); return; } string typeFullName = type.FullName; string typeName = type.Name.String; if (ts != null) { var fnPtrSig = ts.TypeSig as FnPtrSig; typeFullName = DnlibExtensions.GetFnPtrFullName(fnPtrSig); typeName = DnlibExtensions.GetFnPtrName(fnPtrSig); } TypeSig typeSig = null; string name = type.DefinitionAssembly.IsCorLib() ? PrimitiveTypeName(typeFullName, type.Module, out typeSig) : null; if (syntax == ILNameSyntax.ShortTypeName) { if (name != null) { WriteKeyword(writer, name, typeSig.ToTypeDefOrRef()); } else { writer.WriteReference(Escape(typeName), type, TextTokenHelper.GetTextTokenType(type)); } } else if ((syntax == ILNameSyntax.Signature || syntax == ILNameSyntax.SignatureNoNamedTypeParameters) && name != null) { WriteKeyword(writer, name, typeSig.ToTypeDefOrRef()); } else { if (syntax == ILNameSyntax.Signature || syntax == ILNameSyntax.SignatureNoNamedTypeParameters) { writer.Write(DnlibExtensions.IsValueType(type) ? "valuetype" : "class", TextTokenType.Keyword); writer.WriteSpace(); } if (type.DeclaringType != null) { type.DeclaringType.WriteTo(writer, ILNameSyntax.TypeName, depth); writer.Write('/', TextTokenType.Operator); writer.WriteReference(Escape(typeName), type, TextTokenHelper.GetTextTokenType(type)); } else { if (!(type is TypeDef) && type.Scope != null && !(type is TypeSpec)) { writer.Write('[', TextTokenType.Operator); writer.Write(Escape(type.Scope.GetScopeName()), TextTokenType.ILModule); writer.Write(']', TextTokenType.Operator); } if (ts != null || MustEscape(typeFullName)) { writer.WriteReference(Escape(typeFullName), type, TextTokenHelper.GetTextTokenType(type)); } else { WriteNamespace(writer, type.Namespace); if (!string.IsNullOrEmpty(type.Namespace)) { writer.Write('.', TextTokenType.Operator); } writer.WriteReference(IdentifierEscaper.Escape(type.Name), type, TextTokenHelper.GetTextTokenType(type)); } } } }
public static void WriteTo(this TypeSig type, ITextOutput writer, ILNameSyntax syntax, int depth) { if (depth++ > MAX_CONVERTTYPE_DEPTH) { return; } ILNameSyntax syntaxForElementTypes = syntax == ILNameSyntax.SignatureNoNamedTypeParameters ? syntax : ILNameSyntax.Signature; if (type is PinnedSig) { ((PinnedSig)type).Next.WriteTo(writer, syntaxForElementTypes, depth); writer.WriteSpace(); writer.Write("pinned", TextTokenType.Keyword); } else if (type is ArraySig) { ArraySig at = (ArraySig)type; at.Next.WriteTo(writer, syntaxForElementTypes, depth); writer.Write('[', TextTokenType.Operator); for (int i = 0; i < at.Rank; i++) { if (i != 0) { writer.Write(',', TextTokenType.Operator); writer.WriteSpace(); } int? lower = i < at.LowerBounds.Count ? at.LowerBounds[i] : (int?)null; uint?size = i < at.Sizes.Count ? at.Sizes[i] : (uint?)null; if (lower != null) { writer.Write(lower.ToString(), TextTokenType.Number); if (size != null) { writer.Write("..", TextTokenType.Operator); writer.Write((lower.Value + (int)size.Value - 1).ToString(), TextTokenType.Number); } else { writer.Write("...", TextTokenType.Operator); } } } writer.Write(']', TextTokenType.Operator); } else if (type is SZArraySig) { SZArraySig at = (SZArraySig)type; at.Next.WriteTo(writer, syntaxForElementTypes, depth); writer.Write("[]", TextTokenType.Operator); } else if (type is GenericSig) { if (((GenericSig)type).IsMethodVar) { writer.Write("!!", TextTokenType.Operator); } else { writer.Write("!", TextTokenType.Operator); } string typeName = type.TypeName; if (string.IsNullOrEmpty(typeName) || typeName[0] == '!' || syntax == ILNameSyntax.SignatureNoNamedTypeParameters) { writer.Write(((GenericSig)type).Number.ToString(), TextTokenType.Number); } else { writer.Write(Escape(typeName), TextTokenHelper.GetTextTokenType(type)); } } else if (type is ByRefSig) { ((ByRefSig)type).Next.WriteTo(writer, syntaxForElementTypes, depth); writer.Write('&', TextTokenType.Operator); } else if (type is PtrSig) { ((PtrSig)type).Next.WriteTo(writer, syntaxForElementTypes, depth); writer.Write('*', TextTokenType.Operator); } else if (type is GenericInstSig) { ((GenericInstSig)type).GenericType.WriteTo(writer, syntaxForElementTypes, depth); writer.Write('<', TextTokenType.Operator); var arguments = ((GenericInstSig)type).GenericArguments; for (int i = 0; i < arguments.Count; i++) { if (i > 0) { writer.Write(',', TextTokenType.Operator); writer.WriteSpace(); } arguments[i].WriteTo(writer, syntaxForElementTypes, depth); } writer.Write('>', TextTokenType.Operator); } else if (type is CModOptSig) { ((ModifierSig)type).Next.WriteTo(writer, syntax, depth); writer.WriteSpace(); writer.Write("modopt", TextTokenType.Keyword); writer.Write('(', TextTokenType.Operator); ((ModifierSig)type).Modifier.WriteTo(writer, ILNameSyntax.TypeName, depth); writer.Write(')', TextTokenType.Operator); writer.WriteSpace(); } else if (type is CModReqdSig) { ((ModifierSig)type).Next.WriteTo(writer, syntax, depth); writer.WriteSpace(); writer.Write("modreq", TextTokenType.Keyword); writer.Write('(', TextTokenType.Operator); ((ModifierSig)type).Modifier.WriteTo(writer, ILNameSyntax.TypeName, depth); writer.Write(')', TextTokenType.Operator); writer.WriteSpace(); } else if (type is TypeDefOrRefSig) { WriteTo(((TypeDefOrRefSig)type).TypeDefOrRef, writer, syntax, depth); } else if (type is FnPtrSig) { WriteTo(type.ToTypeDefOrRef(), writer, syntax, depth); } //TODO: SentinelSig }
public static void Write(this ITextOutput writer, MethodSig sig, IMethod method = null) { if (sig == null && method != null) { sig = method.MethodSig; } if (sig == null) { return; } if (sig.ExplicitThis) { writer.Write("instance", TextTokenType.Keyword); writer.WriteSpace(); writer.Write("explicit", TextTokenType.Keyword); writer.WriteSpace(); } else if (sig.HasThis) { writer.Write("instance", TextTokenType.Keyword); writer.WriteSpace(); } sig.RetType.WriteTo(writer, ILNameSyntax.SignatureNoNamedTypeParameters); writer.WriteSpace(); if (method != null) { if (method.DeclaringType != null) { method.DeclaringType.WriteTo(writer, ILNameSyntax.TypeName); writer.Write("::", TextTokenType.Operator); } MethodDef md = method as MethodDef; if (md != null && md.IsCompilerControlled) { writer.WriteReference(Escape(method.Name + "$PST" + method.MDToken.ToInt32().ToString("X8")), method, TextTokenHelper.GetTextTokenType(method)); } else { writer.WriteReference(Escape(method.Name), method, TextTokenHelper.GetTextTokenType(method)); } } MethodSpec gim = method as MethodSpec; if (gim != null && gim.GenericInstMethodSig != null) { writer.Write('<', TextTokenType.Operator); for (int i = 0; i < gim.GenericInstMethodSig.GenericArguments.Count; i++) { if (i > 0) { writer.Write(',', TextTokenType.Operator); writer.WriteSpace(); } gim.GenericInstMethodSig.GenericArguments[i].WriteTo(writer); } writer.Write('>', TextTokenType.Operator); } writer.Write("(", TextTokenType.Operator); var parameters = sig.GetParameters(); for (int i = 0; i < parameters.Count; ++i) { if (i > 0) { writer.Write(',', TextTokenType.Operator); writer.WriteSpace(); } parameters[i].WriteTo(writer, ILNameSyntax.SignatureNoNamedTypeParameters); } writer.Write(")", TextTokenType.Operator); }
public override void DecompileField(FieldDef field, ITextOutput output, DecompilationOptions options) { output.WriteReference(IdentifierEscaper.Escape(field.FieldType.GetFullName()), field.FieldType.ToTypeDefOrRef(), TextTokenHelper.GetTextTokenType(field.FieldType)); output.WriteSpace(); output.WriteDefinition(IdentifierEscaper.Escape(field.Name), field, TextTokenHelper.GetTextTokenType(field), false); var c = field.Constant; if (c != null) { output.WriteSpace(); output.Write('=', TextTokenType.Operator); output.WriteSpace(); if (c.Value == null) { output.Write("null", TextTokenType.Keyword); } else { switch (c.Type) { case ElementType.Boolean: if (c.Value is bool) { output.Write((bool)c.Value ? "true" : "false", TextTokenType.Keyword); } else { goto default; } break; case ElementType.Char: output.Write(string.Format("'{0}'", c.Value), TextTokenType.Char); break; case ElementType.I1: case ElementType.U1: case ElementType.I2: case ElementType.U2: case ElementType.I4: case ElementType.U4: case ElementType.I8: case ElementType.U8: case ElementType.R4: case ElementType.R8: case ElementType.I: case ElementType.U: output.Write(string.Format("{0}", c.Value), TextTokenType.Number); break; case ElementType.String: output.Write(string.Format("{0}", c.Value), TextTokenType.String); break; default: output.Write(string.Format("{0}", c.Value), TextTokenType.Text); break; } } } }
public override void WriteTo(ITextOutput output, MemberMapping memberMapping) { var startLoc = output.Location; if (Operand is ILVariable && ((ILVariable)Operand).IsGenerated) { if (Code == ILCode.Stloc && this.InferredType == null) { output.WriteReference(((ILVariable)Operand).Name, Operand, ((ILVariable)Operand).IsParameter ? TextTokenType.Parameter : TextTokenType.Local); output.WriteSpace(); output.Write('=', TextTokenType.Operator); output.WriteSpace(); Arguments.First().WriteTo(output, null); UpdateMemberMapping(memberMapping, startLoc, output.Location, this.GetSelfAndChildrenRecursiveILRanges()); return; } else if (Code == ILCode.Ldloc) { output.WriteReference(((ILVariable)Operand).Name, Operand, ((ILVariable)Operand).IsParameter ? TextTokenType.Parameter : TextTokenType.Local); if (this.InferredType != null) { output.Write(':', TextTokenType.Operator); this.InferredType.WriteTo(output, ILNameSyntax.ShortTypeName); if (this.ExpectedType != null && this.ExpectedType.FullName != this.InferredType.FullName) { output.Write('[', TextTokenType.Operator); output.Write("exp", TextTokenType.Keyword); output.Write(':', TextTokenType.Operator); this.ExpectedType.WriteTo(output, ILNameSyntax.ShortTypeName); output.Write(']', TextTokenType.Operator); } } UpdateMemberMapping(memberMapping, startLoc, output.Location, this.GetSelfAndChildrenRecursiveILRanges()); return; } } if (this.Prefixes != null) { foreach (var prefix in this.Prefixes) { output.Write(prefix.Code.GetName() + ".", TextTokenType.OpCode); output.WriteSpace(); } } output.Write(Code.GetName(), TextTokenType.OpCode); if (this.InferredType != null) { output.Write(':', TextTokenType.Operator); this.InferredType.WriteTo(output, ILNameSyntax.ShortTypeName); if (this.ExpectedType != null && this.ExpectedType.FullName != this.InferredType.FullName) { output.Write('[', TextTokenType.Operator); output.Write("exp", TextTokenType.Keyword); output.Write(':', TextTokenType.Operator); this.ExpectedType.WriteTo(output, ILNameSyntax.ShortTypeName); output.Write(']', TextTokenType.Operator); } } else if (this.ExpectedType != null) { output.Write('[', TextTokenType.Operator); output.Write("exp", TextTokenType.Keyword); output.Write(':', TextTokenType.Operator); this.ExpectedType.WriteTo(output, ILNameSyntax.ShortTypeName); output.Write(']', TextTokenType.Operator); } output.Write('(', TextTokenType.Operator); bool first = true; if (Operand != null) { if (Operand is ILLabel) { output.WriteReference(((ILLabel)Operand).Name, Operand, TextTokenType.Label); } else if (Operand is ILLabel[]) { ILLabel[] labels = (ILLabel[])Operand; for (int i = 0; i < labels.Length; i++) { if (i > 0) { output.Write(',', TextTokenType.Operator); output.WriteSpace(); } output.WriteReference(labels[i].Name, labels[i], TextTokenType.Label); } } else if (Operand is IMethod && (Operand as IMethod).MethodSig != null) { IMethod method = (IMethod)Operand; if (method.DeclaringType != null) { method.DeclaringType.WriteTo(output, ILNameSyntax.ShortTypeName); output.Write("::", TextTokenType.Operator); } output.WriteReference(method.Name, method, TextTokenHelper.GetTextTokenType(method)); } else if (Operand is IField) { IField field = (IField)Operand; field.DeclaringType.WriteTo(output, ILNameSyntax.ShortTypeName); output.Write("::", TextTokenType.Operator); output.WriteReference(field.Name, field, TextTokenHelper.GetTextTokenType(field)); } else if (Operand is ILVariable) { var ilvar = (ILVariable)Operand; output.WriteReference(ilvar.Name, Operand, ilvar.IsParameter ? TextTokenType.Parameter : TextTokenType.Local); } else { DisassemblerHelpers.WriteOperand(output, Operand); } first = false; } foreach (ILExpression arg in this.Arguments) { if (!first) { output.Write(',', TextTokenType.Operator); output.WriteSpace(); } arg.WriteTo(output, null); first = false; } output.Write(')', TextTokenType.Operator); UpdateMemberMapping(memberMapping, startLoc, output.Location, this.GetSelfAndChildrenRecursiveILRanges()); }