Пример #1
0
        public void ExpansiveInheritance()
        {
            var a = new DefaultUnresolvedTypeDefinition(string.Empty, "A");
            var b = new DefaultUnresolvedTypeDefinition(string.Empty, "B");

            // interface A<in U>
            a.Kind = TypeKind.Interface;
            a.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "U")
            {
                Variance = VarianceModifier.Contravariant
            });
            // interface B<X> : A<A<B<X>>> { }
            b.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "X"));
            b.BaseTypes.Add(new ParameterizedTypeReference(
                                a, new[] { new ParameterizedTypeReference(
                                               a, new [] { new ParameterizedTypeReference(
                                                               b, new [] { new TypeParameterReference(EntityType.TypeDefinition, 0) }
                                                               ) }) }));

            ICompilation    compilation = new SimpleCompilation(CecilLoaderTests.Mscorlib);
            ITypeDefinition resolvedA   = compilation.MainAssembly.GetTypeDefinition(a);
            ITypeDefinition resolvedB   = compilation.MainAssembly.GetTypeDefinition(b);

            IType type1 = new ParameterizedType(resolvedB, new[] { compilation.FindType(KnownTypeCode.Double) });
            IType type2 = new ParameterizedType(resolvedA, new [] { new ParameterizedType(resolvedB, new[] { compilation.FindType(KnownTypeCode.String) }) });

            Assert.IsFalse(conversions.ImplicitConversion(type1, type2));
        }
Пример #2
0
        static IEnumerable <ITypeDefinition> LoadTypes()
        {
            var tt = typeof(File).Assembly.Location;

            Reflector.OutputDir = Environment.CurrentDirectory;

            var asmFile = Assembly.GetExecutingAssembly().Location;
            //asmFile = "".GetType().Assembly.Location;

            string xmlFile = Path.GetFullPath(Path.GetFileNameWithoutExtension(asmFile) + ".xml");

            XmlDocumentationProvider doc = MonoCompletionEngine.GetXmlDocumentation(asmFile);

            if (doc == null && File.Exists(xmlFile))
            {
                doc = new XmlDocumentationProvider(xmlFile);
            }

            var loader = new CecilLoader {
                DocumentationProvider = doc
            };
            var unresolvedAsm = loader.LoadAssemblyFile(asmFile);
            var compilation   = new SimpleCompilation(unresolvedAsm);
            var context       = new SimpleTypeResolveContext(compilation);
            var asm           = unresolvedAsm.Resolve(context);

            return(asm.GetAllTypeDefinitions().ToArray());
        }
Пример #3
0
        public virtual ICompilation CreateCompilation()
        {
            var          solutionSnapshot = new DefaultSolutionSnapshot();
            ICompilation compilation      = new SimpleCompilation(solutionSnapshot, this, assemblyReferences);

            solutionSnapshot.AddCompilation(this, compilation);
            return(compilation);
        }
Пример #4
0
        public void FindRedirectedType()
        {
            var compilationWithSystemCore = new SimpleCompilation(systemCore.Value, mscorlib.Value);

            var             typeRef = ReflectionHelper.ParseReflectionName("System.Func`2, System.Core");
            ITypeDefinition c       = typeRef.Resolve(compilationWithSystemCore.TypeResolveContext).GetDefinition();

            Assert.IsNotNull(c, "System.Func<,> not found");
            Assert.AreEqual("mscorlib", c.ParentAssembly.AssemblyName);
        }
Пример #5
0
        ICompilation CreateCompilation()
        {
            var solutionSnapshot = new TestableSolutionSnapshot(msbuildProject);

            msbuildProject.SetProjectContent(projectContent);
            ICompilation compilation = new SimpleCompilation(solutionSnapshot, projectContent, projectContent.AssemblyReferences);

            solutionSnapshot.AddCompilation(projectContent, compilation);
            return(compilation);
        }
Пример #6
0
        public void FixtureSetUp()
        {
            // use "IncludeInternalMembers" so that Cecil results match C# parser results
            CecilLoader loader = new CecilLoader()
            {
                IncludeInternalMembers = true
            };
            IUnresolvedAssembly asm = loader.LoadAssemblyFile(typeof(TestCase.SimplePublicClass).Assembly.Location);

            compilation = new SimpleCompilation(asm, CecilLoaderTests.Mscorlib);
        }
Пример #7
0
        public void CorlibIDStrings()
        {
            var list = new SimpleCompilation(CecilLoaderTests.Mscorlib).FindType(typeof(List <>)).GetDefinition();

            Assert.AreEqual("T:System.Collections.Generic.List`1",
                            IdStringProvider.GetIdString(list));
            Assert.AreEqual("M:System.Collections.Generic.List`1.Add(`0)",
                            IdStringProvider.GetIdString(list.Methods.Single(m => m.Name == "Add")));
            Assert.AreEqual("M:System.Collections.Generic.List`1.AddRange(System.Collections.Generic.IEnumerable{`0})",
                            IdStringProvider.GetIdString(list.Methods.Single(m => m.Name == "AddRange")));
            Assert.AreEqual("M:System.Collections.Generic.List`1.ConvertAll``1(System.Converter{`0,``0})",
                            IdStringProvider.GetIdString(list.Methods.Single(m => m.Name == "ConvertAll")));
        }
        protected IType Resolve(ITreeNavigator treeBuilder, IUnresolvedTypeDefinition type)
        {
            var mainAssembly = GetMainAssembly(treeBuilder);

            if (mainAssembly != null)
            {
                var simpleCompilation = new SimpleCompilation(mainAssembly);
                return(type.Resolve(new SimpleTypeResolveContext(simpleCompilation.MainAssembly)));
            }
            var project = (Project)treeBuilder.GetParentDataItem(typeof(Project), true);
            var ctx     = TypeSystemService.GetCompilation(project);

            return(ctx.MainAssembly.GetTypeDefinition(type.Namespace, type.Name, type.TypeParameters.Count));
        }
        static Value Evaluate(StackFrame frame, Thread evalThread, string code, string contextCode)
        {
            if (frame == null || frame.NextStatement == null)
            {
                throw new GetValueException("no stackframe available!");
            }
            var location           = frame.NextStatement;
            var debuggerTypeSystem = frame.AppDomain.Compilation;
            var compilation        = new SimpleCompilation(debuggerTypeSystem.MainAssembly.UnresolvedAssembly, debuggerTypeSystem.ReferencedAssemblies.Select(a => a.UnresolvedAssembly));
            var rr = ResolveSnippet(location.Filename, new TextLocation(location.StartLine, location.StartColumn),
                                    contextCode, code, compilation);

            return(new ExpressionEvaluationVisitor(frame, evalThread, frame.AppDomain.Compilation, true, true).Convert(rr));
        }
        protected ITypeResolveContext GetContext(ITreeNavigator treeBuilder)
        {
            var mainAssembly = GetMainAssembly(treeBuilder);

            if (mainAssembly != null)
            {
                var simpleCompilation = new SimpleCompilation(mainAssembly);
                return(new SimpleTypeResolveContext(simpleCompilation.MainAssembly));
            }
            var project     = (Project)treeBuilder.GetParentDataItem(typeof(Project), true);
            var compilation = TypeSystemService.GetCompilation(project);

            return(new SimpleTypeResolveContext(compilation.MainAssembly));
        }
        public void ConstructorsAreReportedAsJsonConstructors()
        {
            var compilation = new SimpleCompilation(new CSharpProjectContent());
            var er          = new MockErrorReporter(true);
            var md          = new MetadataImporter(er, compilation, new CompilerOptions());

            Assert.That(er.AllMessages, Is.Empty, "Prepare should not generate errors");

            var t = CreateType(compilation);

            var c = md.GetConstructorSemantics(DefaultResolvedMethod.GetDummyConstructor(compilation, t));

            Assert.That(c.Type, Is.EqualTo(ConstructorScriptSemantics.ImplType.Json));
        }
        public void TransparentIdentiferIsValidJavascriptIdentifierStartingWithDollar()
        {
            var compilation = new SimpleCompilation(new CSharpProjectContent());
            var er          = new MockErrorReporter(true);
            var md          = new MetadataImporter(er, compilation, new CompilerOptions());

            Assert.That(er.AllMessages, Is.Empty, "Prepare should not generate errors");

            var t = CreateType(compilation, new[] { "<>Identifier" });

            var c = md.GetPropertySemantics(t.GetProperties().Single());

            Assert.That(c.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
            Assert.That(c.FieldName, Is.EqualTo("$Identifier"));
        }
Пример #13
0
 public void FixtureSetUp()
 {
     try {
         // use "IncludeInternalMembers" so that Cecil results match C# parser results
         IkvmLoader loader = new IkvmLoader()
         {
             IncludeInternalMembers = true
         };
         IUnresolvedAssembly asm = loader.LoadAssemblyFile(typeof(TestCase.SimplePublicClass).Assembly.Location);
         compilation = new SimpleCompilation(asm, IkvmLoaderTests.Mscorlib);
     } catch (Exception e) {
         Console.WriteLine(e);
         throw;
     }
 }
        public void AnonymousTypePropertyNamesAreNotMinimized()
        {
            var compilation = new SimpleCompilation(new CSharpProjectContent());
            var er          = new MockErrorReporter(true);
            var md          = new MetadataImporter(er, compilation, new CompilerOptions());

            Assert.That(er.AllMessages, Is.Empty, "Prepare should not generate errors");

            var t = CreateType(compilation);

            var p1 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "prop1"));

            Assert.That(p1.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
            Assert.That(p1.FieldName, Is.EqualTo("prop1"));

            var p2 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "Prop2"));

            Assert.That(p2.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
            Assert.That(p2.FieldName, Is.EqualTo("Prop2"));
        }
Пример #15
0
        public void TypeParameterDerivingFromOtherTypeParameterInheritsEffectiveBaseClass()
        {
            // class C<T, U> where T : List<string> where U : T
            var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");

            c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T")
            {
                Constraints = { typeof(List <string>).ToTypeReference() }
            });
            c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U")
            {
                Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) }
            });

            ITypeDefinition resolvedC = new SimpleCompilation(CecilLoaderTests.Mscorlib).MainAssembly.GetTypeDefinition(c);

            Assert.AreEqual(true, resolvedC.TypeParameters[0].IsReferenceType);
            Assert.AreEqual(true, resolvedC.TypeParameters[1].IsReferenceType);
            Assert.AreEqual("System.Collections.Generic.List`1[[System.String]]", resolvedC.TypeParameters[0].EffectiveBaseClass.ReflectionName);
            Assert.AreEqual("System.Collections.Generic.List`1[[System.String]]", resolvedC.TypeParameters[1].EffectiveBaseClass.ReflectionName);
        }
Пример #16
0
        public void TypeParameterDerivingFromOtherTypeParameterDoesNotInheritReferenceConstraint()
        {
            // class C<T, U> where T : class where U : T
            var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C");

            c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T")
            {
                HasReferenceTypeConstraint = true
            });
            c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U")
            {
                Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) }
            });

            ITypeDefinition resolvedC = new SimpleCompilation(CecilLoaderTests.Mscorlib).MainAssembly.GetTypeDefinition(c);

            // At runtime, we might have T=System.ValueType and U=int, so C# can't inherit the 'class' constraint
            // from one type parameter to another.
            Assert.AreEqual(true, resolvedC.TypeParameters[0].IsReferenceType);
            Assert.IsNull(resolvedC.TypeParameters[1].IsReferenceType);
        }
Пример #17
0
        public void PropertiesAreImplementedAsFieldsWithTheSameName()
        {
            var compilation = new SimpleCompilation(new CSharpProjectContent());
            var er          = new MockErrorReporter(true);
            var s           = new AttributeStore(compilation, er);
            var md          = new MetadataImporter(er, compilation, s, new CompilerOptions());

            Assert.That(er.AllMessages, Is.Empty, "Prepare should not generate errors");

            var t = CreateType(compilation);

            var p1 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "prop1"));

            Assert.That(p1.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
            Assert.That(p1.FieldName, Is.EqualTo("prop1"));

            var p2 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "Prop2"));

            Assert.That(p2.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
            Assert.That(p2.FieldName, Is.EqualTo("Prop2"));
        }
Пример #18
0
        static ICompilation GetSystemWebDom(TargetRuntime runtime, TargetFramework targetFramework)
        {
            string file = runtime.AssemblyContext.GetAssemblyNameForVersion(sysWebAssemblyName, targetFramework);

            if (string.IsNullOrEmpty(file))
            {
                throw new Exception("System.Web assembly name not found for framework " + targetFramework.Id);
            }
            file = runtime.AssemblyContext.GetAssemblyLocation(file, targetFramework);
            if (string.IsNullOrEmpty(file))
            {
                throw new Exception("System.Web assembly file not found for framework " + targetFramework.Id);
            }
            var dom = new SimpleCompilation(TypeSystemService.LoadAssemblyContext(runtime, targetFramework, file));

            if (dom == null)
            {
                throw new Exception("System.Web parse database not found for framework " + targetFramework.Id + " file '" + file + "'");
            }
            return(dom);
        }
        public void PropertiesAreImplementedAsFieldsWithTheSameName()
        {
            var compilation = new SimpleCompilation(new CSharpProjectContent());
            var md          = new MetadataImporter.ScriptSharpMetadataImporter(false);
            var er          = new MockErrorReporter(true);

            md.Prepare(new ITypeDefinition[0], compilation.MainAssembly, er);
            Assert.That(er.AllMessagesText, Is.Empty, "Prepare should not generate errors");

            var t = CreateType(compilation);

            var p1 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "prop1"));

            Assert.That(p1.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
            Assert.That(p1.FieldName, Is.EqualTo("prop1"));

            var p2 = md.GetPropertySemantics(t.GetProperties().Single(p => p.Name == "Prop2"));

            Assert.That(p2.Type, Is.EqualTo(PropertyScriptSemantics.ImplType.Field));
            Assert.That(p2.FieldName, Is.EqualTo("Prop2"));
        }
Пример #20
0
 public void FixtureSetUp()
 {
     compilation = new SimpleCompilation(TestAssembly,
                                         Mscorlib.WithOptions(TypeSystemOptions.Default));
     language = new CSharpLanguage();
 }
Пример #21
0
 void HandleCommandLineArgumentsAfterShowList(CommandLineArguments args)
 {
     if (nugetPackagesToLoad.Count > 0)
     {
         LoadAssemblies(nugetPackagesToLoad, commandLineLoadedAssemblies, focusNode: false);
         nugetPackagesToLoad.Clear();
     }
     if (args.NavigateTo != null)
     {
         bool found = false;
         if (args.NavigateTo.StartsWith("N:", StringComparison.Ordinal))
         {
             string namespaceName = args.NavigateTo.Substring(2);
             foreach (LoadedAssembly asm in commandLineLoadedAssemblies)
             {
                 AssemblyTreeNode asmNode = assemblyListTreeNode.FindAssemblyNode(asm);
                 if (asmNode != null)
                 {
                     NamespaceTreeNode nsNode = asmNode.FindNamespaceNode(namespaceName);
                     if (nsNode != null)
                     {
                         found = true;
                         SelectNode(nsNode);
                         break;
                     }
                 }
             }
         }
         else
         {
             foreach (LoadedAssembly asm in commandLineLoadedAssemblies)
             {
                 var def = asm.GetPEFileOrNull();
                 if (def != null)
                 {
                     var compilation = new SimpleCompilation(def, MinimalCorlib.Instance);
                     var mr          = IdStringProvider.FindEntity(args.NavigateTo, new SimpleTypeResolveContext(compilation));
                     if (mr != null)
                     {
                         found = true;
                         // Defer JumpToReference call to allow an assembly that was loaded while
                         // resolving a type-forwarder in FindMemberByKey to appear in the assembly list.
                         Dispatcher.BeginInvoke(new Action(() => JumpToReference(mr)), DispatcherPriority.Loaded);
                         break;
                     }
                 }
             }
         }
         if (!found)
         {
             AvalonEditTextOutput output = new AvalonEditTextOutput();
             output.Write(string.Format("Cannot find '{0}' in command line specified assemblies.", args.NavigateTo));
             decompilerTextView.ShowText(output);
         }
     }
     else if (commandLineLoadedAssemblies.Count == 1)
     {
         // NavigateTo == null and an assembly was given on the command-line:
         // Select the newly loaded assembly
         JumpToReference(commandLineLoadedAssemblies[0].GetPEFileOrNull());
     }
     if (args.Search != null)
     {
         SearchPane.Instance.SearchTerm = args.Search;
         SearchPane.Instance.Show();
     }
     commandLineLoadedAssemblies.Clear();             // clear references once we don't need them anymore
 }