Browse Source

Allow serializing SimpleProjectContent and parsed C# type references.

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
36c412d310
  1. 15
      ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeSystemConvertVisitorTests.cs
  2. 1
      ICSharpCode.NRefactory/CSharp/Parser/ParsedFile.cs
  3. 1
      ICSharpCode.NRefactory/CSharp/Resolver/AliasNamespaceReference.cs
  4. 2
      ICSharpCode.NRefactory/CSharp/Resolver/CSharpAttribute.cs
  5. 13
      ICSharpCode.NRefactory/CSharp/Resolver/ConstantValues.cs
  6. 1
      ICSharpCode.NRefactory/CSharp/Resolver/MemberTypeOrNamespaceReference.cs
  7. 1
      ICSharpCode.NRefactory/CSharp/Resolver/SimpleTypeOrNamespaceReference.cs
  8. 1
      ICSharpCode.NRefactory/CSharp/Resolver/UsingScope.cs
  9. 28
      ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs
  10. 31
      ICSharpCode.NRefactory/TypeSystem/Error.cs
  11. 1
      ICSharpCode.NRefactory/TypeSystem/IAnnotatable.cs
  12. 2
      ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs
  13. 55
      ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs
  14. 3
      ICSharpCode.NRefactory/TypeSystem/Implementation/TypeStorage.cs

15
ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeSystemConvertVisitorTests.cs

@ -51,4 +51,19 @@ namespace ICSharpCode.NRefactory.CSharp.Parser @@ -51,4 +51,19 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
return testCasePC;
}
}
[TestFixture]
public class SerializedTypeSystemConvertVisitorTests : TypeSystemTests
{
[TestFixtureSetUp]
public void FixtureSetUp()
{
FastSerializer serializer = new FastSerializer();
using (MemoryStream ms = new MemoryStream()) {
serializer.Serialize(ms, TypeSystemConvertVisitorTests.ParseTestCase());
ms.Position = 0;
testCasePC = (IProjectContent)serializer.Deserialize(ms);
}
}
}
}

1
ICSharpCode.NRefactory/CSharp/Parser/ParsedFile.cs

@ -27,6 +27,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -27,6 +27,7 @@ namespace ICSharpCode.NRefactory.CSharp
/// <summary>
/// Represents a file that was parsed and converted for the type system.
/// </summary>
[Serializable]
public sealed class ParsedFile : AbstractFreezable, IParsedFile
{
readonly string fileName;

1
ICSharpCode.NRefactory/CSharp/Resolver/AliasNamespaceReference.cs

@ -28,6 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -28,6 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// The member lookup performed by the :: operator is handled
/// by <see cref="MemberTypeOrNamespaceReference"/>.
/// </remarks>
[Serializable]
public class AliasNamespaceReference : ITypeOrNamespaceReference
{
readonly UsingScope parentUsingScope;

2
ICSharpCode.NRefactory/CSharp/Resolver/CSharpAttribute.cs

@ -24,6 +24,7 @@ using ICSharpCode.NRefactory.TypeSystem.Implementation; @@ -24,6 +24,7 @@ using ICSharpCode.NRefactory.TypeSystem.Implementation;
namespace ICSharpCode.NRefactory.CSharp.Resolver
{
[Serializable]
public sealed class CSharpAttribute : Immutable, IAttribute
{
ITypeReference attributeType;
@ -128,6 +129,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -128,6 +129,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// Type reference used within an attribute.
/// Looks up both 'withoutSuffix' and 'withSuffix' and returns the type that exists.
/// </summary>
[Serializable]
public sealed class AttributeTypeReference : ITypeReference, ISupportsInterning
{
ITypeReference withoutSuffix, withSuffix;

13
ICSharpCode.NRefactory/CSharp/Resolver/ConstantValues.cs

@ -29,6 +29,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -29,6 +29,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
// Contains representations for constant C# expressions.
// We use these instead of storing the full AST to reduce the memory usage.
[Serializable]
public sealed class CSharpConstantValue : Immutable, IConstantValue, ISupportsInterning
{
ConstantExpression expression;
@ -125,6 +126,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -125,6 +126,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
/// <summary>
/// Increments an integer <see cref="IConstantValue"/> by a fixed amount without changing the type.
/// </summary>
[Serializable]
public sealed class IncrementConstantValue : Immutable, IConstantValue, ISupportsInterning
{
IConstantValue baseValue;
@ -178,6 +180,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -178,6 +180,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
[Serializable]
public abstract class ConstantExpression
{
public abstract ResolveResult Resolve(CSharpResolver resolver);
@ -186,6 +189,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -186,6 +189,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
/// <summary>
/// C#'s equivalent to the SimpleConstantValue.
/// </summary>
[Serializable]
public sealed class PrimitiveConstantExpression : ConstantExpression, ISupportsInterning
{
ITypeReference type;
@ -233,6 +237,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -233,6 +237,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
[Serializable]
public sealed class ConstantCast : ConstantExpression, ISupportsInterning
{
ITypeReference targetType;
@ -274,6 +279,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -274,6 +279,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
[Serializable]
public sealed class ConstantIdentifierReference : ConstantExpression, ISupportsInterning
{
string identifier;
@ -327,6 +333,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -327,6 +333,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
[Serializable]
public sealed class ConstantMemberReference : ConstantExpression, ISupportsInterning
{
ITypeReference targetType;
@ -398,6 +405,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -398,6 +405,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
[Serializable]
public sealed class ConstantCheckedExpression : ConstantExpression, ISupportsInterning
{
bool checkForOverflow;
@ -441,6 +449,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -441,6 +449,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
[Serializable]
public sealed class ConstantDefaultValue : ConstantExpression, ISupportsInterning
{
ITypeReference type;
@ -474,6 +483,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -474,6 +483,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
[Serializable]
public sealed class ConstantUnaryOperator : ConstantExpression, ISupportsInterning
{
UnaryOperatorType operatorType;
@ -513,6 +523,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -513,6 +523,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
[Serializable]
public sealed class ConstantBinaryOperator : ConstantExpression, ISupportsInterning
{
ConstantExpression left;
@ -559,6 +570,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -559,6 +570,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
[Serializable]
public sealed class ConstantConditionalOperator : ConstantExpression, ISupportsInterning
{
ConstantExpression condition, trueExpr, falseExpr;
@ -614,6 +626,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -614,6 +626,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
/// <summary>
/// Represents an array creation (as used within an attribute argument)
/// </summary>
[Serializable]
public sealed class ConstantArrayCreation : ConstantExpression, ISupportsInterning
{
// type may be null when the element is being inferred

1
ICSharpCode.NRefactory/CSharp/Resolver/MemberTypeOrNamespaceReference.cs

@ -25,6 +25,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -25,6 +25,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// <summary>
/// Reference to a qualified type or namespace name.
/// </summary>
[Serializable]
public sealed class MemberTypeOrNamespaceReference : ITypeOrNamespaceReference
{
readonly ITypeOrNamespaceReference target;

1
ICSharpCode.NRefactory/CSharp/Resolver/SimpleTypeOrNamespaceReference.cs

@ -25,6 +25,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -25,6 +25,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// <summary>
/// Represents a simple C# name. (a single non-qualified identifier with an optional list of type arguments)
/// </summary>
[Serializable]
public sealed class SimpleTypeOrNamespaceReference : ITypeOrNamespaceReference
{
readonly ITypeDefinition parentTypeDefinition;

1
ICSharpCode.NRefactory/CSharp/Resolver/UsingScope.cs

@ -28,6 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -28,6 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// Represents a scope that contains "using" statements.
/// This is either the file itself, or a namespace declaration.
/// </summary>
[Serializable]
public class UsingScope : AbstractFreezable
{
readonly IProjectContent projectContent;

28
ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs

@ -23,6 +23,7 @@ using System.Diagnostics; @@ -23,6 +23,7 @@ using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
using Mono.Cecil;
@ -234,6 +235,33 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -234,6 +235,33 @@ namespace ICSharpCode.NRefactory.TypeSystem
else
return null;
}
IEnumerable<object> IAnnotatable.Annotations {
get { return EmptyList<object>.Instance; }
}
T IAnnotatable.Annotation<T>()
{
return null;
}
object IAnnotatable.Annotation(Type type)
{
return null;
}
void IAnnotatable.AddAnnotation(object annotation)
{
throw new NotSupportedException();
}
void IAnnotatable.RemoveAnnotations<T>()
{
}
void IAnnotatable.RemoveAnnotations(Type type)
{
}
}
#endregion

31
ICSharpCode.NRefactory/TypeSystem/Error.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// Error.cs
//
// Author:
@ -42,22 +42,27 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -42,22 +42,27 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// <summary>
/// Descibes an error during parsing.
/// </summary>
[Serializable]
public class Error
{
readonly ErrorType errorType;
readonly string message;
readonly DomRegion region;
/// <summary>
/// The type of the error.
/// </summary>
public ErrorType ErrorType { get; private set; }
public ErrorType ErrorType { get { return errorType; } }
/// <summary>
/// The error description.
/// </summary>
public string Message { get; private set; }
public string Message { get { return message; } }
/// <summary>
/// The region of the error.
/// </summary>
public DomRegion Region { get; private set; }
public DomRegion Region { get { return region; } }
/// <summary>
/// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.TypeSystem.Error"/> class.
@ -73,9 +78,9 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -73,9 +78,9 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </param>
public Error (ErrorType errorType, string message, DomRegion region)
{
this.ErrorType = errorType;
this.Message = message;
this.Region = region;
this.errorType = errorType;
this.message = message;
this.region = region;
}
/// <summary>
@ -92,9 +97,9 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -92,9 +97,9 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </param>
public Error (ErrorType errorType, string message, AstLocation location)
{
this.ErrorType = errorType;
this.Message = message;
this.Region = new DomRegion (location, location);
this.errorType = errorType;
this.message = message;
this.region = new DomRegion (location, location);
}
/// <summary>
@ -127,9 +132,9 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -127,9 +132,9 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </param>
public Error (ErrorType errorType, string message)
{
this.ErrorType = errorType;
this.Message = message;
this.Region = DomRegion.Empty;
this.errorType = errorType;
this.message = message;
this.region = DomRegion.Empty;
}
}
}

1
ICSharpCode.NRefactory/TypeSystem/IAnnotatable.cs

@ -78,7 +78,6 @@ namespace ICSharpCode.NRefactory @@ -78,7 +78,6 @@ namespace ICSharpCode.NRefactory
void RemoveAnnotations(Type type);
}
[Serializable]
public abstract class AbstractAnnotatable : IAnnotatable
{
// Annotations: points either null (no annotations), to the single annotation,

2
ICSharpCode.NRefactory/TypeSystem/Implementation/ProxyTypeResolveContext.cs

@ -26,7 +26,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -26,7 +26,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
/// Useful as base class for decorators.
/// </summary>
[Serializable]
public class ProxyTypeResolveContext : AbstractAnnotatable, ITypeResolveContext
public class ProxyTypeResolveContext : ITypeResolveContext
{
protected readonly ITypeResolveContext target;

55
ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs

@ -20,8 +20,8 @@ using System; @@ -20,8 +20,8 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using ICSharpCode.NRefactory.Utils;
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
@ -34,12 +34,22 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -34,12 +34,22 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
/// Compared with <see cref="TypeStorage"/>, this class adds support for the IProjectContent interface,
/// for partial classes, and for multi-threading.
/// </remarks>
public class SimpleProjectContent : AbstractAnnotatable, IProjectContent
[Serializable]
public class SimpleProjectContent : AbstractAnnotatable, IProjectContent, ISerializable, IDeserializationCallback
{
readonly TypeStorage types = new TypeStorage();
readonly ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim();
readonly Dictionary<string, IParsedFile> fileDict = new Dictionary<string, IParsedFile>(Platform.FileNameComparer);
#region Constructor
/// <summary>
/// Creates a new SimpleProjectContent instance.
/// </summary>
public SimpleProjectContent()
{
}
#endregion
#region AssemblyAttributes
readonly List<IAttribute> assemblyAttributes = new List<IAttribute>(); // mutable assembly attribute storage
readonly List<IAttribute> moduleAttributes = new List<IAttribute>();
@ -284,5 +294,46 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -284,5 +294,46 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
}
}
#endregion
#region Serialization
SerializationInfo serializationInfo;
protected SimpleProjectContent(SerializationInfo info, StreamingContext context)
{
this.serializationInfo = info;
assemblyAttributes.AddRange((IAttribute[])info.GetValue("AssemblyAttributes", typeof(IAttribute[])));
readOnlyAssemblyAttributes = assemblyAttributes.ToArray();
moduleAttributes.AddRange((IAttribute[])info.GetValue("ModuleAttributes", typeof(IAttribute[])));
readOnlyModuleAttributes = moduleAttributes.ToArray();
}
public virtual void OnDeserialization(object sender)
{
// We need to do this in OnDeserialization because at the time the deserialization
// constructor runs, type.FullName/file.FileName may not be deserialized yet.
if (serializationInfo != null) {
foreach (var typeDef in (ITypeDefinition[])serializationInfo.GetValue("Types", typeof(ITypeDefinition[]))) {
types.UpdateType(typeDef);
}
foreach (IParsedFile file in (IParsedFile[])serializationInfo.GetValue("Files", typeof(IParsedFile[]))) {
fileDict.Add(file.FileName, file);
}
serializationInfo = null;
}
}
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
readerWriterLock.EnterReadLock();
try {
info.AddValue("Types", types.GetTypes().ToArray());
info.AddValue("AssemblyAttributes", readOnlyAssemblyAttributes);
info.AddValue("ModuleAttributes", readOnlyModuleAttributes);
info.AddValue("Files", fileDict.Values.ToArray());
} finally {
readerWriterLock.ExitReadLock();
}
}
#endregion
}
}

3
ICSharpCode.NRefactory/TypeSystem/Implementation/TypeStorage.cs

@ -374,7 +374,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -374,7 +374,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
SerializationInfo serializationInfo;
protected TypeStorage(SerializationInfo info, StreamingContext context)
private TypeStorage(SerializationInfo info, StreamingContext context)
{
this.serializationInfo = info;
}
@ -391,6 +391,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -391,6 +391,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
foreach (var typeDef in (ITypeDefinition[])serializationInfo.GetValue("Types", typeof(ITypeDefinition[]))) {
UpdateType(typeDef);
}
serializationInfo = null;
}
#endregion
}

Loading…
Cancel
Save