public override void TransferStateTo(Visitor targetVisitor) { base.TransferStateTo(targetVisitor); Scoper target = targetVisitor as Scoper; if (target == null) { return; } target.ScopeFor = this.ScopeFor; target.currentScope = this.currentScope; target.currentModule = this.currentModule; }
public override void CompileParseTree(Compilation compilation, ErrorNodeList errorNodes) { if (compilation == null) { Debug.Assert(false, "wrong compilation"); return; } CONTEXT.useComputeMath = ((ZonnonCompilerParameters)compilation.CompilerParameters).UseComputeMath; // Before the actual back-end compilation takes place, // we perform the Zonnon-tree to CCI-tree conversion. // Notice that we do that for the overall tree (i.e., for all sources) // and store the resulting CCI tree to CompilationUnits[0], // but do not spread subtrees to corresponding CompilationUnits. ERROR.errCount = 0; Parser.ConvertTree(compilation.CompilationUnits[0]); bool mainCompilationFailed = ERROR.errCount > 0; // ERROR.EndOfMessages(); // Module symbolTable = compilation.TargetModule; // compilation.GlobalScope = this.GetGlobalScope(symbolTable); ZonnonCompilerParameters options = compilation.CompilerParameters as ZonnonCompilerParameters; if (options == null) { options = new ZonnonCompilerParameters(); } // Walk IR looking up names #if DEBUG if (options.Debug) { System.Console.Write("Scoper is working..."); } #endif ErrorHandler eh = new ErrorHandler(errorNodes); TrivialHashtable ambiguousTypes = new TrivialHashtable(); TrivialHashtable referencedLabels = new TrivialHashtable(); TrivialHashtable scopeFor = new TrivialHashtable(); TypeSystem typeSystem = new TypeSystem(eh); Scoper scoper = new Scoper(scopeFor); scoper.VisitCompilation(compilation); #if DEBUG if (options.Debug) { System.Console.WriteLine("Done."); System.Console.Write("Looker is working... "); } #endif Looker looker = new Looker(compilation.GlobalScope, eh, scopeFor, typeSystem, ambiguousTypes, referencedLabels); looker.VisitCompilation(compilation); // Walk IR inferring types and resolving overloads if (!mainCompilationFailed) // Resolve only if no errors { Resolver resolver = new Resolver(eh, typeSystem); resolver.VisitCompilation(compilation); // Walk IR checking for semantic errors and repairing it so that it the next walk will work Checker checker = new Checker(eh, typeSystem, ambiguousTypes, referencedLabels); checker.VisitCompilation(compilation); // Walk IR reducing it to nodes that have predefined mappings to MD+IL Normalizer normalizer = new Normalizer(typeSystem); normalizer.VisitCompilation(compilation); Analyzer analyzer = new Analyzer(typeSystem, compilation); analyzer.VisitCompilation(compilation); } #if DEBUG if (options.Debug) { System.Console.WriteLine("Done."); } #endif }
public virtual void ConstructSymbolTable(Compilation compilation, ErrorNodeList errors, TrivialHashtable scopeFor){ if (compilation == null || scopeFor == null){Debug.Assert(false); return;} this.CurrentCompilation = compilation; Module symbolTable = compilation.TargetModule = this.CreateModule(compilation.CompilerParameters, errors, compilation); Scoper scoper = new Scoper(scopeFor); scoper.currentModule = symbolTable; ErrorHandler errorHandler = new ErrorHandler(errors); Looker looker = new Looker(this.GetGlobalScope(symbolTable), errorHandler, scopeFor); // begin change by drunje SpecSharpCompilerOptions options = compilation.CompilerParameters as SpecSharpCompilerOptions; if (options != null) looker.AllowPointersToManagedStructures = options.AllowPointersToManagedStructures; // end change by drunje looker.currentAssembly = (looker.currentModule = symbolTable) as AssemblyNode; looker.ignoreMethodBodies = true; Scope globalScope = compilation.GlobalScope = this.GetGlobalScope(symbolTable); CompilationUnitList sources = compilation.CompilationUnits; if (sources == null) return; int n = sources.Count; for (int i = 0; i < n; i++){ CompilationUnitSnippet compilationUnitSnippet = sources[i] as CompilationUnitSnippet; if (compilationUnitSnippet == null){Debug.Assert(false); continue;} compilationUnitSnippet.ChangedMethod = null; Document doc = compilationUnitSnippet.SourceContext.Document; if (doc == null || doc.Text == null){Debug.Assert(false); continue;} IParserFactory factory = compilationUnitSnippet.ParserFactory; if (factory == null){Debug.Assert(false); return;} IParser p = factory.CreateParser(doc.Name, doc.LineNumber, doc.Text, symbolTable, errors, compilation.CompilerParameters); if (p is ResgenCompilerStub) continue; if (p == null){Debug.Assert(false); continue;} Parser specSharpParser = p as Parser; if (specSharpParser == null) p.ParseCompilationUnit(compilationUnitSnippet); else specSharpParser.ParseCompilationUnit(compilationUnitSnippet, true, false); //TODO: this following is a good idea only if the files will not be frequently reparsed from source //StringSourceText stringSourceText = doc.Text.TextProvider as StringSourceText; //if (stringSourceText != null && stringSourceText.IsSameAsFileContents) // doc.Text.TextProvider = new CollectibleSourceText(doc.Name, doc.Text.Length); //else if (doc.Text.TextProvider != null) // doc.Text.TextProvider.MakeCollectible(); } CompilationUnitList compilationUnits = new CompilationUnitList(); for (int i = 0; i < n; i++){ CompilationUnit cUnit = sources[i]; compilationUnits.Add(scoper.VisitCompilationUnit(cUnit)); } for (int i = 0; i < n; i++){ CompilationUnit cUnit = compilationUnits[i]; if (cUnit == null) continue; looker.VisitCompilationUnit(cUnit); } //Run resolver over symbol table so that custom attributes on member signatures are known and can be used //to error check the the given file. TypeSystem typeSystem = new TypeSystem(errorHandler); Resolver resolver = new Resolver(errorHandler, typeSystem); resolver.currentAssembly = (resolver.currentModule = symbolTable) as AssemblyNode; for (int i = 0; i < n; i++) { CompilationUnit cUnit = compilationUnits[i]; if (cUnit == null) continue; resolver.VisitCompilationUnit(cUnit); } this.CurrentCompilation = null; }
public virtual void ResolveSymbolTable(Compilation/*!*/ parsedCompilation, ErrorNodeList/*!*/ errors, out TrivialHashtable scopeFor){ scopeFor = new TrivialHashtable(); if (parsedCompilation == null) { Debug.Assert(false); return; } if (errors == null){Debug.Assert(false); return;} Scoper scoper = new Scoper(scopeFor); scoper.currentModule = parsedCompilation.TargetModule; scoper.VisitCompilation(parsedCompilation); ErrorHandler errorHandler = new ErrorHandler(errors); TrivialHashtable ambiguousTypes = new TrivialHashtable(); TrivialHashtable referencedLabels = new TrivialHashtable(); Looker looker = new Looker(null, errorHandler, scopeFor, ambiguousTypes, referencedLabels); // begin change by drunje SpecSharpCompilerOptions options = parsedCompilation.CompilerParameters as SpecSharpCompilerOptions; if (options != null) looker.AllowPointersToManagedStructures = options.AllowPointersToManagedStructures; // end change by drunje looker.currentAssembly = (looker.currentModule = parsedCompilation.TargetModule) as AssemblyNode; looker.VisitCompilation(parsedCompilation); }
public override void CompileParseTree(Compilation compilation, ErrorNodeList errorNodes){ if (compilation == null || compilation.CompilationUnits == null || compilation.TargetModule == null){Debug.Assert(false); return;} if (compilation.CompilationUnits.Count == 0) return; TrivialHashtable ambiguousTypes = new TrivialHashtable(); TrivialHashtable referencedLabels = new TrivialHashtable(); TrivialHashtable scopeFor = new TrivialHashtable(64); ErrorHandler errorHandler = new ErrorHandler(errorNodes); SpecSharpCompilation ssCompilation = new SpecSharpCompilation(); SpecSharpCompilerOptions options = (SpecSharpCompilerOptions)compilation.CompilerParameters; //Attach scopes to namespaces and types so that forward references to base types can be looked up in the appropriate namespace scope Scoper scoper = new Scoper(scopeFor); scoper.VisitCompilation(compilation); scoper = null; if (options.NoStandardLibrary && compilation.TargetModule is AssemblyNode) { if (compilation.TargetModule.IsValidTypeName(StandardIds.System, StandardIds.CapitalObject)) { SystemAssemblyLocation.ParsedAssembly = (AssemblyNode)compilation.TargetModule; SystemCompilerRuntimeAssemblyLocation.ParsedAssembly = (AssemblyNode)compilation.TargetModule; //So that mscorlib can have contracts but no reference to another assembly } else if (compilation.TargetModule.IsValidTypeName(Identifier.For("System.Compiler"), Identifier.For("ComposerAttribute"))) SystemCompilerRuntimeAssemblyLocation.ParsedAssembly = (AssemblyNode)compilation.TargetModule; else if (compilation.TargetModule.IsValidTypeName(Identifier.For("Microsoft.SpecSharp"), Identifier.For("dummy"))) RuntimeAssemblyLocation.ParsedAssembly = (AssemblyNode)compilation.TargetModule; } object ObjectType = SystemTypes.Object; if (ObjectType == null) return; //system types did not initialize //Walk IR looking up names Looker looker = new Looker(compilation.GlobalScope, errorHandler, scopeFor, ambiguousTypes, referencedLabels); if (options != null && options.EmitSourceContextsOnly) { looker.DontInjectDefaultConstructors = true; } // begin change by drunje looker.AllowPointersToManagedStructures = options.AllowPointersToManagedStructures; // end change by drunje looker.VisitCompilation(compilation); looker = null; if (options != null && options.EmitSourceContextsOnly) return; // stop after looker to have resolved types //Walk IR inferring types and resolving overloads TypeSystem typeSystem = new TypeSystem(errorHandler); Resolver resolver = new Resolver(errorHandler, typeSystem); resolver.VisitCompilation(compilation); resolver = null; //Walk IR checking for semantic errors and repairing it so that the next walk will work Checker checker = new Checker(ssCompilation, errorHandler, typeSystem, scopeFor, ambiguousTypes, referencedLabels); checker.VisitCompilation(compilation); checker = null; scopeFor = null; ambiguousTypes = null; referencedLabels = null; if (!options.IsContractAssembly) { if (options.RunProgramVerifier) ssCompilation.AddProgramVerifierPlugin(typeSystem, compilation); //Allow third party extensions to analyze AST IR for further errors ssCompilation.RunPlugins(compilation, errorHandler); } //Walk IR reducing it to nodes that have predefined mappings to MD+IL Normalizer normalizer = new Normalizer(typeSystem); normalizer.VisitCompilation(compilation); normalizer = null; if (options.IsContractAssembly) return; //Walk normalized IR instrumenting accesses of fields of guarded classes with checks CompilationUnit cu = compilation.CompilationUnits[0]; if (cu != null && cu.PreprocessorDefinedSymbols != null && cu.PreprocessorDefinedSymbols.ContainsKey("GuardedFieldAccessChecks")){ if (errorNodes.Count == 0){ GuardedFieldAccessInstrumenter instrumenter = new GuardedFieldAccessInstrumenter(); instrumenter.VisitCompilation(compilation); instrumenter = null; } } //Walk normalized IR doing code analysis Analyzer analyzer = new Analyzer(typeSystem, compilation); analyzer.Visit(compilation); //Allow third party extensions to analyze normalized IR for further errors ssCompilation.analyzer = analyzer; // make the analyzer available to plugins for access to method CFGs ssCompilation.RunPlugins(compilation, errorHandler); ssCompilation.analyzer = null; ssCompilation = null; analyzer = null; errorHandler = null; //Walk IR to optimize code further after analyses were performed, eg. to remove debug only code Optimizer optimizer = new Optimizer(); optimizer.Visit(compilation); optimizer = null; }
public override Node CompileParseTree(Node node, Scope scope, Module targetModule, ErrorNodeList errorNodes){ TrivialHashtable ambiguousTypes = new TrivialHashtable(); TrivialHashtable referencedLabels = new TrivialHashtable(); TrivialHashtable scopeFor = new TrivialHashtable(); ErrorHandler errorHandler = new ErrorHandler(errorNodes); SpecSharpCompilation ssCompilation = new SpecSharpCompilation(); // Setting the state TypeNode thisType = null; Method currentMethod = null; BlockScope blockScope = scope as BlockScope; if (blockScope != null){ Class baseScope = blockScope; MethodScope methodScope = null; while (baseScope != null){ methodScope = baseScope.BaseClass as MethodScope; if (methodScope != null) break; baseScope = baseScope.BaseClass; } if (methodScope != null){ thisType = methodScope.ThisType; if (thisType == null && methodScope.BaseClass is TypeScope){ thisType = ((TypeScope) methodScope.BaseClass).Type; } currentMethod = methodScope.DeclaringMethod; } } //Attach scope to namespaces and types scopeFor[node.UniqueKey] = scope; Scoper scoper = new Scoper(scopeFor); scoper.currentScope = scope; node = scoper.Visit(node); //Walk IR looking up names Looker looker = new Looker(scope, errorHandler, scopeFor, ambiguousTypes, referencedLabels); // begin change by drunje (this is called from debugger only) looker.AllowPointersToManagedStructures = true; // end change by drunje if (blockScope != null) { looker.currentType = thisType; looker.currentMethod = currentMethod; } looker.currentAssembly = targetModule as AssemblyNode; looker.currentModule = targetModule; node = looker.Visit(node); //Walk IR inferring types and resolving overloads TypeSystem typeSystem = new TypeSystem(errorHandler); Resolver resolver = new Resolver(errorHandler, typeSystem); if (blockScope != null){ resolver.currentType = thisType; resolver.currentMethod = currentMethod; } resolver.currentAssembly = targetModule as AssemblyNode; resolver.currentModule = targetModule; node = resolver.Visit(node); //TODO: Need to set the state of the checker for compiling Expression, STOP using this method when the shift is complete //Walk IR checking for semantic errors and repairing it so that the next walk will work Checker checker = new Checker(ssCompilation, errorHandler, typeSystem, scopeFor, ambiguousTypes, referencedLabels); if (blockScope != null){ checker.currentType = thisType; checker.currentMethod = currentMethod; } checker.currentAssembly = targetModule as AssemblyNode; checker.currentModule = targetModule; node = checker.Visit(node); //Walk IR reducing it to nodes that have predefined mappings to MD+IL Normalizer normalizer = new Normalizer(typeSystem); if (blockScope != null){ normalizer.currentType = thisType; normalizer.currentMethod = currentMethod; normalizer.WrapToBlockExpression = false; } normalizer.currentModule = targetModule; node = normalizer.Visit(node); return node; }
public override void Resolve(CompilationUnit partialCompilationUnit){ if (partialCompilationUnit == null){Debug.Assert(false); return;} TrivialHashtable scopeFor = new TrivialHashtable(); Scoper scoper = new Scoper(scopeFor); scoper.currentModule = this.currentSymbolTable; scoper.VisitCompilationUnit(partialCompilationUnit); ErrorHandler errorHandler = new ErrorHandler(new ErrorNodeList(0)); TrivialHashtable ambiguousTypes = new TrivialHashtable(); TrivialHashtable referencedLabels = new TrivialHashtable(); Looker looker = new Looker(null, errorHandler, scopeFor, ambiguousTypes, referencedLabels); looker.currentAssembly = (looker.currentModule = this.currentSymbolTable) as AssemblyNode; looker.identifierInfos = this.identifierInfos = new NodeList(); looker.identifierPositions = this.identifierPositions = new Int32List(); looker.identifierLengths = this.identifierLengths = new Int32List(); looker.identifierContexts = this.identifierContexts = new Int32List(); looker.identifierScopes = this.identifierScopes = new ScopeList(); looker.allScopes = this.allScopes = new ScopeList(); looker.VisitCompilationUnit(partialCompilationUnit); //Walk IR inferring types and resolving overloads Resolver resolver = new Resolver(errorHandler, new TypeSystem(errorHandler)); resolver.currentAssembly = (resolver.currentModule = this.currentSymbolTable) as AssemblyNode; resolver.Visit(partialCompilationUnit); }
public override void ParseAndAnalyzeCompilationUnit(string fname, string text, int line, int col, ErrorNodeList errors, Compilation compilation, AuthoringSink sink) { if (fname == null || text == null || errors == null || compilation == null){Debug.Assert(false); return;} if (compilation != null && compilation.CompilerParameters is SpecSharpCompilerOptions) this.allowSpecSharpExtensions = !((SpecSharpCompilerOptions)compilation.CompilerParameters).Compatibility; CompilationUnitList compilationUnitSnippets = compilation.CompilationUnits; if (compilationUnitSnippets == null){Debug.Assert(false); return;} //Fix up the CompilationUnitSnippet corresponding to fname with the new source text CompilationUnitSnippet cuSnippet = this.GetCompilationUnitSnippet(compilation, fname); if (cuSnippet == null) return; Compiler compiler = new Compiler(); compiler.CurrentCompilation = compilation; cuSnippet.SourceContext.Document = compiler.CreateDocument(fname, 1, new DocumentText(text)); cuSnippet.SourceContext.EndPos = text.Length; //Parse all of the compilation unit snippets Module symbolTable = compilation.TargetModule = compiler.CreateModule(compilation.CompilerParameters, errors); AttributeList assemblyAttributes = symbolTable is AssemblyNode ? symbolTable.Attributes : null; AttributeList moduleAttributes = symbolTable is AssemblyNode ? ((AssemblyNode)symbolTable).ModuleAttributes : symbolTable.Attributes; int n = compilationUnitSnippets.Count; for (int i = 0; i < n; i++){ CompilationUnitSnippet compilationUnitSnippet = compilationUnitSnippets[i] as CompilationUnitSnippet; if (compilationUnitSnippet == null){Debug.Assert(false); continue;} Document doc = compilationUnitSnippet.SourceContext.Document; doc = compilationUnitSnippet.SourceContext.Document; if (doc == null || doc.Text == null){Debug.Assert(false); continue;} IParserFactory factory = compilationUnitSnippet.ParserFactory; if (factory == null) continue; compilationUnitSnippet.Nodes = null; compilationUnitSnippet.PreprocessorDefinedSymbols = null; IParser p = factory.CreateParser(doc.Name, doc.LineNumber, doc.Text, symbolTable, compilationUnitSnippet == cuSnippet ? errors : new ErrorNodeList(), compilation.CompilerParameters); if (p == null){Debug.Assert(false); continue;} if (p is ResgenCompilerStub) continue; Parser specSharpParser = p as Parser; if (specSharpParser == null) p.ParseCompilationUnit(compilationUnitSnippet); else specSharpParser.ParseCompilationUnit(compilationUnitSnippet, compilationUnitSnippet != cuSnippet, false); //TODO: this following is a good idea only if the files will not be frequently reparsed from source //StringSourceText stringSourceText = doc.Text.TextProvider as StringSourceText; //if (stringSourceText != null && stringSourceText.IsSameAsFileContents) // doc.Text.TextProvider = new CollectibleSourceText(doc.Name, doc.Text.Length); } //Construct symbol table for entire project ErrorHandler errorHandler = new ErrorHandler(errors); SpecSharpCompilation ssCompilation = new SpecSharpCompilation(); TrivialHashtable ambiguousTypes = new TrivialHashtable(); TrivialHashtable referencedLabels = new TrivialHashtable(); TrivialHashtable scopeFor = this.scopeFor = new TrivialHashtable(); Scoper scoper = new Scoper(scopeFor); scoper.currentModule = symbolTable; Looker symLooker = new Looker(null, new ErrorHandler(new ErrorNodeList(0)), scopeFor, ambiguousTypes, referencedLabels); symLooker.currentAssembly = (symLooker.currentModule = symbolTable) as AssemblyNode; Looker looker = new Looker(null, errorHandler, scopeFor, ambiguousTypes, referencedLabels); looker.currentAssembly = (looker.currentModule = symbolTable) as AssemblyNode; looker.VisitAttributeList(assemblyAttributes); bool dummyCompilation = compilation.CompilerParameters is SpecSharpCompilerOptions && ((SpecSharpCompilerOptions)compilation.CompilerParameters).DummyCompilation; if (dummyCompilation){ //This happens when there is no project. In this case, semantic errors should be ignored since the references and options are unknown. //But proceed with the full analysis anyway so that some measure of Intellisense can still be provided. errorHandler.Errors = new ErrorNodeList(0); } for (int i = 0; i < n; i++){ CompilationUnit cUnit = compilationUnitSnippets[i]; scoper.VisitCompilationUnit(cUnit); } for (int i = 0; i < n; i++){ CompilationUnit cUnit = compilationUnitSnippets[i]; if (cUnit == cuSnippet) looker.VisitCompilationUnit(cUnit); //Uses real error message list and populate the identifier info lists else symLooker.VisitCompilationUnit(cUnit); //Errors are discarded } //Run resolver over symbol table so that custom attributes on member signatures are known and can be used //to error check the the given file. TypeSystem typeSystem = new TypeSystem(errorHandler); Resolver resolver = new Resolver(errorHandler, typeSystem); resolver.currentAssembly = (resolver.currentModule = symbolTable) as AssemblyNode; Resolver symResolver = new Resolver(new ErrorHandler(new ErrorNodeList(0)), typeSystem); symResolver.currentAssembly = resolver.currentAssembly; symResolver.VisitAttributeList(assemblyAttributes); for (int i = 0; i < n; i++) { CompilationUnit cUnit = compilationUnitSnippets[i]; if (cUnit == cuSnippet) resolver.VisitCompilationUnit(cUnit); //Uses real error message list and populate the identifier info lists else symResolver.VisitCompilationUnit(cUnit); //Errors are discarded } if (dummyCompilation) return; //Now analyze the given file for errors Checker checker = new Checker(ssCompilation, errorHandler, typeSystem, scopeFor, ambiguousTypes, referencedLabels); checker.currentAssembly = (checker.currentModule = symbolTable) as AssemblyNode; checker.VisitAttributeList(assemblyAttributes, checker.currentAssembly); checker.VisitModuleAttributes(moduleAttributes); checker.VisitCompilationUnit(cuSnippet); MemberFinder finder = new MemberFinder(line, col); finder.VisitCompilationUnit(cuSnippet); Node node = finder.Member; if (node == null){ if (line == 0 && col == 0) node = cuSnippet; else return; } SpecSharpCompilerOptions options = (SpecSharpCompilerOptions) compilation.CompilerParameters; if (options.IsContractAssembly) return; ssCompilation.RunPlugins(node, errorHandler); Normalizer normalizer = new Normalizer(typeSystem); normalizer.Visit(node); Analyzer analyzer = new Analyzer(typeSystem, compilation); analyzer.Visit(node); if (options.RunProgramVerifierWhileEditing) ssCompilation.AddProgramVerifierPlugin(typeSystem, compilation); ssCompilation.analyzer = analyzer; // make the analyzer available to plugins for access to method CFGs ssCompilation.RunPlugins(node, errorHandler); ssCompilation.analyzer = null; analyzer = null; }
public void ResolveIR(Compilation compilation, ErrorNodeList errorNodes) { TrivialHashtable ambiguousTypes = new TrivialHashtable(); TrivialHashtable scopeFor = new TrivialHashtable(); TrivialHashtable referencedLabels = new TrivialHashtable(); Hashtable exceptionNames = new Hashtable(); ErrorHandler errorHandler = new ErrorHandler(errorNodes); string target = ""; ZingCompilerOptions zoptions = compilation.CompilerParameters as ZingCompilerOptions; if (zoptions != null && zoptions.DumpSource) { target = compilation.CompilerParameters.OutputAssembly; if (string.IsNullOrEmpty(target)) { target = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar; } string output = Path.GetDirectoryName(target); if (string.IsNullOrEmpty(output)) output = Directory.GetCurrentDirectory(); target = Path.GetFileNameWithoutExtension(target); } if (this.Options == null) this.Options = compilation.CompilerParameters; //Attach scopes to namespaces and types so that forward references to base types can be looked up in the appropriate namespace scope Scoper scoper = new Scoper(scopeFor); scoper.VisitCompilation(compilation); //Walk IR looking up names TypeSystem typeSystem = new TypeSystem(errorHandler); Looker looker = new Looker(compilation.GlobalScope, errorHandler, scopeFor, typeSystem, // LJW: added typeSystem ambiguousTypes, referencedLabels, exceptionNames); looker.VisitCompilation(compilation); //Walk IR inferring types and resolving overloads Resolver resolver = new Resolver(errorHandler, typeSystem); resolver.VisitCompilation(compilation); Checker checker = new Checker(errorHandler, typeSystem, scopeFor, ambiguousTypes, referencedLabels); // LJW: added scopeFor checker.VisitCompilation(compilation); }
/// <summary> /// Parses all of the CompilationUnitSnippets in the given compilation, ignoring method bodies. Then resolves all type expressions. /// The resulting types can be retrieved from the module in compilation.TargetModule. The base types, interfaces and /// member signatures will all be resolved and on an equal footing with imported, already compiled modules and assemblies. /// </summary> public override void ConstructSymbolTable(Compilation compilation, ErrorNodeList errors) { if (compilation == null) { Debug.Assert(false); return; } Module symbolTable = compilation.TargetModule = this.CreateModule(compilation.CompilerParameters, errors, compilation); TrivialHashtable scopeFor = new TrivialHashtable(); Scoper scoper = new Scoper(scopeFor); scoper.currentModule = symbolTable; ErrorHandler errorHandler = new ErrorHandler(errors); TypeSystem typeSystem = new TypeSystem(errorHandler); // LJW: added typeSystem Looker looker = new Looker(this.GetGlobalScope(symbolTable), errorHandler, scopeFor, typeSystem); looker.currentAssembly = (looker.currentModule = symbolTable) as AssemblyNode; looker.ignoreMethodBodies = true; compilation.GlobalScope = this.GetGlobalScope(symbolTable); CompilationUnitList sources = compilation.CompilationUnits; if (sources == null) { Debug.Assert(false); return; } int n = sources.Count; for (int i = 0; i < n; i++) { CompilationUnitSnippet compilationUnitSnippet = sources[i] as CompilationUnitSnippet; if (compilationUnitSnippet == null) { Debug.Assert(false); continue; } compilationUnitSnippet.ChangedMethod = null; Document doc = compilationUnitSnippet.SourceContext.Document; if (doc == null || doc.Text == null) { Debug.Assert(false); continue; } IParserFactory factory = compilationUnitSnippet.ParserFactory; if (factory == null) { Debug.Assert(false); return; } IParser p = factory.CreateParser(doc.Name, doc.LineNumber, doc.Text, symbolTable, errors, compilation.CompilerParameters); if (p is ResgenCompilerStub) continue; if (p == null) { Debug.Assert(false); continue; } Parser zingParser = p as Parser; if (zingParser == null) p.ParseCompilationUnit(compilationUnitSnippet); else zingParser.ParseCompilationUnit(compilationUnitSnippet, true, true, null); StringSourceText stringSourceText = doc.Text.TextProvider as StringSourceText; if (stringSourceText != null && stringSourceText.IsSameAsFileContents) doc.Text.TextProvider = new CollectibleSourceText(doc.Name, doc.Text.Length); else if (doc.Text.TextProvider != null) doc.Text.TextProvider.MakeCollectible(); } CompilationUnitList compilationUnits = new CompilationUnitList(); for (int i = 0; i < n; i++) { CompilationUnit cUnit = sources[i]; compilationUnits.Add(scoper.VisitCompilationUnit(cUnit)); } for (int i = 0; i < n; i++) { CompilationUnit cUnit = compilationUnits[i]; if (cUnit == null) continue; looker.VisitCompilationUnit(cUnit); } }
public override void CompileParseTree(Compilation compilation, ErrorNodeList errorNodes) { TrivialHashtable ambiguousTypes = new TrivialHashtable(); TrivialHashtable scopeFor = new TrivialHashtable(); TrivialHashtable referencedLabels = new TrivialHashtable(); Hashtable exceptionNames = new Hashtable(); ErrorHandler errorHandler = new ErrorHandler(errorNodes); string target = ""; ZingCompilerOptions zoptions = compilation.CompilerParameters as ZingCompilerOptions; if (zoptions != null && zoptions.DumpSource) { target = compilation.CompilerParameters.OutputAssembly; if (string.IsNullOrEmpty(target)) { target = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar; } string output = Path.GetDirectoryName(target); if (string.IsNullOrEmpty(output)) output = Directory.GetCurrentDirectory(); target = Path.GetFileNameWithoutExtension(target); } if (this.Options == null) this.Options = compilation.CompilerParameters; //Attach scopes to namespaces and types so that forward references to base types can be looked up in the appropriate namespace scope Scoper scoper = new Scoper(scopeFor); scoper.VisitCompilation(compilation); //Walk IR looking up names TypeSystem typeSystem = new TypeSystem(errorHandler); Looker looker = new Looker(compilation.GlobalScope, errorHandler, scopeFor, typeSystem, // LJW: added typeSystem ambiguousTypes, referencedLabels, exceptionNames); looker.VisitCompilation(compilation); //Walk IR inferring types and resolving overloads Resolver resolver = new Resolver(errorHandler, typeSystem); resolver.VisitCompilation(compilation); Checker checker = new Checker(errorHandler, typeSystem, scopeFor, ambiguousTypes, referencedLabels); // LJW: added scopeFor checker.VisitCompilation(compilation); // Walk the Zing IR splicing it into our code-gen templates Splicer splicer = new Splicer(this.Options, referencedLabels, exceptionNames); CompilationUnit cuMerged = splicer.CodeGen(compilation); // // If the 'dumpSource' option is given, then we decompile the IR to C# and write this // to a file. // if (zoptions != null && zoptions.DumpSource) { // Once the codegen is done and we have the IR for the generated code, we use the // decompiler to get C# back out, and hand this to the standard compiler. Later, // we'll replace this with the X# normalizer and avoid going in and out of source // code (except for compiler debugging). string tempSrc; string tempName = compilation.CompilerParameters.OutputAssembly + ".cs"; Decompiler decompiler = new Decompiler(); tempSrc = decompiler.Decompile(cuMerged); StreamWriter sw = File.CreateText(tempName); sw.Write(tempSrc); sw.Close(); } if (zoptions != null && zoptions.DumpLabels) { string tempName = compilation.CompilerParameters.OutputAssembly + ".labels"; StreamWriter sw = File.CreateText(tempName); sw.Write(splicer.LabelString); sw.Close(); } for (int i = 0; i < RequiredTypes.Length; i++) { R.Assembly asm = R.Assembly.GetAssembly(RequiredTypes[i]); if (!compilation.CompilerParameters.ReferencedAssemblies.Contains(asm.Location)) compilation.CompilerParameters.ReferencedAssemblies.Add(asm.Location); } // The if-statement added by Jiri Adamek // It loads a native ZOM assembly if used if (zoptions != null && zoptions.ZomAssemblyName != null) { compilation.CompilerParameters.ReferencedAssemblies.Add(zoptions.ZomAssemblyName); } CS.Compiler csCompiler = new CS.Compiler(); // We have to create a new module to pull in references from the // assemblies we just added above. compilation.TargetModule = this.targetModule = csCompiler.CreateModule(compilation.CompilerParameters, errorNodes); // Our one top-level type must be added to the module's type list compilation.TargetModule.Types.Add(((Namespace)cuMerged.Nodes[0]).NestedNamespaces[0].Types[0]); // The restorer patches the DeclaringModule field of our types Restorer restorer = new Restorer(compilation.TargetModule); restorer.VisitCompilationUnit(cuMerged); // Replace the Zing compilation unit with our generated code before invoking // the Spec# back end. compilation.CompilationUnits = new CompilationUnitList(cuMerged); foreach (CompilationUnit cunit in compilation.CompilationUnits) cunit.Compilation = compilation; // For retail builds, disable the time-consuming definite-assignment checks // in the Spec# compiler. if (!this.Options.IncludeDebugInformation) compilation.CompilationUnits[0].PreprocessorDefinedSymbols["NODEFASSIGN"] = "true"; // Let the Spec# back end process the IR from here csCompiler.CompileParseTree(compilation, errorNodes); }