private void Prepare(string source, Action preparer)
        {
            IProjectContent project = new CSharpProjectContent();
            var             parser  = new CSharpParser();

            using (var rdr = new StringReader(source)) {
                var pf = new CSharpUnresolvedFile {
                    FileName = "File.cs"
                };
                var syntaxTree = parser.Parse(rdr, pf.FileName);
                syntaxTree.AcceptVisitor(new TypeSystemConvertVisitor(pf));
                project = project.AddOrUpdateFiles(pf);
            }
            project = project.AddAssemblyReferences(new[] { Files.Mscorlib });

            var compilation = project.CreateCompilation();

            AllTypes = compilation.MainAssembly.TopLevelTypeDefinitions.SelectMany(SelfAndNested).ToDictionary(t => t.ReflectionName);

            var er = new MockErrorReporter(true);
            var s  = new AttributeStore(compilation, er);

            Metadata = new MetadataImporter(er, compilation, s, new CompilerOptions());
            preparer();
            Metadata.Prepare(compilation.GetAllTypeDefinitions());
            Assert.That(er.AllMessages, Is.Empty, "Should not generate errrors");
        }
Beispiel #2
0
        public static void Prepare(this IMetadataImporter md, IEnumerable <ITypeDefinition> types)
        {
            var l = types.ToList();

            foreach (var t in TopologicalSorter.TopologicalSort(l, l.SelectMany(GetBaseAndOuterTypeDefinitions, Edge.Create)))
            {
                md.Prepare(t);
            }
        }
        private void Prepare(string source, IRuntimeLibrary runtimeLibrary = null, bool expectErrors = false, bool MinimizeNames = false)
        {
            IProjectContent project = new CSharpProjectContent();
            var             parser  = new CSharpParser();

            using (var rdr = new StringReader(source)) {
                var pf = new CSharpUnresolvedFile()
                {
                    FileName = "File.cs"
                };
                var syntaxTree = parser.Parse(rdr, pf.FileName);
                syntaxTree.AcceptVisitor(new TypeSystemConvertVisitor(pf));
                project = project.AddOrUpdateFiles(pf);
            }
            project = project.AddAssemblyReferences(new[] { Files.Mscorlib, Files.Web, Files.Knockout });

            _compilation = project.CreateCompilation();

            var options = new CompilerOptions {
                MinimizeScript = MinimizeNames
            };

            _errorReporter = new MockErrorReporter(!expectErrors);
            var s = new AttributeStore(_compilation, _errorReporter);

            s.RunAttributeCode();
            _metadata = new MetadataImporter(_errorReporter, _compilation, s, options);

            _metadata.Prepare(_compilation.GetAllTypeDefinitions());

            _allErrors = _errorReporter.AllMessages.ToList().AsReadOnly();
            if (expectErrors)
            {
                Assert.That(_allErrors, Is.Not.Empty, "Compile should have generated errors");
            }
            else
            {
                Assert.That(_allErrors, Is.Empty, "Compile should not generate errors");
            }

            var rtl = new RuntimeLibrary(_metadata, _errorReporter, _compilation, new Namer(), s);
        }
		private void Prepare(string source, Action preparer) {
			IProjectContent project = new CSharpProjectContent();
			var parser = new CSharpParser();

			using (var rdr = new StringReader(source)) {
				var pf = new CSharpUnresolvedFile("File.cs");
				var syntaxTree = parser.Parse(rdr, pf.FileName);
				syntaxTree.AcceptVisitor(new TypeSystemConvertVisitor(pf));
				project = project.AddOrUpdateFiles(pf);
			}
			project = project.AddAssemblyReferences(new[] { Files.Mscorlib });

			var compilation = project.CreateCompilation();
			AllTypes = compilation.MainAssembly.TopLevelTypeDefinitions.SelectMany(SelfAndNested).ToDictionary(t => t.ReflectionName);

			var er = new MockErrorReporter(true);
			Metadata = new MetadataImporter(er, compilation, new CompilerOptions());
			preparer();
			Metadata.Prepare(compilation.GetAllTypeDefinitions());
			Assert.That(er.AllMessages, Is.Empty, "Should not generate errrors");
		}
 public virtual void Prepare(ITypeDefinition type)
 {
     _prev.Prepare(type);
 }
        public IEnumerable <JsType> Compile(PreparedCompilation compilation)
        {
            _compilation = compilation.Compilation;

            _metadataImporter.Prepare(_compilation.GetAllTypeDefinitions(), _compilation.MainAssembly, _errorReporter);

            _types = new Dictionary <ITypeDefinition, JsClass>();
            _constructorDeclarations = new HashSet <Tuple <ConstructorDeclaration, CSharpAstResolver> >();
            _instanceInitStatements  = new Dictionary <JsClass, List <JsStatement> >();

            foreach (var f in compilation.SourceFiles)
            {
                try {
                    _definedSymbols = f.DefinedSymbols;

                    _resolver = new CSharpAstResolver(_compilation, f.SyntaxTree, f.ParsedFile);
                    _resolver.ApplyNavigator(new ResolveAllNavigator());
                    f.SyntaxTree.AcceptVisitor(this);
                }
                catch (Exception ex) {
                    _errorReporter.Region = _currentNode.GetRegion();
                    _errorReporter.InternalError(ex);
                }
            }

            // Handle constructors. We must do this after we have visited all the compilation units because field initializer (which change the InstanceInitStatements and StaticInitStatements) might appear anywhere.
            foreach (var n in _constructorDeclarations)
            {
                try {
                    _resolver = n.Item2;
                    HandleConstructorDeclaration(n.Item1);
                }
                catch (Exception ex) {
                    _errorReporter.Region = n.Item1.GetRegion();
                    _errorReporter.InternalError(ex);
                }
            }

            // Add default constructors where needed.
            foreach (var toAdd in _types.Where(t => t.Value != null).SelectMany(kvp => kvp.Key.GetConstructors().Where(c => c.IsSynthetic).Select(c => new { jsClass = kvp.Value, c })))
            {
                try {
                    MaybeAddDefaultConstructorToType(toAdd.jsClass, toAdd.c);
                }
                catch (Exception ex) {
                    _errorReporter.Region = toAdd.c.Region;
                    _errorReporter.InternalError(ex, "Error adding default constructor to type");
                }
            }

            _types.Values.Where(t => t != null).ForEach(t => t.Freeze());

            var enums = new List <JsType>();

            foreach (var e in _compilation.MainAssembly.TopLevelTypeDefinitions.SelectMany(SelfAndNested).Where(t => t.Kind == TypeKind.Enum))
            {
                try {
                    enums.Add(ConvertEnum(e.GetDefinition()));
                }
                catch (Exception ex) {
                    _errorReporter.Region = e.GetDefinition().Region;
                    _errorReporter.InternalError(ex);
                }
            }

            return(_types.Values.Concat(enums).Where(t => t != null));
        }
        private void Prepare(string source, IRuntimeLibrary runtimeLibrary = null, bool expectErrors = false, bool MinimizeNames = false)
        {
            IProjectContent project = new CSharpProjectContent();
            var parser = new CSharpParser();

            using (var rdr = new StringReader(source)) {
                var pf = new CSharpUnresolvedFile() { FileName = "File.cs" };
                var syntaxTree = parser.Parse(rdr, pf.FileName);
                syntaxTree.AcceptVisitor(new TypeSystemConvertVisitor(pf));
                project = project.AddOrUpdateFiles(pf);
            }
            project = project.AddAssemblyReferences(new[] { Files.Mscorlib, Files.Web, Files.Knockout });

            _compilation = project.CreateCompilation();

            var options = new CompilerOptions { MinimizeScript = MinimizeNames };
            _errorReporter = new MockErrorReporter(!expectErrors);
            var s = new AttributeStore(_compilation, _errorReporter);
            s.RunAttributeCode();
            _metadata = new MetadataImporter(_errorReporter, _compilation, s, options);

            _metadata.Prepare(_compilation.GetAllTypeDefinitions());

            _allErrors = _errorReporter.AllMessages.ToList().AsReadOnly();
            if (expectErrors) {
                Assert.That(_allErrors, Is.Not.Empty, "Compile should have generated errors");
            }
            else {
                Assert.That(_allErrors, Is.Empty, "Compile should not generate errors");
            }

            var rtl = new RuntimeLibrary(_metadata, _errorReporter, _compilation, new Namer(), s);
        }