void tokenize_file (SourceFile sourceFile, ModuleContainer module, ParserSession session) { Stream input; try { input = File.OpenRead (sourceFile.Name); } catch { Report.Error (2001, "Source file `" + sourceFile.Name + "' could not be found"); return; } using (input){ SeekableStreamReader reader = new SeekableStreamReader (input, ctx.Settings.Encoding); var file = new CompilationSourceFile (module, sourceFile); Tokenizer lexer = new Tokenizer (reader, file, session, ctx.Report); int token, tokens = 0, errors = 0; while ((token = lexer.token ()) != Token.EOF){ tokens++; if (token == Token.ERROR) errors++; } Console.WriteLine ("Tokenized: " + tokens + " found " + errors + " errors"); } return; }
public DocumentationBuilder (ModuleContainer module) { doc_module = new ModuleContainer (module.Compiler); doc_module.DocumentationBuilder = this; this.module = module; XmlDocumentation = new XmlDocument (); XmlDocumentation.PreserveWhitespace = false; }
public Evaluator (CompilerContext ctx) { this.ctx = ctx; module = new ModuleContainer (ctx); module.Evaluator = this; source_file = new CompilationSourceFile (module, null); module.AddTypeContainer (source_file); startup_files = ctx.SourceFiles.Count; // FIXME: Importer needs this assembly for internalsvisibleto module.SetDeclaringAssembly (new AssemblyDefinitionDynamic (module, "evaluator")); importer = new ReflectionImporter (module, ctx.BuiltinTypes); InteractiveBaseClass = typeof (InteractiveBase); fields = new Dictionary<string, Tuple<FieldSpec, FieldInfo>> (); }
void Parse (ModuleContainer module) { bool tokenize_only = module.Compiler.Settings.TokenizeOnly; var sources = module.Compiler.SourceFiles; Location.Initialize (sources); var session = new ParserSession { UseJayGlobalArrays = true, LocatedTokens = new LocatedToken[15000] }; for (int i = 0; i < sources.Count; ++i) { if (tokenize_only) { tokenize_file (sources[i], module, session); } else { Parse (sources[i], module, session, Report); } } }
public StaticDataContainer (ModuleContainer module) : base (module, new MemberName ("<PrivateImplementationDetails>" + module.builder.ModuleVersionId.ToString ("B"), Location.Null), Modifiers.STATIC | Modifiers.INTERNAL) { size_types = new Dictionary<int, Struct> (); }
// // Main compilation method // public bool Compile() { #if NETSTANDARD2_0 throw new PlatformNotSupportedException(); #else var settings = ctx.Settings; // // If we are an exe, require a source file for the entry point or // if there is nothing to put in the assembly, and we are not a library // if (settings.FirstSourceFile == null && ((settings.Target == Target.Exe || settings.Target == Target.WinExe || settings.Target == Target.Module) || settings.Resources == null)) { Report.Error(2008, "No files to compile were specified"); return(false); } if (settings.Platform == Platform.AnyCPU32Preferred && (settings.Target == Target.Library || settings.Target == Target.Module)) { Report.Error(4023, "Platform option `anycpu32bitpreferred' is valid only for executables"); return(false); } TimeReporter tr = new TimeReporter(settings.Timestamps); ctx.TimeReporter = tr; tr.StartTotal(); var module = new ModuleContainer(ctx); RootContext.ToplevelTypes = module; tr.Start(TimeReporter.TimerType.ParseTotal); Parse(module); tr.Stop(TimeReporter.TimerType.ParseTotal); if (Report.Errors > 0) { return(false); } if (settings.TokenizeOnly || settings.ParseOnly) { tr.StopTotal(); tr.ShowStats(); return(true); } var output_file = settings.OutputFile; string output_file_name; if (output_file == null) { var source_file = settings.FirstSourceFile; if (source_file == null) { Report.Error(1562, "If no source files are specified you must specify the output file with -out:"); return(false); } output_file_name = source_file.Name; int pos = output_file_name.LastIndexOf('.'); if (pos > 0) { output_file_name = output_file_name.Substring(0, pos); } output_file_name += settings.TargetExt; output_file = output_file_name; } else { output_file_name = Path.GetFileName(output_file); if (string.IsNullOrEmpty(Path.GetFileNameWithoutExtension(output_file_name)) || output_file_name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) { Report.Error(2021, "Output file name is not valid"); return(false); } } #if STATIC var importer = new StaticImporter(module); var references_loader = new StaticLoader(importer, ctx); tr.Start(TimeReporter.TimerType.AssemblyBuilderSetup); var assembly = new AssemblyDefinitionStatic(module, references_loader, output_file_name, output_file); assembly.Create(references_loader.Domain); tr.Stop(TimeReporter.TimerType.AssemblyBuilderSetup); // Create compiler types first even before any referenced // assembly is loaded to allow forward referenced types from // loaded assembly into compiled builder to be resolved // correctly tr.Start(TimeReporter.TimerType.CreateTypeTotal); module.CreateContainer(); importer.AddCompiledAssembly(assembly); references_loader.CompiledAssembly = assembly; tr.Stop(TimeReporter.TimerType.CreateTypeTotal); references_loader.LoadReferences(module); tr.Start(TimeReporter.TimerType.PredefinedTypesInit); if (!ctx.BuiltinTypes.CheckDefinitions(module)) { return(false); } tr.Stop(TimeReporter.TimerType.PredefinedTypesInit); references_loader.LoadModules(assembly, module.GlobalRootNamespace); #else var assembly = new AssemblyDefinitionDynamic(module, output_file_name, output_file); module.SetDeclaringAssembly(assembly); var importer = new ReflectionImporter(module, ctx.BuiltinTypes); assembly.Importer = importer; var loader = new DynamicLoader(importer, ctx); loader.LoadReferences(module); if (!ctx.BuiltinTypes.CheckDefinitions(module)) { return(false); } if (!assembly.Create(AppDomain.CurrentDomain, AssemblyBuilderAccess.Save)) { return(false); } module.CreateContainer(); loader.LoadModules(assembly, module.GlobalRootNamespace); #endif module.InitializePredefinedTypes(); tr.Start(TimeReporter.TimerType.ModuleDefinitionTotal); module.Define(); tr.Stop(TimeReporter.TimerType.ModuleDefinitionTotal); if (Report.Errors > 0) { return(false); } if (settings.DocumentationFile != null) { var doc = new DocumentationBuilder(module); doc.OutputDocComment(output_file, settings.DocumentationFile); } assembly.Resolve(); if (Report.Errors > 0) { return(false); } tr.Start(TimeReporter.TimerType.EmitTotal); assembly.Emit(); tr.Stop(TimeReporter.TimerType.EmitTotal); if (Report.Errors > 0) { return(false); } tr.Start(TimeReporter.TimerType.CloseTypes); module.CloseContainer(); tr.Stop(TimeReporter.TimerType.CloseTypes); tr.Start(TimeReporter.TimerType.Resouces); if (!settings.WriteMetadataOnly) { assembly.EmbedResources(); } tr.Stop(TimeReporter.TimerType.Resouces); if (Report.Errors > 0) { return(false); } assembly.Save(); #if STATIC references_loader.Dispose(); #endif tr.StopTotal(); tr.ShowStats(); return(Report.Errors == 0); }
protected MetadataImporter (ModuleContainer module) { this.module = module; import_cache = new Dictionary<MetaType, TypeSpec> (1024, ReferenceEquality<MetaType>.Default); compiled_types = new Dictionary<MetaType, TypeSpec> (40, ReferenceEquality<MetaType>.Default); assembly_2_definition = new Dictionary<Assembly, IAssemblyDefinition> (ReferenceEquality<Assembly>.Default); IgnorePrivateMembers = true; }
// // Main compilation method // public bool Compile () { var settings = ctx.Settings; // // If we are an exe, require a source file for the entry point or // if there is nothing to put in the assembly, and we are not a library // if (settings.FirstSourceFile == null && ((settings.Target == Target.Exe || settings.Target == Target.WinExe || settings.Target == Target.Module) || settings.Resources == null)) { Report.Error (2008, "No files to compile were specified"); return false; } if (settings.Platform == Platform.AnyCPU32Preferred && (settings.Target == Target.Library || settings.Target == Target.Module)) { Report.Error (4023, "Platform option `anycpu32bitpreferred' is valid only for executables"); return false; } TimeReporter tr = new TimeReporter (settings.Timestamps); ctx.TimeReporter = tr; tr.StartTotal (); var module = new ModuleContainer (ctx); RootContext.ToplevelTypes = module; tr.Start (TimeReporter.TimerType.ParseTotal); Parse (module); tr.Stop (TimeReporter.TimerType.ParseTotal); if (Report.Errors > 0) return false; if (settings.TokenizeOnly || settings.ParseOnly) { tr.StopTotal (); tr.ShowStats (); return true; } var output_file = settings.OutputFile; string output_file_name; if (output_file == null) { var source_file = settings.FirstSourceFile; if (source_file == null) { Report.Error (1562, "If no source files are specified you must specify the output file with -out:"); return false; } output_file_name = source_file.Name; int pos = output_file_name.LastIndexOf ('.'); if (pos > 0) output_file_name = output_file_name.Substring (0, pos); output_file_name += settings.TargetExt; output_file = output_file_name; } else { output_file_name = Path.GetFileName (output_file); if (string.IsNullOrEmpty (Path.GetFileNameWithoutExtension (output_file_name)) || output_file_name.IndexOfAny (Path.GetInvalidFileNameChars ()) >= 0) { Report.Error (2021, "Output file name is not valid"); return false; } } #if STATIC var importer = new StaticImporter (module); var references_loader = new StaticLoader (importer, ctx); tr.Start (TimeReporter.TimerType.AssemblyBuilderSetup); var assembly = new AssemblyDefinitionStatic (module, references_loader, output_file_name, output_file); assembly.Create (references_loader.Domain); tr.Stop (TimeReporter.TimerType.AssemblyBuilderSetup); // Create compiler types first even before any referenced // assembly is loaded to allow forward referenced types from // loaded assembly into compiled builder to be resolved // correctly tr.Start (TimeReporter.TimerType.CreateTypeTotal); module.CreateContainer (); importer.AddCompiledAssembly (assembly); references_loader.CompiledAssembly = assembly; tr.Stop (TimeReporter.TimerType.CreateTypeTotal); references_loader.LoadReferences (module); tr.Start (TimeReporter.TimerType.PredefinedTypesInit); if (!ctx.BuiltinTypes.CheckDefinitions (module)) return false; tr.Stop (TimeReporter.TimerType.PredefinedTypesInit); references_loader.LoadModules (assembly, module.GlobalRootNamespace); #else var assembly = new AssemblyDefinitionDynamic (module, output_file_name, output_file); module.SetDeclaringAssembly (assembly); var importer = new ReflectionImporter (module, ctx.BuiltinTypes); assembly.Importer = importer; var loader = new DynamicLoader (importer, ctx); loader.LoadReferences (module); if (!ctx.BuiltinTypes.CheckDefinitions (module)) return false; if (!assembly.Create (AppDomain.CurrentDomain, AssemblyBuilderAccess.Save)) return false; module.CreateContainer (); loader.LoadModules (assembly, module.GlobalRootNamespace); #endif module.InitializePredefinedTypes (); tr.Start (TimeReporter.TimerType.ModuleDefinitionTotal); module.Define (); tr.Stop (TimeReporter.TimerType.ModuleDefinitionTotal); if (Report.Errors > 0) return false; if (settings.DocumentationFile != null) { var doc = new DocumentationBuilder (module); doc.OutputDocComment (output_file, settings.DocumentationFile); } assembly.Resolve (); if (Report.Errors > 0) return false; tr.Start (TimeReporter.TimerType.EmitTotal); assembly.Emit (); tr.Stop (TimeReporter.TimerType.EmitTotal); if (Report.Errors > 0){ return false; } tr.Start (TimeReporter.TimerType.CloseTypes); module.CloseContainer (); tr.Stop (TimeReporter.TimerType.CloseTypes); tr.Start (TimeReporter.TimerType.Resouces); if (!settings.WriteMetadataOnly) assembly.EmbedResources (); tr.Stop (TimeReporter.TimerType.Resouces); if (Report.Errors > 0) return false; assembly.Save (); #if STATIC references_loader.Dispose (); #endif tr.StopTotal (); tr.ShowStats (); return Report.Errors == 0; }
public void Parse (SourceFile file, ModuleContainer module, ParserSession session, Report report) { Stream input; try { input = File.OpenRead (file.Name); } catch { report.Error (2001, "Source file `{0}' could not be found", file.Name); return; } // Check 'MZ' header if (input.ReadByte () == 77 && input.ReadByte () == 90) { report.Error (2015, "Source file `{0}' is a binary file and not a text file", file.Name); input.Close (); return; } input.Position = 0; SeekableStreamReader reader = new SeekableStreamReader (input, ctx.Settings.Encoding, session.StreamReaderBuffer); Parse (reader, file, module, session, report); if (ctx.Settings.GenerateDebugInfo && report.Errors == 0 && !file.HasChecksum) { input.Position = 0; var checksum = session.GetChecksumAlgorithm (); file.SetChecksum (checksum.ComputeHash (input)); } reader.Dispose (); input.Close (); }
public ReflectionImporter(ModuleContainer module, BuiltinTypes builtin) { throw new NotSupportedException(); }
public virtual void Visit (ModuleContainer mc) { foreach (var container in mc.Containers) { container.Accept (this); } }
public StaticDataContainer(ModuleContainer module) : base(module, new MemberName("<PrivateImplementationDetails>" + module.builder.ModuleVersionId.ToString("B"), Location.Null), Modifiers.STATIC | Modifiers.INTERNAL) { size_types = new Dictionary <int, Struct> (); }
public PatternMatchingHelper(ModuleContainer module) : base(module, new MemberName("<PatternMatchingHelper>", Location.Null), Modifiers.STATIC | Modifiers.INTERNAL | Modifiers.DEBUGGER_HIDDEN) { }
private AnonymousTypeClass (ModuleContainer parent, MemberName name, IList<AnonymousTypeParameter> parameters, Location loc) : base (parent, name, parent.Evaluator != null ? Modifiers.PUBLIC : Modifiers.INTERNAL) { this.parameters = parameters; }
public override void Visit(ModuleContainer mc) { bool first = true; foreach (var container in mc.Containers) { var nspace = container as NamespaceContainer; if (nspace == null) { container.Accept(this); continue; } NamespaceDeclaration nDecl = null; var loc = LocationsBag.GetLocations(nspace); if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) { nDecl = new NamespaceDeclaration(); if (loc != null) { nDecl.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.NamespaceKeyword), Roles.NamespaceKeyword); } nDecl.AddChild(ConvertNamespaceName(nspace.RealMemberName), NamespaceDeclaration.NamespaceNameRole); if (loc != null && loc.Count > 1) { nDecl.AddChild(new CSharpTokenNode(Convert(loc [1]), Roles.LBrace), Roles.LBrace); } AddToNamespace(nDecl); namespaceStack.Push(nDecl); } if (nspace.Usings != null) { foreach (var us in nspace.Usings) { us.Accept(this); } } if (first) { first = false; if (mc.OptAttributes != null) { foreach (var attr in mc.OptAttributes.Sections) { var section = ConvertAttributeSection(attr); if (section != null) unit.AddChild(section, SyntaxTree.MemberRole); } } } if (nspace.Containers != null) { foreach (var subContainer in nspace.Containers) { subContainer.Accept(this); } } if (nDecl != null) { AddAttributeSection(nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole); if (loc != null && loc.Count > 2) nDecl.AddChild(new CSharpTokenNode(Convert(loc [2]), Roles.RBrace), Roles.RBrace); if (loc != null && loc.Count > 3) nDecl.AddChild(new CSharpTokenNode(Convert(loc [3]), Roles.Semicolon), Roles.Semicolon); namespaceStack.Pop(); } else { AddAttributeSection(unit, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole); } } AddAttributeSection(unit, mc.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole); }
// // Assembly container with file output // public AssemblyDefinitionDynamic(ModuleContainer module, string name, string fileName) : base(module, name, fileName) { }
public void AddType (ModuleContainer module, TypeSpec ts) { if (types == null) { types = new Dictionary<string, IList<TypeSpec>> (64); } if (ts.IsClass && ts.Arity == 0) { var extension_method_allowed = ts.MemberDefinition.IsImported ? (ts.Modifiers & Modifiers.METHOD_EXTENSION) != 0 : (ts.IsStatic || ts.MemberDefinition.IsPartial); if (extension_method_allowed) { if (extension_method_types == null) extension_method_types = new List<TypeSpec> (); extension_method_types.Add (ts); } } var name = ts.Name; IList<TypeSpec> existing; if (types.TryGetValue (name, out existing)) { TypeSpec better_type; TypeSpec found; if (existing.Count == 1) { found = existing[0]; if (ts.Arity == found.Arity) { better_type = IsImportedTypeOverride (module, ts, found); if (better_type == found) return; if (better_type != null) { existing [0] = better_type; return; } } existing = new List<TypeSpec> (); existing.Add (found); types[name] = existing; } else { for (int i = 0; i < existing.Count; ++i) { found = existing[i]; if (ts.Arity != found.Arity) continue; better_type = IsImportedTypeOverride (module, ts, found); if (better_type == found) return; if (better_type != null) { existing.RemoveAt (i); --i; continue; } } } existing.Add (ts); } else { types.Add (name, new TypeSpec[] { ts }); } }
public FixedFieldSpec (ModuleContainer module, TypeSpec declaringType, IMemberDefinition definition, FieldInfo info, FieldSpec element, Modifiers modifiers) : base (declaringType, definition, PointerContainer.MakeType (module, element.MemberType), info, modifiers) { this.element = element; // It's never CLS-Compliant state &= ~StateFlags.CLSCompliant_Undetected; }
// // We import any types but in the situation there are same types // but one has better visibility (either public or internal with friend) // the less visible type is removed from the namespace cache // public static TypeSpec IsImportedTypeOverride (ModuleContainer module, TypeSpec ts, TypeSpec found) { var ts_accessible = (ts.Modifiers & Modifiers.PUBLIC) != 0 || ts.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly); var found_accessible = (found.Modifiers & Modifiers.PUBLIC) != 0 || found.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly); if (ts_accessible && !found_accessible) return ts; // found is better always better for accessible or inaccessible ts if (!ts_accessible) return found; return null; }
public static CSharpParser Parse (SeekableStreamReader reader, SourceFile sourceFile, ModuleContainer module, ParserSession session, Report report, int lineModifier = 0, int colModifier = 0) { var file = new CompilationSourceFile (module, sourceFile); module.AddTypeContainer(file); CSharpParser parser = new CSharpParser (reader, file, report, session); parser.Lexer.Line += lineModifier; parser.Lexer.Column += colModifier; parser.Lexer.sbag = new SpecialsBag (); parser.parse (); return parser; }
public CompilationSourceFile (ModuleContainer parent, SourceFile sourceFile) : this (parent) { this.file = sourceFile; }
public CompilationSourceFile (ModuleContainer parent) : base (parent) { }
protected NamespaceContainer (ModuleContainer parent) : base (parent, null, null, MemberKind.Namespace) { ns = parent.GlobalRootNamespace; containers = new List<TypeContainer> (2); }
void ParseParallel (ModuleContainer module) { var sources = module.Compiler.SourceFiles; Location.Initialize (sources); var pcount = Environment.ProcessorCount; var threads = new Thread[System.Math.Max (2, pcount - 1)]; for (int i = 0; i < threads.Length; ++i) { var t = new Thread (l => { var session = new ParserSession () { //UseJayGlobalArrays = true, }; var report = new Report (ctx, Report.Printer); // TODO: Implement flush at once printer for (int ii = (int) l; ii < sources.Count; ii += threads.Length) { Parse (sources[ii], module, session, report); } // TODO: Merge warning regions }); t.Start (i); threads[i] = t; } for (int t = 0; t < threads.Length; ++t) { threads[t].Join (); } }
SyntaxTree Parse(ITextSource program, string fileName, int initialLine, int initialColumn) { lock (parseLock) { errorReportPrinter = new ErrorReportPrinter(""); var ctx = new CompilerContext(compilerSettings.ToMono(), errorReportPrinter); ctx.Settings.TabSize = 1; var reader = new SeekableStreamReader(program); var file = new SourceFile(fileName, fileName, 0); Location.Initialize(new List<SourceFile>(new [] { file })); var module = new ModuleContainer(ctx); var session = new ParserSession(); session.LocationsBag = new LocationsBag(); var report = new Report(ctx, errorReportPrinter); var parser = Driver.Parse(reader, file, module, session, report, initialLine - 1, initialColumn - 1); var top = new CompilerCompilationUnit { ModuleCompiled = module, LocationsBag = session.LocationsBag, SpecialsBag = parser.Lexer.sbag, Conditionals = parser.Lexer.SourceFile.Conditionals }; var unit = Parse(top, fileName); unit.Errors.AddRange(errorReportPrinter.Errors); CompilerCallableEntryPoint.Reset(); return unit; } }
public static CSharpParser Parse(SeekableStreamReader reader, SourceFile sourceFile, ModuleContainer module, ParserSession session, Report report, int lineModifier = 0, int colModifier = 0) { var file = new CompilationSourceFile(module, sourceFile); module.AddTypeContainer(file); CSharpParser parser = new CSharpParser(reader, file, report, session); parser.Lexer.Line += lineModifier; parser.Lexer.Column += colModifier; parser.Lexer.sbag = new SpecialsBag(); parser.parse(); return(parser); }
/* /// <summary> /// Parses a file snippet; guessing what the code snippet represents (whole file, type members, block, type reference, expression). /// </summary> public AstNode ParseSnippet (string code) { // TODO: add support for parsing a part of a file throw new NotImplementedException (); } */ public DocumentationReference ParseDocumentationReference(string cref) { // see ICSharpCode.NRefactory.MonoCSharp.DocumentationBuilder.HandleXrefCommon if (cref == null) throw new ArgumentNullException("cref"); // Additional symbols for < and > are allowed for easier XML typing cref = cref.Replace('{', '<').Replace('}', '>'); lock (parseLock) { errorReportPrinter = new ErrorReportPrinter(""); var ctx = new CompilerContext(compilerSettings.ToMono(), errorReportPrinter); ctx.Settings.TabSize = 1; var reader = new SeekableStreamReader(new StringTextSource(cref)); var file = new SourceFile("", "", 0); Location.Initialize(new List<SourceFile>(new [] { file })); var module = new ModuleContainer(ctx); module.DocumentationBuilder = new DocumentationBuilder(module); var source_file = new CompilationSourceFile(module); var report = new Report(ctx, errorReportPrinter); var session = new ParserSession(); session.LocationsBag = new LocationsBag(); var parser = new ICSharpCode.NRefactory.MonoCSharp.CSharpParser(reader, source_file, report, session); parser.Lexer.Line += initialLocation.Line - 1; parser.Lexer.Column += initialLocation.Column - 1; parser.Lexer.putback_char = Tokenizer.DocumentationXref; parser.Lexer.parsing_generic_declaration_doc = true; parser.parse(); if (report.Errors > 0) { // Report.Warning (1584, 1, mc.Location, "XML comment on `{0}' has syntactically incorrect cref attribute `{1}'", // mc.GetSignatureForError (), cref); } var conversionVisitor = new ConversionVisitor(false, session.LocationsBag); var docRef = conversionVisitor.ConvertXmlDoc(module.DocumentationBuilder); CompilerCallableEntryPoint.Reset(); return docRef; } }
public CSharpParser (SeekableStreamReader reader, CompilationSourceFile file, Report report, ParserSession session) { this.file = file; current_container = current_namespace = file; this.module = file.Module; this.compiler = file.Compiler; this.settings = compiler.Settings; this.report = report; lang_version = settings.Version; yacc_verbose_flag = settings.VerboseParserFlag; doc_support = settings.DocumentationFile != null; lexer = new Tokenizer (reader, file, session, report); oob_stack = new Stack<object> (); lbag = session.LocationsBag; use_global_stacks = session.UseJayGlobalArrays; parameters_bucket = session.ParametersStack; }
public PatternMatchingHelper (ModuleContainer module) : base (module, new MemberName ("<PatternMatchingHelper>", Location.Null), Modifiers.STATIC | Modifiers.INTERNAL | Modifiers.DEBUGGER_HIDDEN) { }