Browse Source

Merge commit '611c310eb2867f65b1db2460c6d8b0cbc57ba37e' from NRefactory repository into SharpDevelop's newNR branch.

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
c20aa3e04e
  1. 24
      src/Libraries/NRefactory/ICSharpCode.Editor/ITextSource.cs
  2. 12
      src/Libraries/NRefactory/ICSharpCode.Editor/ReadOnlyDocument.cs
  3. 12
      src/Libraries/NRefactory/ICSharpCode.Editor/StringTextSource.cs
  4. 8
      src/Libraries/NRefactory/ICSharpCode.NRefactory.Demo/CSDemo.cs
  5. 15
      src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/ArrayCreateExpressionTests.cs
  6. 4
      src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/AttributeSectionTests.cs
  7. 2
      src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeSystemConvertVisitorTests.cs
  8. 2
      src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Refactoring/TypeSystemAstBuilderTests.cs
  9. 19
      src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/LinqTests.cs
  10. 4
      src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/ResolverTestBase.cs
  11. 72
      src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs
  12. 3
      src/Libraries/NRefactory/ICSharpCode.NRefactory.VB.Tests/ICSharpCode.NRefactory.VB.Tests.csproj
  13. 47
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/CSharpModifierToken.cs
  14. 2
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Modifiers.cs
  15. 3
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs
  16. 54
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs
  17. 4
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/ParsedFile.cs
  18. 27
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs
  19. 1355
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/mcs/cs-parser.cs
  20. 7
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/mcs/cs-parser.jay
  21. 32
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Refactoring/TypeSystemAstBuilder.cs
  22. 47
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/CSharpAttribute.cs
  23. 6
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/CSharpResolver.cs
  24. 120
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/ConstantValues.cs
  25. 13
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/FindReferences.cs
  26. 2
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/MemberResolveResult.cs
  27. 51
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/OperatorResolveResult.cs
  28. 2
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/ResolveAtLocation.cs
  29. 55
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs
  30. 44
      src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/TypeOfResolveResult.cs
  31. 1
      src/Libraries/NRefactory/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj
  32. 5
      src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/IAttribute.cs
  33. 32
      src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/IConstantValue.cs
  34. 9
      src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultAttribute.cs
  35. 8
      src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultParameter.cs
  36. 17
      src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleConstantValue.cs
  37. 9
      src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs
  38. 11
      src/Libraries/NRefactory/NRefactory.sln

24
src/Libraries/NRefactory/ICSharpCode.Editor/ITextSource.cs

@ -97,11 +97,31 @@ namespace ICSharpCode.Editor @@ -97,11 +97,31 @@ namespace ICSharpCode.Editor
/// Gets the index of the first occurrence of any character in the specified array.
/// </summary>
/// <param name="anyOf">Characters to search for</param>
/// <param name="startIndex">Start index of the search.</param>
/// <param name="startIndex">Start index of the area to search.</param>
/// <param name="count">Length of the area to search.</param>
/// <returns>The first index where any character was found; or -1 if no occurrence was found.</returns>
int IndexOfAny(char[] anyOf, int startIndex, int count);
/// <summary>
/// Gets the index of the first occurrence of the specified search text in this text source.
/// </summary>
/// <param name="searchText">The search text</param>
/// <param name="startIndex">Start index of the area to search.</param>
/// <param name="count">Length of the area to search.</param>
/// <param name="comparisonType">String comparison to use.</param>
/// <returns>The first index where the search term was found; or -1 if no occurrence was found.</returns>
int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType);
/// <summary>
/// Gets the index of the last occurrence of the specified search text in this text source.
/// </summary>
/// <param name="searchText">The search text</param>
/// <param name="startIndex">Start index of the area to search.</param>
/// <param name="count">Length of the area to search.</param>
/// <param name="comparisonType">String comparison to use.</param>
/// <returns>The last index where the search term was found; or -1 if no occurrence was found.</returns>
int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType);
/* What about:
void Insert (int offset, string value);
void Remove (int offset, int count);
@ -116,7 +136,7 @@ namespace ICSharpCode.Editor @@ -116,7 +136,7 @@ namespace ICSharpCode.Editor
IEnumerable<int> SearchBackward (string pattern, int startIndex);
IEnumerable<int> SearchBackwardIgnoreCase (string pattern, int startIndex);
*/
*/
}
/// <summary>

12
src/Libraries/NRefactory/ICSharpCode.Editor/ReadOnlyDocument.cs

@ -327,6 +327,18 @@ namespace ICSharpCode.Editor @@ -327,6 +327,18 @@ namespace ICSharpCode.Editor
return textSource.IndexOfAny(anyOf, startIndex, count);
}
/// <inheritdoc/>
public int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
{
return textSource.IndexOf(searchText, startIndex, count, comparisonType);
}
/// <inheritdoc/>
public int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
{
return textSource.LastIndexOf(searchText, startIndex, count, comparisonType);
}
object IServiceProvider.GetService(Type serviceType)
{
return null;

12
src/Libraries/NRefactory/ICSharpCode.Editor/StringTextSource.cs

@ -102,5 +102,17 @@ namespace ICSharpCode.Editor @@ -102,5 +102,17 @@ namespace ICSharpCode.Editor
{
return text.IndexOfAny(anyOf, startIndex, count);
}
/// <inheritdoc/>
public int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
{
return text.IndexOf(searchText, startIndex, count, comparisonType);
}
/// <inheritdoc/>
public int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
{
return text.LastIndexOf(searchText, startIndex, count, comparisonType);
}
}
}

8
src/Libraries/NRefactory/ICSharpCode.NRefactory.Demo/CSDemo.cs

@ -225,7 +225,7 @@ namespace ICSharpCode.NRefactory.Demo @@ -225,7 +225,7 @@ namespace ICSharpCode.NRefactory.Demo
foreach (TreeNode t in c) {
AstNode node = t.Tag as AstNode;
if (node != null) {
ResolveResult rr = v.GetResolveResult(node);
ResolveResult rr = v.GetResolveResultIfResolved(node);
if (rr != null)
t.Text = GetNodeTitle(node) + " " + rr.ToString();
else
@ -282,10 +282,12 @@ namespace ICSharpCode.NRefactory.Demo @@ -282,10 +282,12 @@ namespace ICSharpCode.NRefactory.Demo
FindReferences fr = new FindReferences();
int referenceCount = 0;
fr.ReferenceFound += delegate { referenceCount++; };
FoundReferenceCallback callback = delegate(AstNode matchNode, ResolveResult result) {
referenceCount++;
};
var searchScopes = fr.GetSearchScopes(entity);
navigator = new CompositeResolveVisitorNavigator(searchScopes.ToArray());
navigator = new CompositeResolveVisitorNavigator(searchScopes.Select(s => s.GetNavigator(callback)).ToArray());
visitor = new ResolveVisitor(resolver, parsedFile, navigator);
visitor.Scan(compilationUnit);

15
src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/ArrayCreateExpressionTests.cs

@ -57,6 +57,9 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression @@ -57,6 +57,9 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
"new int[] { 1 }",
new ArrayCreateExpression {
Type = new PrimitiveType("int"),
AdditionalArraySpecifiers = {
new ArraySpecifier(0)
},
Initializer = new ArrayInitializerExpression {
Elements = { new PrimitiveExpression(1) }
}
@ -70,7 +73,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression @@ -70,7 +73,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
"new int[,] { { 1 } }",
new ArrayCreateExpression {
Type = new PrimitiveType("int"),
Arguments = { new EmptyExpression(), new EmptyExpression() }, // TODO: can we improve the AST for this?
AdditionalArraySpecifiers = { new ArraySpecifier (2) },
Initializer = new ArrayInitializerExpression {
Elements = {
new ArrayInitializerExpression {
@ -87,6 +90,9 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression @@ -87,6 +90,9 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
ParseUtilCSharp.AssertExpression(
"new[] { 1, 10, 100, 1000 }",
new ArrayCreateExpression {
AdditionalArraySpecifiers = {
new ArraySpecifier(0)
},
Initializer = new ArrayInitializerExpression {
Elements = {
new PrimitiveExpression(1),
@ -103,7 +109,9 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression @@ -103,7 +109,9 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
ParseUtilCSharp.AssertExpression(
"new [,] { { 1, 10 }, { 100, 1000 } }",
new ArrayCreateExpression {
Arguments = { new EmptyExpression(), new EmptyExpression() }, // TODO: can we improve the AST for this?
AdditionalArraySpecifiers = {
new ArraySpecifier(2),
},
Initializer = new ArrayInitializerExpression {
Elements = {
new ArrayInitializerExpression {
@ -128,6 +136,9 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression @@ -128,6 +136,9 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
ParseUtilCSharp.AssertExpression(
"new [] { a = 10 }",
new ArrayCreateExpression {
AdditionalArraySpecifiers = {
new ArraySpecifier(0)
},
Initializer = new ArrayInitializerExpression {
Elements = {
new AssignmentExpression(new IdentifierExpression("a"), new PrimitiveExpression(10))

4
src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/AttributeSectionTests.cs

@ -88,7 +88,7 @@ public class Form1 { @@ -88,7 +88,7 @@ public class Form1 {
}}});
}
[Test, Ignore("Parser doesn't support attributes on type parameters")]
[Test]
public void AttributesOnTypeParameter()
{
ParseUtilCSharp.AssertGlobal(
@ -148,7 +148,7 @@ public class Form1 { @@ -148,7 +148,7 @@ public class Form1 {
// TODO: Tests for other contexts where attributes can appear
[Test, Ignore("Parser does not support NamedArgumentExpression in attributes")]
[Test]
public void AttributeWithNamedArguments()
{
ParseUtilCSharp.AssertTypeMember(

2
src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeSystemConvertVisitorTests.cs

@ -45,7 +45,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser @@ -45,7 +45,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
}
var testCasePC = new SimpleProjectContent();
ParsedFile parsedFile = new TypeSystemConvertVisitor(testCasePC, fileName).Convert(cu);
CSharpParsedFile parsedFile = new TypeSystemConvertVisitor(testCasePC, fileName).Convert(cu);
parsedFile.Freeze();
testCasePC.UpdateProjectContent(null, parsedFile);
return testCasePC;

2
src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Refactoring/TypeSystemAstBuilderTests.cs

@ -55,7 +55,7 @@ namespace OtherNS { @@ -55,7 +55,7 @@ namespace OtherNS {
SimpleProjectContent pc;
ITypeResolveContext ctx;
ITypeDefinition baseClass, derivedClass, nestedClass, systemClass;
ParsedFile parsedFile;
CSharpParsedFile parsedFile;
[SetUp]
public void SetUp()

19
src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/LinqTests.cs

@ -313,5 +313,24 @@ class XYZ @@ -313,5 +313,24 @@ class XYZ
Assert.AreEqual("GroupJoin", rr.Member.Name);
Assert.AreEqual("System.Int32", rr.Type.FullName);
}
[Test]
public void GroupWithQueryContinuation()
{
string program = @"using System; using System.Linq;
class TestClass
{
static void M(string[] args)
{
var query =
from w in ""one to three"".Split()
group w by w.Length into g
orderby g.Key descending
select new { g.Key, Count = g.Count(), Avg = g.Average ($w$ => w.Length) };
}
}";
var rr = Resolve<LocalResolveResult>(program);
Assert.AreEqual("System.String", rr.Type.FullName);
}
}
}

4
src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Resolver/ResolverTestBase.cs

@ -194,7 +194,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -194,7 +194,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
SetUp();
ParsedFile parsedFile = new ParsedFile("test.cs", resolver.CurrentUsingScope);
CSharpParsedFile parsedFile = new CSharpParsedFile("test.cs", resolver.CurrentUsingScope);
TypeSystemConvertVisitor convertVisitor = new TypeSystemConvertVisitor(parsedFile, resolver.CurrentUsingScope, null);
cu.AcceptVisitor(convertVisitor, null);
project.UpdateProjectContent(null, convertVisitor.ParsedFile);
@ -259,7 +259,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -259,7 +259,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
SetUp();
ParsedFile parsedFile = new ParsedFile("test.cs", resolver.CurrentUsingScope);
CSharpParsedFile parsedFile = new CSharpParsedFile("test.cs", resolver.CurrentUsingScope);
TypeSystemConvertVisitor convertVisitor = new TypeSystemConvertVisitor(parsedFile, resolver.CurrentUsingScope, null);
cu.AcceptVisitor(convertVisitor, null);
project.UpdateProjectContent(null, convertVisitor.ParsedFile);

72
src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/TypeSystem/TypeSystemTests.cs

@ -20,6 +20,7 @@ using System; @@ -20,6 +20,7 @@ using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
using ICSharpCode.NRefactory.TypeSystem.TestCase;
using NUnit.Framework;
@ -132,14 +133,15 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -132,14 +133,15 @@ namespace ICSharpCode.NRefactory.TypeSystem
var posArgs = typeTest.GetPositionalArguments(ctx);
Assert.AreEqual(3, posArgs.Count);
// first argument is (int)42
Assert.AreEqual(42, (int)posArgs[0].GetValue(ctx));
Assert.AreEqual(42, (int)posArgs[0].ConstantValue);
// second argument is typeof(System.Action<>)
IType rt = (IType)posArgs[1].GetValue(ctx);
Assert.IsFalse(rt is ParameterizedType); // rt must not be constructed - it's just an unbound type
Assert.AreEqual("System.Action", rt.FullName);
Assert.AreEqual(1, rt.TypeParameterCount);
TypeOfResolveResult rt = (TypeOfResolveResult)posArgs[1];
Assert.IsFalse(rt.ReferencedType is ParameterizedType); // rt must not be constructed - it's just an unbound type
Assert.AreEqual("System.Action", rt.ReferencedType.FullName);
Assert.AreEqual(1, rt.ReferencedType.TypeParameterCount);
// third argument is typeof(IDictionary<string, IList<TestAttribute>>)
ParameterizedType crt = (ParameterizedType)posArgs[2].GetValue(ctx);
rt = (TypeOfResolveResult)posArgs[2];
ParameterizedType crt = (ParameterizedType)rt.ReferencedType;
Assert.AreEqual("System.Collections.Generic.IDictionary", crt.FullName);
Assert.AreEqual("System.String", crt.TypeArguments[0].FullName);
// ? for NUnit.TestAttribute (because that assembly isn't in ctx)
@ -153,8 +155,8 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -153,8 +155,8 @@ namespace ICSharpCode.NRefactory.TypeSystem
var forwardAttribute = attributes.Single(a => a.AttributeType.Resolve(ctx).FullName == typeof(TypeForwardedToAttribute).FullName);
var posArgs = forwardAttribute.GetPositionalArguments(ctx);
Assert.AreEqual(1, posArgs.Count);
IType rt = (IType)posArgs[0].GetValue(ctx);
Assert.AreEqual("System.Func`2", rt.ReflectionName);
TypeOfResolveResult rt = (TypeOfResolveResult)posArgs[0];
Assert.AreEqual("System.Func`2", rt.ReferencedType.ReflectionName);
}
[Test]
@ -254,33 +256,25 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -254,33 +256,25 @@ namespace ICSharpCode.NRefactory.TypeSystem
Assert.IsTrue(f.IsStatic);
Assert.IsTrue(f.IsConst);
Assert.AreEqual(Accessibility.Public, f.Accessibility);
Assert.AreSame(e, f.ConstantValue.GetValueType(ctx));
Assert.AreEqual(typeof(short), f.ConstantValue.GetValue(ctx).GetType());
Assert.AreSame(e, f.ConstantValue.Resolve(ctx).Type);
Assert.AreEqual(typeof(short), f.ConstantValue.Resolve(ctx).ConstantValue.GetType());
}
Assert.AreEqual("First", e.Fields[0].Name);
Assert.AreEqual(0, e.Fields[0].ConstantValue.GetValue(ctx));
Assert.AreEqual(0, e.Fields[0].ConstantValue.Resolve(ctx).ConstantValue);
Assert.AreEqual("Second", e.Fields[1].Name);
Assert.AreSame(e, e.Fields[1].ConstantValue.GetValueType(ctx));
Assert.AreEqual(1, e.Fields[1].ConstantValue.GetValue(ctx));
Assert.AreSame(e, e.Fields[1].ConstantValue.Resolve(ctx).Type);
Assert.AreEqual(1, e.Fields[1].ConstantValue.Resolve(ctx).ConstantValue);
Assert.AreEqual("Flag1", e.Fields[2].Name);
Assert.AreEqual(0x10, e.Fields[2].ConstantValue.GetValue(ctx));
Assert.AreEqual(0x10, e.Fields[2].ConstantValue.Resolve(ctx).ConstantValue);
Assert.AreEqual("Flag2", e.Fields[3].Name);
Assert.AreEqual(0x20, e.Fields[3].ConstantValue.GetValue(ctx));
Assert.AreEqual(0x20, e.Fields[3].ConstantValue.Resolve(ctx).ConstantValue);
Assert.AreEqual("CombinedFlags", e.Fields[4].Name);
Assert.AreEqual(0x30, e.Fields[4].ConstantValue.GetValue(ctx));
}
[Test]
public void GetNestedTypesFromGenericClassTest()
{
ITypeDefinition b = ctx.GetTypeDefinition(typeof(Base<>));
// Base.GetNestedTypes() = { Base`1+Nested`1[`0, unbound] }
Assert.AreEqual(0x30, e.Fields[4].ConstantValue.Resolve(ctx).ConstantValue);
}
[Test]
@ -336,20 +330,20 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -336,20 +330,20 @@ namespace ICSharpCode.NRefactory.TypeSystem
{
IAttribute attr = ctx.GetTypeDefinition(typeof(ExplicitFieldLayoutStruct)).Attributes.Single();
Assert.AreEqual("System.Runtime.InteropServices.StructLayoutAttribute", attr.AttributeType.Resolve(ctx).FullName);
IConstantValue arg1 = attr.GetPositionalArguments(ctx).Single();
Assert.AreEqual("System.Runtime.InteropServices.LayoutKind", arg1.GetValueType(ctx).FullName);
Assert.AreEqual((int)LayoutKind.Explicit, arg1.GetValue(ctx));
ResolveResult arg1 = attr.GetPositionalArguments(ctx).Single();
Assert.AreEqual("System.Runtime.InteropServices.LayoutKind", arg1.Type.FullName);
Assert.AreEqual((int)LayoutKind.Explicit, arg1.ConstantValue);
var namedArgs = attr.GetNamedArguments(ctx);
var arg2 = namedArgs[0];
Assert.AreEqual("CharSet", arg2.Key);
Assert.AreEqual("System.Runtime.InteropServices.CharSet", arg2.Value.GetValueType(ctx).FullName);
Assert.AreEqual((int)CharSet.Unicode, arg2.Value.GetValue(ctx));
Assert.AreEqual("System.Runtime.InteropServices.CharSet", arg2.Value.Type.FullName);
Assert.AreEqual((int)CharSet.Unicode, arg2.Value.ConstantValue);
var arg3 = namedArgs[1];
Assert.AreEqual("Pack", arg3.Key);
Assert.AreEqual("System.Int32", arg3.Value.GetValueType(ctx).FullName);
Assert.AreEqual(8, arg3.Value.GetValue(ctx));
Assert.AreEqual("System.Int32", arg3.Value.Type.FullName);
Assert.AreEqual(8, arg3.Value.ConstantValue);
}
[Test]
@ -357,15 +351,15 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -357,15 +351,15 @@ namespace ICSharpCode.NRefactory.TypeSystem
{
IField field = ctx.GetTypeDefinition(typeof(ExplicitFieldLayoutStruct)).Fields.Single(f => f.Name == "Field0");
Assert.AreEqual("System.Runtime.InteropServices.FieldOffsetAttribute", field.Attributes.Single().AttributeType.Resolve(ctx).FullName);
IConstantValue arg = field.Attributes.Single().GetPositionalArguments(ctx).Single();
Assert.AreEqual("System.Int32", arg.GetValueType(ctx).FullName);
Assert.AreEqual(0, arg.GetValue(ctx));
ResolveResult arg = field.Attributes.Single().GetPositionalArguments(ctx).Single();
Assert.AreEqual("System.Int32", arg.Type.FullName);
Assert.AreEqual(0, arg.ConstantValue);
field = ctx.GetTypeDefinition(typeof(ExplicitFieldLayoutStruct)).Fields.Single(f => f.Name == "Field100");
Assert.AreEqual("System.Runtime.InteropServices.FieldOffsetAttribute", field.Attributes.Single().AttributeType.Resolve(ctx).FullName);
arg = field.Attributes.Single().GetPositionalArguments(ctx).Single();
Assert.AreEqual("System.Int32", arg.GetValueType(ctx).FullName);
Assert.AreEqual(100, arg.GetValue(ctx));
Assert.AreEqual("System.Int32", arg.Type.FullName);
Assert.AreEqual(100, arg.ConstantValue);
}
[Test]
@ -374,8 +368,8 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -374,8 +368,8 @@ namespace ICSharpCode.NRefactory.TypeSystem
IMethod method = ctx.GetTypeDefinition(typeof(NonCustomAttributes)).Methods.Single(m => m.Name == "DllMethod");
IAttribute dllImport = method.Attributes.Single();
Assert.AreEqual("System.Runtime.InteropServices.DllImportAttribute", dllImport.AttributeType.Resolve(ctx).FullName);
Assert.AreEqual("unmanaged.dll", dllImport.GetPositionalArguments(ctx)[0].GetValue(ctx));
Assert.AreEqual((int)CharSet.Unicode, dllImport.GetNamedArguments(ctx).Single().Value.GetValue(ctx));
Assert.AreEqual("unmanaged.dll", dllImport.GetPositionalArguments(ctx)[0].ConstantValue);
Assert.AreEqual((int)CharSet.Unicode, dllImport.GetNamedArguments(ctx).Single().Value.ConstantValue);
}
[Test]
@ -394,7 +388,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -394,7 +388,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
{
IMethod method = ctx.GetTypeDefinition(typeof(NonCustomAttributes)).Methods.Single(m => m.Name == "DllMethod");
IAttribute marshalAs = method.ReturnTypeAttributes.Single();
Assert.AreEqual((int)UnmanagedType.Bool, marshalAs.GetPositionalArguments(ctx).Single().GetValue(ctx));
Assert.AreEqual((int)UnmanagedType.Bool, marshalAs.GetPositionalArguments(ctx).Single().ConstantValue);
}
[Test]

3
src/Libraries/NRefactory/ICSharpCode.NRefactory.VB.Tests/ICSharpCode.NRefactory.VB.Tests.csproj

@ -62,8 +62,7 @@ @@ -62,8 +62,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
<HintPath>..\..\Mono.Cecil\Test\libs\nunit-2.4.8\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">

47
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/CSharpModifierToken.cs

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
//
// CSharpModifierToken.cs
//
//
// Author:
// Mike Krüger <mkrueger@novell.com>
//
@ -37,14 +37,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -37,14 +37,8 @@ namespace ICSharpCode.NRefactory.CSharp
public Modifiers Modifier {
get { return modifier; }
set {
for (int i = 0; i < lengthTable.Count; i++) {
if (lengthTable[i].Key == value) {
this.modifier = value;
this.tokenLength = lengthTable[i].Value;
return;
}
}
throw new ArgumentException ("Modifier " + value + " is invalid.");
this.tokenLength = GetModifierName(value).Length;
this.modifier = value;
}
}
@ -56,30 +50,18 @@ namespace ICSharpCode.NRefactory.CSharp @@ -56,30 +50,18 @@ namespace ICSharpCode.NRefactory.CSharp
// Not worth using a dictionary for such few elements.
// This table is sorted in the order that modifiers should be output when generating code.
static readonly List<KeyValuePair<Modifiers, int>> lengthTable = new List<KeyValuePair<Modifiers, int>> () {
new KeyValuePair<Modifiers, int>(Modifiers.Public, "public".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Protected, "protected".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Private, "private".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Internal, "internal".Length),
new KeyValuePair<Modifiers, int>(Modifiers.New, "new".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Unsafe, "unsafe".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Abstract, "abstract".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Virtual, "virtual".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Sealed, "sealed".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Static, "static".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Override, "override".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Readonly, "readonly".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Volatile, "volatile".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Extern, "extern".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Partial, "partial".Length),
new KeyValuePair<Modifiers, int>(Modifiers.Const, "const".Length),
// even though it's used for patterns only, it needs to be in this table to be usable in the AST
new KeyValuePair<Modifiers, int>(Modifiers.Any, "any".Length)
static readonly Modifiers[] allModifiers = {
Modifiers.Public, Modifiers.Protected, Modifiers.Private, Modifiers.Internal,
Modifiers.New,
Modifiers.Unsafe,
Modifiers.Abstract, Modifiers.Virtual, Modifiers.Sealed, Modifiers.Static, Modifiers.Override,
Modifiers.Readonly, Modifiers.Volatile,
Modifiers.Extern, Modifiers.Partial, Modifiers.Const,
Modifiers.Any
};
public static IEnumerable<Modifiers> AllModifiers {
get { return lengthTable.Select(p => p.Key); }
get { return allModifiers; }
}
public CSharpModifierToken (AstLocation location, Modifiers modifier) : base (location, 0)
@ -122,8 +104,9 @@ namespace ICSharpCode.NRefactory.CSharp @@ -122,8 +104,9 @@ namespace ICSharpCode.NRefactory.CSharp
return "volatile";
case Modifiers.Unsafe:
return "unsafe";
case Modifiers.Fixed:
return "fixed";
case Modifiers.Any:
// even though it's used for patterns only, it needs to be in this list to be usable in the AST
return "any";
default:
throw new NotSupportedException("Invalid value for Modifiers");
}

2
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/Modifiers.cs

@ -58,7 +58,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -58,7 +58,7 @@ namespace ICSharpCode.NRefactory.CSharp
//Overloads = 0x10000,
//WithEvents = 0x20000,
//Default = 0x40000,
Fixed = 0x80000,
//Fixed = 0x80000,
//ProtectedOrInternal = Internal | Protected,
//ProtectedAndInternal = 0x100000, (not supported in C#)

3
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

@ -551,7 +551,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -551,7 +551,8 @@ namespace ICSharpCode.NRefactory.CSharp
StartNode (arrayCreateExpression);
WriteKeyword ("new");
arrayCreateExpression.Type.AcceptVisitor (this, data);
WriteCommaSeparatedListInBrackets (arrayCreateExpression.Arguments);
if (arrayCreateExpression.Arguments.Count > 0)
WriteCommaSeparatedListInBrackets (arrayCreateExpression.Arguments);
foreach (var specifier in arrayCreateExpression.AdditionalArraySpecifiers)
specifier.AcceptVisitor (this, data);
arrayCreateExpression.Initializer.AcceptVisitor (this, data);

54
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

@ -195,9 +195,22 @@ namespace ICSharpCode.NRefactory.CSharp @@ -195,9 +195,22 @@ namespace ICSharpCode.NRefactory.CSharp
if (attr.PosArguments != null) {
foreach (var arg in attr.PosArguments) {
var na = arg as NamedArgument;
if (na != null) {
var newArg = new NamedArgumentExpression ();
newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), NamedArgumentExpression.Roles.Identifier);
var argLoc = LocationsBag.GetLocations (na);
if (argLoc != null)
newArg.AddChild (new CSharpTokenNode (Convert (argLoc[0]), 1), NamedArgumentExpression.Roles.Assign);
newArg.AddChild ((Expression)na.Expr.Accept (this), NamedExpression.Roles.Expression);
result.AddChild (newArg, Attribute.Roles.Argument);
continue;
}
result.AddChild ((Expression)arg.Expr.Accept (this), Attribute.Roles.Argument);
}
}
Console.WriteLine ("---");
if (attr.NamedArguments != null) {
foreach (NamedArgument na in attr.NamedArguments) {
var newArg = new NamedExpression ();
@ -714,7 +727,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -714,7 +727,14 @@ namespace ICSharpCode.NRefactory.CSharp
{
if (a.OptAttributes == null)
return;
foreach (var attr in a.OptAttributes.Sections) {
AddAttributeSection (parent, a.OptAttributes);
}
public void AddAttributeSection (AstNode parent, Attributes attrs)
{
if (attrs == null)
return;
foreach (var attr in attrs.Sections) {
parent.AddChild (ConvertAttributeSection (attr), AttributedNode.AttributeRole);
}
}
@ -2156,7 +2176,9 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2156,7 +2176,9 @@ namespace ICSharpCode.NRefactory.CSharp
if (arg == null)
continue;
TypeParameterDeclaration tp = new TypeParameterDeclaration();
// TODO: attributes
AddAttributeSection (tp, arg.OptAttributes);
switch (arg.Variance) {
case Variance.Covariant:
tp.Variance = VarianceModifier.Covariant;
@ -2432,7 +2454,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2432,7 +2454,11 @@ namespace ICSharpCode.NRefactory.CSharp
result.AddChild (ConvertToType (arrayCreationExpression.NewType), ArrayCreateExpression.Roles.Type);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), ArrayCreateExpression.Roles.LBracket);
var next = arrayCreationExpression.Rank;
if (arrayCreationExpression.Arguments != null) {
// skip first array rank.
next = next.Next;
var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Arguments);
for (int i = 0 ;i < arrayCreationExpression.Arguments.Count; i++) {
result.AddChild ((Expression)arrayCreationExpression.Arguments[i].Accept (this), ArrayCreateExpression.Roles.Argument);
@ -2440,7 +2466,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2440,7 +2466,7 @@ namespace ICSharpCode.NRefactory.CSharp
result.AddChild (new CSharpTokenNode (Convert (commaLocations [commaLocations.Count - i]), 1), ArrayCreateExpression.Roles.Comma);
}
}
var next = arrayCreationExpression.Rank.Next;
while (next != null) {
ArraySpecifier spec = new ArraySpecifier (next.Dimension);
var loc = LocationsBag.GetLocations (next);
@ -2888,7 +2914,6 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2888,7 +2914,6 @@ namespace ICSharpCode.NRefactory.CSharp
var result = new QueryJoinClause ();
var location = LocationsBag.GetLocations (join);
result.AddChild (new CSharpTokenNode (Convert (join.Location), "join".Length), QueryJoinClause.JoinKeywordRole);
result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.JoinIdentifierRole);
if (location != null)
@ -2898,11 +2923,17 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2898,11 +2923,17 @@ namespace ICSharpCode.NRefactory.CSharp
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[1]), "on".Length), QueryJoinClause.OnKeywordRole);
// TODO: on expression
var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
if (outer != null)
result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[2]), "equals".Length), QueryJoinClause.EqualsKeywordRole);
// TODO: equals expression
var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
if (inner != null)
result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
return result;
}
@ -2919,16 +2950,19 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2919,16 +2950,19 @@ namespace ICSharpCode.NRefactory.CSharp
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[0]), "in".Length), QueryJoinClause.InKeywordRole);
result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
if (outer != null)
result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[1]), "on".Length), QueryJoinClause.OnKeywordRole);
// TODO: on expression
result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[2]), "equals".Length), QueryJoinClause.EqualsKeywordRole);
// TODO: equals expression
var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
if (inner != null)
result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[3]), "into".Length), QueryJoinClause.IntoKeywordRole);

4
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/ParsedFile.cs

@ -28,7 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -28,7 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp
/// Represents a file that was parsed and converted for the type system.
/// </summary>
[Serializable]
public sealed class ParsedFile : AbstractFreezable, IParsedFile
public sealed class CSharpParsedFile : AbstractFreezable, IParsedFile
{
readonly string fileName;
readonly UsingScope rootUsingScope;
@ -48,7 +48,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -48,7 +48,7 @@ namespace ICSharpCode.NRefactory.CSharp
usingScopes = FreezeList(usingScopes);
}
public ParsedFile(string fileName, UsingScope rootUsingScope)
public CSharpParsedFile(string fileName, UsingScope rootUsingScope)
{
if (fileName == null)
throw new ArgumentNullException("fileName");

27
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs

@ -34,7 +34,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -34,7 +34,7 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary>
public class TypeSystemConvertVisitor : DepthFirstAstVisitor<object, IEntity>
{
readonly ParsedFile parsedFile;
readonly CSharpParsedFile parsedFile;
UsingScope usingScope;
DefaultTypeDefinition currentTypeDefinition;
DefaultMethod currentMethod;
@ -61,7 +61,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -61,7 +61,7 @@ namespace ICSharpCode.NRefactory.CSharp
throw new ArgumentNullException("pc");
if (fileName == null)
throw new ArgumentNullException("fileName");
this.parsedFile = new ParsedFile(fileName, new UsingScope(pc));
this.parsedFile = new CSharpParsedFile(fileName, new UsingScope(pc));
this.usingScope = parsedFile.RootUsingScope;
}
@ -71,7 +71,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -71,7 +71,7 @@ namespace ICSharpCode.NRefactory.CSharp
/// <param name="parsedFile">The parsed file to which members should be added.</param>
/// <param name="currentUsingScope">The current using scope.</param>
/// <param name="currentTypeDefinition">The current type definition.</param>
public TypeSystemConvertVisitor(ParsedFile parsedFile, UsingScope currentUsingScope = null, DefaultTypeDefinition currentTypeDefinition = null)
public TypeSystemConvertVisitor(CSharpParsedFile parsedFile, UsingScope currentUsingScope = null, DefaultTypeDefinition currentTypeDefinition = null)
{
if (parsedFile == null)
throw new ArgumentNullException("parsedFile");
@ -80,13 +80,13 @@ namespace ICSharpCode.NRefactory.CSharp @@ -80,13 +80,13 @@ namespace ICSharpCode.NRefactory.CSharp
this.currentTypeDefinition = currentTypeDefinition;
}
public ParsedFile Convert(AstNode node)
public CSharpParsedFile Convert(AstNode node)
{
node.AcceptVisitor(this, null);
return parsedFile;
}
public ParsedFile ParsedFile {
public CSharpParsedFile ParsedFile {
get { return parsedFile; }
}
@ -340,9 +340,6 @@ namespace ICSharpCode.NRefactory.CSharp @@ -340,9 +340,6 @@ namespace ICSharpCode.NRefactory.CSharp
field.IsReadOnly = (modifiers & Modifiers.Readonly) != 0;
field.ReturnType = ConvertType(fieldDeclaration.ReturnType);
if ((modifiers & Modifiers.Fixed) != 0) {
field.ReturnType = PointerTypeReference.Create(field.ReturnType);
}
if ((modifiers & Modifiers.Const) != 0) {
field.ConstantValue = ConvertConstantValue(field.ReturnType, vi.Initializer);
@ -356,6 +353,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -356,6 +353,12 @@ namespace ICSharpCode.NRefactory.CSharp
return isSingleField ? field : null;
}
public override IEntity VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration, object data)
{
// TODO: add support for fixed fields
return base.VisitFixedFieldDeclaration(fixedFieldDeclaration, data);
}
public override IEntity VisitEnumMemberDeclaration(EnumMemberDeclaration enumMemberDeclaration, object data)
{
DefaultField field = new DefaultField(currentTypeDefinition, enumMemberDeclaration.Name);
@ -762,12 +765,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -762,12 +765,12 @@ namespace ICSharpCode.NRefactory.CSharp
namedCtorArguments = new List<KeyValuePair<string, IConstantValue>>();
namedCtorArguments.Add(new KeyValuePair<string, IConstantValue>(nae.Identifier, ConvertAttributeArgument(nae.Expression)));
} else {
AssignmentExpression ae = expr as AssignmentExpression;
if (ae != null && ae.Left is IdentifierExpression && ae.Operator == AssignmentOperatorType.Assign) {
string name = ((IdentifierExpression)ae.Left).Identifier;
NamedExpression namedExpression = expr as NamedExpression;
if (namedExpression != null) {
string name = namedExpression.Identifier;
if (namedArguments == null)
namedArguments = new List<KeyValuePair<string, IConstantValue>>();
namedArguments.Add(new KeyValuePair<string, IConstantValue>(name, ConvertAttributeArgument(ae.Right)));
namedArguments.Add(new KeyValuePair<string, IConstantValue>(name, ConvertAttributeArgument(namedExpression.Expression)));
} else {
if (positionalArguments == null)
positionalArguments = new List<IConstantValue>();

1355
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/mcs/cs-parser.cs

File diff suppressed because it is too large Load Diff

7
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Parser/mcs/cs-parser.jay

@ -145,6 +145,7 @@ namespace Mono.CSharp @@ -145,6 +145,7 @@ namespace Mono.CSharp
Location parameterModifierLocation, savedLocation, savedOpenLocation, savedCloseLocation;
Location savedAttrParenOpenLocation, savedAttrParenCloseLocation;
Stack<List<Location>> locationListStack = new Stack<List<Location>> (); // used for type parameters
List<Location> attributeCommas = new List<Location> ();
object lastYYVal;
@ -670,6 +671,10 @@ attribute_sections @@ -670,6 +671,10 @@ attribute_sections
var sect = (List<Attribute>) $1;
$$ = new Attributes (sect);
lbag.AddLocation (sect, savedOpenLocation, savedCloseLocation);
if (attributeCommas.Count > 0) {
lbag.AppendTo (sect, attributeCommas);
attributeCommas.Clear ();
}
}
| attribute_sections attribute_section
{
@ -752,7 +757,7 @@ attribute_list @@ -752,7 +757,7 @@ attribute_list
{
var attrs = (List<Attribute>) $1;
attrs.Add ((Attribute) $3);
lbag.AppendTo (attrs, GetLocation ($2));
attributeCommas.Add (GetLocation ($2));
$$ = attrs;
}

32
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Refactoring/TypeSystemAstBuilder.cs

@ -335,17 +335,31 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -335,17 +335,31 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
if (constantValue == null)
throw new ArgumentNullException("constantValue");
IType type = constantValue.GetValueType(context);
object val = constantValue.GetValue(context);
if (val == null) {
if (type.IsReferenceType(context) == true)
return new NullReferenceExpression();
else
return new DefaultValueExpression(ConvertType(type));
} else if (type.Kind == TypeKind.Enum) {
return ConvertConstantValue(constantValue.Resolve(context));
}
Expression ConvertConstantValue(ResolveResult rr)
{
if (rr is TypeOfResolveResult) {
return new TypeOfExpression(ConvertType(((TypeOfResolveResult)rr).Type));
} else if (rr is ArrayCreateResolveResult) {
ArrayCreateResolveResult acrr = (ArrayCreateResolveResult)rr;
AstType type = ConvertType(acrr.Type);
throw new NotImplementedException();
} else if (rr.IsCompileTimeConstant) {
object val = rr.ConstantValue;
if (val == null) {
if (rr.Type.IsReferenceType(context) == true)
return new NullReferenceExpression();
else
return new DefaultValueExpression(ConvertType(rr.Type));
} else if (rr.Type.Kind == TypeKind.Enum) {
throw new NotImplementedException();
} else {
return new PrimitiveExpression(val);
}
} else {
return new PrimitiveExpression(val);
return new EmptyExpression();
}
}
#endregion

47
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/CSharpAttribute.cs

@ -19,6 +19,8 @@ @@ -19,6 +19,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
@ -70,14 +72,14 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -70,14 +72,14 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
if (positionalArguments != null) {
while (i < positionalArguments.Count) {
IConstantValue cv = positionalArguments[i];
arguments[i] = new ConstantResolveResult(cv.GetValueType(context), cv.GetValue(context));
arguments[i] = cv.Resolve(context);
i++;
}
}
if (namedCtorArguments != null) {
foreach (var pair in namedCtorArguments) {
argumentNames[i] = pair.Key;
arguments[i] = new ConstantResolveResult(pair.Value.GetValueType(context), pair.Value.GetValue(context));
arguments[i] = pair.Value.Resolve(context);
i++;
}
}
@ -85,17 +87,19 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -85,17 +87,19 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
return mrr != null ? mrr.Member as IMethod : null;
}
public IList<IConstantValue> GetPositionalArguments(ITypeResolveContext context)
public IList<ResolveResult> GetPositionalArguments(ITypeResolveContext context)
{
List<ResolveResult> result = new List<ResolveResult>();
if (positionalArguments != null) {
foreach (var arg in positionalArguments) {
result.Add(Resolve(arg, context));
}
}
if (namedCtorArguments == null || namedCtorArguments.Count == 0) {
// no namedCtorArguments: just return the positionalArguments
if (positionalArguments != null)
return new ReadOnlyCollection<IConstantValue>(positionalArguments);
else
return EmptyList<IConstantValue>.Instance;
return result.AsReadOnly();
}
// we do have namedCtorArguments, which need to be re-ordered and appended to the positional arguments
List<IConstantValue> result = new List<IConstantValue>(this.positionalArguments);
IMethod method = ResolveConstructor(context);
if (method != null) {
for (int i = result.Count; i < method.Parameters.Count; i++) {
@ -103,25 +107,40 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -103,25 +107,40 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
bool found = false;
foreach (var pair in namedCtorArguments) {
if (pair.Key == p.Name) {
result.Add(pair.Value);
result.Add(Resolve(pair.Value, context));
found = true;
}
}
if (!found) {
// add the parameter's default value:
result.Add(p.DefaultValue ?? new SimpleConstantValue(p.Type, CSharpResolver.GetDefaultValue(p.Type.Resolve(context))));
if (p.DefaultValue != null) {
result.Add(Resolve(p.DefaultValue, context));
} else {
IType type = p.Type.Resolve(context);
result.Add(new ConstantResolveResult(type, CSharpResolver.GetDefaultValue(type)));
}
}
}
}
return result.AsReadOnly();
}
public IList<KeyValuePair<string, IConstantValue>> GetNamedArguments(ITypeResolveContext context)
ResolveResult Resolve(IConstantValue constantValue, ITypeResolveContext context)
{
if (namedArguments != null)
return new ReadOnlyCollection<KeyValuePair<string, IConstantValue>>(namedArguments);
if (constantValue != null)
return constantValue.Resolve(context);
else
return EmptyList<KeyValuePair<string, IConstantValue>>.Instance;
return new ErrorResolveResult(SharedTypes.UnknownType);
}
public IList<KeyValuePair<string, ResolveResult>> GetNamedArguments(ITypeResolveContext context)
{
if (namedArguments != null) {
return namedArguments.Select(p => new KeyValuePair<string, ResolveResult>(p.Key, p.Value.Resolve(context)))
.ToList().AsReadOnly();
} else {
return EmptyList<KeyValuePair<string, ResolveResult>>.Instance;
}
}
}

6
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/CSharpResolver.cs

@ -1960,7 +1960,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -1960,7 +1960,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
// Look in local variables
foreach (IVariable v in this.LocalVariables) {
if (v.Name == identifier) {
object constantValue = v.IsConst ? v.ConstantValue.GetValue(context) : null;
object constantValue = v.IsConst ? v.ConstantValue.Resolve(context).ConstantValue : null;
return new LocalResolveResult(v, v.Type.Resolve(context), constantValue);
}
}
@ -2788,7 +2788,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -2788,7 +2788,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// Specifies whether to allow treating single-dimensional arrays like compile-time constants.
/// This is used for attribute arguments.
/// </param>
public ArrayCreateResolveResult ResolveArrayCreation(IType elementType, int dimensions = 1, ResolveResult[] sizeArguments = null, ResolveResult[] initializerElements = null, bool allowArrayConstants = false)
public ArrayCreateResolveResult ResolveArrayCreation(IType elementType, int dimensions = 1, ResolveResult[] sizeArguments = null, ResolveResult[] initializerElements = null)
{
if (sizeArguments != null && dimensions != Math.Max(1, sizeArguments.Length))
throw new ArgumentException("dimensions and sizeArguments.Length don't match");
@ -2807,7 +2807,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -2807,7 +2807,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
initializerElements[i] = Convert(initializerElements[i], elementType);
}
}
return new ArrayCreateResolveResult(arrayType, sizeArguments, initializerElements, allowArrayConstants);
return new ArrayCreateResolveResult(arrayType, sizeArguments, initializerElements);
}
#endregion
}

120
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/ConstantValues.cs

@ -57,72 +57,56 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -57,72 +57,56 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
};
}
sealed class ConstantValueResult
{
public readonly IType ValueType;
public readonly object Value;
public ConstantValueResult(IType valueType, object value)
{
this.ValueType = valueType;
this.Value = value;
}
}
ConstantValueResult DoResolve(ITypeResolveContext context)
public ResolveResult Resolve(ITypeResolveContext context)
{
CacheManager cache = context.CacheManager;
if (cache != null) {
ConstantValueResult cachedResult = cache.GetShared(this) as ConstantValueResult;
ResolveResult cachedResult = cache.GetShared(this) as ResolveResult;
if (cachedResult != null)
return cachedResult;
}
CSharpResolver resolver = CreateResolver(context);
ResolveResult rr = expression.Resolve(resolver);
IType type = rr.Type;
object val = rr.ConstantValue;
if (resolver.Context != context) {
// Retrieve the equivalent type in the new resolve context.
// E.g. if the constant is defined in a .NET 2.0 project, type might be Int32 from mscorlib 2.0.
// However, the calling project might be a .NET 4.0 project, so we need to return Int32 from mscorlib 4.0.
type = type.AcceptVisitor(new MapTypeIntoNewContext(context));
// If 'val' is a type or an array containing types, we need to map it to the new context.
val = MapToNewContext(val, context);
}
ConstantValueResult result = new ConstantValueResult(type, val);
// Retrieve the equivalent type in the new resolve context.
// E.g. if the constant is defined in a .NET 2.0 project, type might be Int32 from mscorlib 2.0.
// However, the calling project might be a .NET 4.0 project, so we need to return Int32 from mscorlib 4.0.
rr = MapToNewContext(rr, new MapTypeIntoNewContext(context));
if (cache != null)
cache.SetShared(this, result);
return result;
}
public IType GetValueType(ITypeResolveContext context)
{
return DoResolve(context).ValueType;
}
public object GetValue(ITypeResolveContext context)
{
return DoResolve(context).Value;
cache.SetShared(this, rr);
return rr;
}
static ResolveResult MapToNewContext(ResolveResult rr, MapTypeIntoNewContext mapping)
{
if (rr is TypeOfResolveResult) {
return new TypeOfResolveResult(
rr.Type.AcceptVisitor(mapping),
((TypeOfResolveResult)rr).ReferencedType.AcceptVisitor(mapping));
} else if (rr is ArrayCreateResolveResult) {
ArrayCreateResolveResult acrr = (ArrayCreateResolveResult)rr;
return new ArrayCreateResolveResult(
acrr.Type.AcceptVisitor(mapping),
MapToNewContext(acrr.SizeArguments, mapping),
MapToNewContext(acrr.InitializerElements, mapping));
} else if (rr.IsCompileTimeConstant) {
return new ConstantResolveResult(
rr.Type.AcceptVisitor(mapping),
rr.ConstantValue
);
} else {
return new ErrorResolveResult(rr.Type.AcceptVisitor(mapping));
}
}
static object MapToNewContext(object val, ITypeResolveContext context)
static ResolveResult[] MapToNewContext(ResolveResult[] input, MapTypeIntoNewContext mapping)
{
IType type = val as IType;
if (type != null) {
return type.AcceptVisitor(new MapTypeIntoNewContext(context));
}
object[] arr = val as object[];
if (arr != null) {
object[] newArr = new object[arr.Length];
bool modified = false;
for (int i = 0; i < arr.Length; i++) {
newArr[i] = MapToNewContext(arr[i], context);
modified |= arr[i] != newArr[i];
}
if (modified)
return newArr;
if (input == null)
return null;
ResolveResult[] output = new ResolveResult[input.Length];
for (int i = 0; i < input.Length; i++) {
output[i] = MapToNewContext(input[i], mapping);
}
return val;
return output;
}
void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
@ -170,21 +154,19 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -170,21 +154,19 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
}
}
public IType GetValueType(ITypeResolveContext context)
public ResolveResult Resolve(ITypeResolveContext context)
{
return baseValue.GetValueType(context);
}
public object GetValue(ITypeResolveContext context)
{
object val = baseValue.GetValue(context);
if (val == null)
return null;
TypeCode typeCode = Type.GetTypeCode(val.GetType());
if (!(typeCode >= TypeCode.SByte && typeCode <= TypeCode.UInt64))
return null;
long intVal = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, val, false);
return CSharpPrimitiveCast.Cast(typeCode, unchecked(intVal + incrementAmount), false);
ResolveResult rr = baseValue.Resolve(context);
if (rr.IsCompileTimeConstant && rr.ConstantValue != null) {
object val = rr.ConstantValue;
TypeCode typeCode = Type.GetTypeCode(val.GetType());
if (typeCode >= TypeCode.SByte && typeCode <= TypeCode.UInt64) {
long intVal = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, val, false);
object newVal = CSharpPrimitiveCast.Cast(typeCode, unchecked(intVal + incrementAmount), false);
return new ConstantResolveResult(rr.Type, newVal);
}
}
return new ErrorResolveResult(rr.Type);
}
void ISupportsInterning.PrepareForInterning(IInterningProvider provider)
@ -672,9 +654,9 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues @@ -672,9 +654,9 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver.ConstantValues
elements[i] = arrayElements[i].Resolve(resolver);
}
if (elementType != null) {
return resolver.ResolveArrayCreation(elementType.Resolve(resolver.Context), 1, null, elements, true);
return resolver.ResolveArrayCreation(elementType.Resolve(resolver.Context), 1, null, elements);
} else {
return resolver.ResolveArrayCreation(null, 1, null, elements, true);
return resolver.ResolveArrayCreation(null, 1, null, elements);
}
}

13
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/FindReferences.cs

@ -31,7 +31,12 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -31,7 +31,12 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// <summary>
/// 'Find references' implementation.
/// </summary>
public class FindReferences
/// <remarks>
/// This class is thread-safe.
/// The intended multi-threaded usage is to call GetSearchScopes() once, and then
/// call FindReferencesInFile() concurrently on multiple threads (parallel foreach over all interesting files).
/// </remarks>
public sealed class FindReferences
{
#region Properties
/// <summary>
@ -214,7 +219,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -214,7 +219,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
}
#endregion
#region FindReferencesInSolution
#region GetInterestingFileNames
/// <summary>
/// Gets the file names that possibly contain references to the element being searched for.
/// </summary>
@ -275,7 +280,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -275,7 +280,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// <param name="compilationUnit">The compilation unit of the file being searched.</param>
/// <param name="context">The type resolve context to use for resolving the file.</param>
/// <param name="callback">Callback used to report the references that were found.</param>
public void FindReferencesInFile(IFindReferenceSearchScope searchScope, ParsedFile parsedFile, CompilationUnit compilationUnit,
public void FindReferencesInFile(IFindReferenceSearchScope searchScope, CSharpParsedFile parsedFile, CompilationUnit compilationUnit,
ITypeResolveContext context, FoundReferenceCallback callback)
{
if (searchScope == null)
@ -291,7 +296,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -291,7 +296,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// <param name="compilationUnit">The compilation unit of the file being searched.</param>
/// <param name="context">The type resolve context to use for resolving the file.</param>
/// <param name="callback">Callback used to report the references that were found.</param>
public void FindReferencesInFile(IList<IFindReferenceSearchScope> searchScopes, ParsedFile parsedFile, CompilationUnit compilationUnit,
public void FindReferencesInFile(IList<IFindReferenceSearchScope> searchScopes, CSharpParsedFile parsedFile, CompilationUnit compilationUnit,
ITypeResolveContext context, FoundReferenceCallback callback)
{
if (searchScopes == null)

2
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/MemberResolveResult.cs

@ -60,7 +60,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -60,7 +60,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
if (field != null) {
isConstant = field.IsConst;
if (isConstant)
constantValue = field.ConstantValue.GetValue(context);
constantValue = field.ConstantValue.Resolve(context).ConstantValue;
}
}

51
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/OperatorResolveResult.cs

@ -147,60 +147,11 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -147,60 +147,11 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
readonly object[] constantArray;
public ArrayCreateResolveResult(IType arrayType, ResolveResult[] sizeArguments, ResolveResult[] initializerElements,
bool allowArrayConstants)
public ArrayCreateResolveResult(IType arrayType, ResolveResult[] sizeArguments, ResolveResult[] initializerElements)
: base(arrayType)
{
this.SizeArguments = sizeArguments;
this.InitializerElements = initializerElements;
if (allowArrayConstants) {
this.constantArray = MakeConstantArray(sizeArguments, initializerElements);
}
}
static object[] MakeConstantArray(ResolveResult[] sizeArguments, ResolveResult[] initializerElements)
{
if (initializerElements == null)
return null;
for (int i = 0; i < initializerElements.Length; i++) {
if (!initializerElements[i].IsCompileTimeConstant)
return null;
}
if (sizeArguments != null && sizeArguments.Length > 0) {
if (sizeArguments.Length > 1) {
// 2D-arrays can't be constant
return null;
}
if (!sizeArguments[0].IsCompileTimeConstant)
return null;
int expectedSize;
try {
expectedSize = (int)CSharpPrimitiveCast.Cast(TypeCode.Int32, sizeArguments[0].ConstantValue, true);
} catch (InvalidCastException) {
return null;
} catch (OverflowException) {
return null;
}
if (expectedSize != initializerElements.Length)
return null;
}
object[] constants = new object[initializerElements.Length];
for (int i = 0; i < initializerElements.Length; i++) {
constants[i] = initializerElements[i].ConstantValue;
}
return constants;
}
public override object ConstantValue {
get { return constantArray; }
}
public override bool IsCompileTimeConstant {
get { return constantArray != null; }
}
public override IEnumerable<ResolveResult> GetChildResults()

2
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/ResolveAtLocation.cs

@ -28,7 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -28,7 +28,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// </summary>
public static class ResolveAtLocation
{
public static ResolveResult Resolve(ITypeResolveContext context, ParsedFile parsedFile, CompilationUnit cu, AstLocation location,
public static ResolveResult Resolve(ITypeResolveContext context, CSharpParsedFile parsedFile, CompilationUnit cu, AstLocation location,
CancellationToken cancellationToken = default(CancellationToken))
{
AstNode node = cu.GetNodeAt(location);

55
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs

@ -62,7 +62,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -62,7 +62,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
SimpleNameLookupMode currentTypeLookupMode = SimpleNameLookupMode.Type;
/// <summary>Resolve result of the current LINQ query</summary>
ResolveResult currentQueryResult;
readonly ParsedFile parsedFile;
readonly CSharpParsedFile parsedFile;
readonly Dictionary<AstNode, ResolveResult> resolveResultCache = new Dictionary<AstNode, ResolveResult>();
readonly Dictionary<AstNode, CSharpResolver> resolverBeforeDict = new Dictionary<AstNode, CSharpResolver>();
@ -89,7 +89,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -89,7 +89,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// If you pass <c>null</c>, then nothing will be resolved on the initial scan, and the resolver
/// will resolve additional nodes on demand (when one of the Get-methods is called).
/// </param>
public ResolveVisitor(CSharpResolver resolver, ParsedFile parsedFile, IResolveVisitorNavigator navigator = null)
public ResolveVisitor(CSharpResolver resolver, CSharpParsedFile parsedFile, IResolveVisitorNavigator navigator = null)
{
if (resolver == null)
throw new ArgumentNullException("resolver");
@ -374,14 +374,23 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -374,14 +374,23 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
if (resolveResultCache.TryGetValue(node, out result))
return result;
bool needResolveParent = (node.NodeType == NodeType.Token || IsVar(node));
AstNode nodeToResolve = node;
if (needResolveParent) {
nodeToResolve = node.Parent;
if (resolveResultCache.ContainsKey(nodeToResolve))
return null;
}
AstNode parent;
CSharpResolver storedResolver = GetPreviouslyScannedContext(node, out parent);
CSharpResolver storedResolver = GetPreviouslyScannedContext(nodeToResolve, out parent);
ResetContext(
storedResolver.Clone(),
delegate {
navigator = new NodeListResolveVisitorNavigator(node);
if (parent == node) {
Resolve(node);
navigator = new NodeListResolveVisitorNavigator(nodeToResolve);
if (parent == nodeToResolve) {
Resolve(nodeToResolve);
} else {
Debug.Assert(!resolverEnabled);
parent.AcceptVisitor(this, null);
@ -598,7 +607,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -598,7 +607,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
string identifier = variableInitializer.Name;
foreach (IVariable v in resolver.LocalVariables) {
if (v.Name == identifier) {
object constantValue = v.IsConst ? v.ConstantValue.GetValue(resolver.Context) : null;
object constantValue = v.IsConst ? v.ConstantValue.Resolve(resolver.Context).ConstantValue : null;
result = new LocalResolveResult(v, v.Type.Resolve(resolver.Context), constantValue);
break;
}
@ -927,18 +936,23 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -927,18 +936,23 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
int dimensions = arrayCreateExpression.Arguments.Count;
ResolveResult[] sizeArguments;
IEnumerable<ArraySpecifier> additionalArraySpecifiers;
if (dimensions == 0) {
dimensions = 1;
sizeArguments = null;
} else {
if (arrayCreateExpression.Arguments.All(e => e is EmptyExpression)) {
sizeArguments = null;
var firstSpecifier = arrayCreateExpression.AdditionalArraySpecifiers.FirstOrDefault();
if (firstSpecifier != null) {
dimensions = firstSpecifier.Dimensions;
additionalArraySpecifiers = arrayCreateExpression.AdditionalArraySpecifiers.Skip(1);
} else {
sizeArguments = new ResolveResult[dimensions];
int pos = 0;
foreach (var node in arrayCreateExpression.Arguments)
sizeArguments[pos++] = Resolve(node);
dimensions = 0;
additionalArraySpecifiers = arrayCreateExpression.AdditionalArraySpecifiers;
}
sizeArguments = null;
} else {
sizeArguments = new ResolveResult[dimensions];
int pos = 0;
foreach (var node in arrayCreateExpression.Arguments)
sizeArguments[pos++] = Resolve(node);
additionalArraySpecifiers = arrayCreateExpression.AdditionalArraySpecifiers;
}
List<Expression> initializerElements;
@ -962,7 +976,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -962,7 +976,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
acrr = resolver.ResolveArrayCreation(null, dimensions, sizeArguments, initializerElementResults);
} else {
IType elementType = ResolveType(arrayCreateExpression.Type);
foreach (var spec in arrayCreateExpression.AdditionalArraySpecifiers.Reverse()) {
foreach (var spec in additionalArraySpecifiers.Reverse()) {
elementType = new ArrayType(elementType, spec.Dimensions);
}
acrr = resolver.ResolveArrayCreation(elementType, dimensions, sizeArguments, initializerElementResults);
@ -1798,7 +1812,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -1798,7 +1812,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
readonly QuerySelectClause selectClause;
readonly CSharpResolver storedContext;
readonly ParsedFile parsedFile;
readonly CSharpParsedFile parsedFile;
readonly List<LambdaTypeHypothesis> hypotheses = new List<LambdaTypeHypothesis>();
readonly List<IParameter> parameters = new List<IParameter>();
@ -2343,6 +2357,9 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -2343,6 +2357,9 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
ITypeReference type;
if (needResolve) {
type = Resolve(vi.Initializer).Type;
if (!resolveResultCache.ContainsKey(variableDeclarationStatement.Type)) {
StoreResult(variableDeclarationStatement.Type, new TypeResolveResult(type.Resolve(resolver.Context)));
}
} else {
Scan(vi.Initializer);
type = MakeVarTypeReference(vi.Initializer, false);
@ -2528,7 +2545,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -2528,7 +2545,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
#endregion
#region Local Variable Type Inference
static bool IsVar(AstType returnType)
static bool IsVar(AstNode returnType)
{
SimpleType st = returnType as SimpleType;
return st != null && st.Identifier == "var" && st.TypeArguments.Count == 0;

44
src/Libraries/NRefactory/ICSharpCode.NRefactory/CSharp/Resolver/TypeOfResolveResult.cs

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Resolver
{
/// <summary>
/// Represents the 'typeof'.
/// </summary>
public class TypeOfResolveResult : ResolveResult
{
readonly IType referencedType;
public TypeOfResolveResult(IType systemType, IType referencedType)
: base(systemType)
{
this.referencedType = referencedType;
}
/// <summary>
/// The type referenced by the 'typeof'.
/// </summary>
public IType ReferencedType {
get { return referencedType; }
}
}
}

1
src/Libraries/NRefactory/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -115,6 +115,7 @@ @@ -115,6 +115,7 @@
<Compile Include="CSharp\Resolver\MapTypeIntoNewContext.cs" />
<Compile Include="CSharp\Resolver\ResolveAtLocation.cs" />
<Compile Include="CSharp\Resolver\SimpleNameLookupMode.cs" />
<Compile Include="CSharp\Resolver\TypeOfResolveResult.cs" />
<Compile Include="Documentation\IDStringProvider.cs" />
<Compile Include="PatternMatching\BacktrackingInfo.cs" />
<Compile Include="PatternMatching\Choice.cs" />

5
src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/IAttribute.cs

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using ICSharpCode.NRefactory.CSharp.Resolver;
namespace ICSharpCode.NRefactory.TypeSystem
{
@ -44,12 +45,12 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -44,12 +45,12 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// <summary>
/// Gets the positional arguments passed to the attribute.
/// </summary>
IList<IConstantValue> GetPositionalArguments(ITypeResolveContext context);
IList<ResolveResult> GetPositionalArguments(ITypeResolveContext context);
/// <summary>
/// Gets the named arguments passed to the attribute.
/// </summary>
IList<KeyValuePair<string, IConstantValue>> GetNamedArguments(ITypeResolveContext context);
IList<KeyValuePair<string, ResolveResult>> GetNamedArguments(ITypeResolveContext context);
/// <summary>
/// Resolves the constructor method used for this attribute invocation.

32
src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/IConstantValue.cs

@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
using System;
using System.Diagnostics.Contracts;
using ICSharpCode.NRefactory.CSharp.Resolver;
namespace ICSharpCode.NRefactory.TypeSystem
{
@ -27,40 +28,21 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -27,40 +28,21 @@ namespace ICSharpCode.NRefactory.TypeSystem
public interface IConstantValue : IFreezable
{
/// <summary>
/// Gets the type of the constant value.
/// Resolves the value of this constant.
/// </summary>
IType GetValueType(ITypeResolveContext context);
/// <summary>
/// Gets the .NET value of the constant value.
/// Possible return values are:
/// - null
/// - primitive integers
/// - float/double
/// - bool
/// - string
/// - IType (for typeof-expressions)
/// and arrays of these values. Enum values are returned using the underlying primitive integer.
///
/// TODO: how do we represent errors (value not available?)
/// </summary>
object GetValue(ITypeResolveContext context);
/// <param name="context">Type resolve context where the constant value will be used.</param>
/// <returns>Resolve result representing the constant value.</returns>
ResolveResult Resolve(ITypeResolveContext context);
}
#if WITH_CONTRACTS
[ContractClassFor(typeof(IConstantValue))]
abstract class IConstantValueContract : IFreezableContract, IConstantValue
{
IType IConstantValue.GetValueType(ITypeResolveContext context)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<IType>() != null);
return null;
}
object IConstantValue.GetValue(ITypeResolveContext context)
ResolveResult IConstantValue.Resolve(ITypeResolveContext context)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<ResolveResult>() != null);
return null;
}
}

9
src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultAttribute.cs

@ -21,6 +21,7 @@ using System.Collections.Generic; @@ -21,6 +21,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory.CSharp.Resolver;
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
@ -84,9 +85,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -84,9 +85,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
}
}
IList<IConstantValue> IAttribute.GetPositionalArguments(ITypeResolveContext context)
public IList<ResolveResult> GetPositionalArguments(ITypeResolveContext context)
{
return this.PositionalArguments;
return this.PositionalArguments.Select(a => a.Resolve(context)).ToList();
}
public IList<KeyValuePair<string, IConstantValue>> NamedArguments {
@ -97,9 +98,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -97,9 +98,9 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
}
}
IList<KeyValuePair<string, IConstantValue>> IAttribute.GetNamedArguments(ITypeResolveContext context)
public IList<KeyValuePair<string, ResolveResult>> GetNamedArguments(ITypeResolveContext context)
{
return this.NamedArguments;
return this.NamedArguments.Select(p => new KeyValuePair<string, ResolveResult>(p.Key, p.Value.Resolve(context))).ToList();
}
public IMethod ResolveConstructor(ITypeResolveContext context)

8
src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultParameter.cs

@ -105,14 +105,6 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -105,14 +105,6 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
}
}
public object GetDefaultValue(ITypeResolveContext context)
{
if (defaultValue == null)
throw new InvalidOperationException();
else
return defaultValue.GetValue(context);
}
public DomRegion Region {
get { return region; }
set {

17
src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleConstantValue.cs

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using System;
using ICSharpCode.NRefactory.CSharp.Resolver;
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
@ -37,17 +38,13 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -37,17 +38,13 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
this.value = value;
}
public IType GetValueType(ITypeResolveContext context)
public ResolveResult Resolve(ITypeResolveContext context)
{
return type.Resolve(context);
}
public object GetValue(ITypeResolveContext context)
{
if (value is ITypeReference)
return ((ITypeReference)value).Resolve(context);
else
return value;
if (value is ITypeReference) {
return new TypeOfResolveResult(type.Resolve(context), ((ITypeReference)value).Resolve(context));
} else {
return new ConstantResolveResult(type.Resolve(context), value);
}
}
public override string ToString()

9
src/Libraries/NRefactory/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleProjectContent.cs

@ -129,8 +129,13 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -129,8 +129,13 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
if (compoundTypeDef != null) {
// Remove one part from a compound class
var newParts = new List<ITypeDefinition>(compoundTypeDef.GetParts());
if (newParts.Remove(typeDefinition)) {
((DefaultTypeDefinition)typeDefinition).SetCompoundTypeDefinition(typeDefinition);
// We cannot use newParts.Remove() because we need to use reference equality
for (int i = 0; i < newParts.Count; i++) {
if (newParts[i] == typeDefinition) {
newParts.RemoveAt(i);
((DefaultTypeDefinition)typeDefinition).SetCompoundTypeDefinition(typeDefinition);
break;
}
}
types.UpdateType(CompoundTypeDefinition.Create(newParts));
} else {

11
src/Libraries/NRefactory/NRefactory.sln

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
# SharpDevelop 4.1.0.7854-beta
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DC98210E-1646-483B-819A-2BB8272461E4}"
ProjectSection(SolutionItems) = preProject
README = README
@ -13,8 +14,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.NRefactory.Test @@ -13,8 +14,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.NRefactory.Test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.NRefactory.VB", "ICSharpCode.NRefactory.VB\ICSharpCode.NRefactory.VB.csproj", "{7B82B671-419F-45F4-B778-D9286F996EFA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.NRefactory.VB.Tests", "ICSharpCode.NRefactory.VB.Tests\ICSharpCode.NRefactory.VB.Tests.csproj", "{870115DD-960A-4406-A6B9-600BCDC36A03}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Cecil", "..\Mono.Cecil\Mono.Cecil.csproj", "{D68133BD-1E63-496E-9EDE-4FBDBF77B486}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.NRefactory.Demo", "ICSharpCode.NRefactory.Demo\ICSharpCode.NRefactory.Demo.csproj", "{9C19E629-C93E-4ACB-9A4B-13072B5AEF9D}"
@ -53,14 +52,6 @@ Global @@ -53,14 +52,6 @@ Global
{7B82B671-419F-45F4-B778-D9286F996EFA}.Release|Any CPU.Build.0 = Release|Any CPU
{7B82B671-419F-45F4-B778-D9286F996EFA}.Release|x86.ActiveCfg = Release|Any CPU
{7B82B671-419F-45F4-B778-D9286F996EFA}.Release|x86.Build.0 = Release|Any CPU
{870115DD-960A-4406-A6B9-600BCDC36A03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{870115DD-960A-4406-A6B9-600BCDC36A03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{870115DD-960A-4406-A6B9-600BCDC36A03}.Debug|x86.ActiveCfg = Debug|x86
{870115DD-960A-4406-A6B9-600BCDC36A03}.Debug|x86.Build.0 = Debug|x86
{870115DD-960A-4406-A6B9-600BCDC36A03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{870115DD-960A-4406-A6B9-600BCDC36A03}.Release|Any CPU.Build.0 = Release|Any CPU
{870115DD-960A-4406-A6B9-600BCDC36A03}.Release|x86.ActiveCfg = Release|x86
{870115DD-960A-4406-A6B9-600BCDC36A03}.Release|x86.Build.0 = Release|x86
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug|Any CPU.ActiveCfg = net_4_0_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug|Any CPU.Build.0 = net_4_0_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug|x86.ActiveCfg = net_4_0_Debug|Any CPU

Loading…
Cancel
Save