Example #1
0
        public IEnumerable <JsType> Compile(PreparedCompilation compilation)
        {
            _compilation = compilation.Compilation;

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

            foreach (var f in compilation.SourceFiles)
            {
                try {
                    _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));
        }
Example #2
0
        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>>();

            var unsupportedConstructsScanner = new UnsupportedConstructsScanner(_errorReporter, compilation.Compilation.ReferencedAssemblies.Count == 0);
            bool hasUnsupported = false;

            foreach (var f in compilation.SourceFiles) {
                try {
                    if (!AllowUnsupportedConstructs) {
                        if (!unsupportedConstructsScanner.ProcessAndReturnTrueIfEverythingIsSupported(f.SyntaxTree)) {
                            hasUnsupported = true;
                            continue;
                        }
                    }
                    _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);
                }
            }

            if (hasUnsupported)
                return new JsType[0];	// Just to be safe

            // 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 IMethod FindEntryPoint(CompilerOptions options, ErrorReporterWrapper er, PreparedCompilation compilation) {
			if (options.HasEntryPoint) {
				List<IMethod> candidates;
				if (!string.IsNullOrEmpty(options.EntryPointClass)) {
					var t = compilation.Compilation.MainAssembly.GetTypeDefinition(new FullTypeName(options.EntryPointClass));
					if (t == null) {
						er.Region = DomRegion.Empty;
						er.Message(Messages._7950, "Could not find the entry point class " + options.EntryPointClass + ".");
						return null;
					}
					candidates = t.Methods.Where(IsEntryPointCandidate).ToList();
				}
				else {
					candidates =
						compilation.Compilation.MainAssembly.GetAllTypeDefinitions()
						           .SelectMany(t => t.Methods)
						           .Where(IsEntryPointCandidate)
						           .ToList();
				}
				if (candidates.Count != 1) {
					er.Region = DomRegion.Empty;
					er.Message(Messages._7950, "Could not find a unique entry point.");
					return null;
				}
				return candidates[0];
			}

			return null;
		}