Skip to content

Commit 5ecc288

Browse files
Merge pull request microsoft#1512 from Microsoft/layering
Compiler Layering
2 parents af9b56b + ef2087a commit 5ecc288

20 files changed

Lines changed: 642 additions & 615 deletions

Jakefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var compilerSources = [
3939
"binder.ts",
4040
"checker.ts",
4141
"emitter.ts",
42+
"program.ts",
4243
"commandLineParser.ts",
4344
"tsc.ts",
4445
"diagnosticInformationMap.generated.ts"
@@ -56,6 +57,7 @@ var servicesSources = [
5657
"binder.ts",
5758
"checker.ts",
5859
"emitter.ts",
60+
"program.ts",
5961
"diagnosticInformationMap.generated.ts"
6062
].map(function (f) {
6163
return path.join(compilerDirectory, f);
@@ -92,6 +94,7 @@ var definitionsRoots = [
9294
"compiler/scanner.d.ts",
9395
"compiler/parser.d.ts",
9496
"compiler/checker.d.ts",
97+
"compiler/program.d.ts",
9598
"services/services.d.ts",
9699
];
97100

src/compiler/binder.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
/// <reference path="types.ts"/>
2-
/// <reference path="core.ts"/>
3-
/// <reference path="scanner.ts"/>
41
/// <reference path="parser.ts"/>
52

63
module ts {
7-
84
export const enum ModuleInstanceState {
95
NonInstantiated = 0,
106
Instantiated = 1,

src/compiler/checker.ts

Lines changed: 76 additions & 105 deletions
Large diffs are not rendered by default.

src/compiler/emitter.ts

Lines changed: 56 additions & 52 deletions
Large diffs are not rendered by default.

src/compiler/parser.ts

Lines changed: 0 additions & 361 deletions
Large diffs are not rendered by default.

src/compiler/program.ts

Lines changed: 408 additions & 0 deletions
Large diffs are not rendered by default.

src/compiler/scanner.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference path="types.ts"/>
21
/// <reference path="core.ts"/>
32
/// <reference path="diagnosticInformationMap.generated.ts"/>
43

src/compiler/tsc.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
/// <reference path="core.ts"/>
2-
/// <reference path="sys.ts"/>
3-
/// <reference path="types.ts"/>
4-
/// <reference path="scanner.ts"/>
5-
/// <reference path="parser.ts"/>
6-
/// <reference path="binder.ts"/>
7-
/// <reference path="checker.ts"/>
8-
/// <reference path="emitter.ts"/>
1+
/// <reference path="program.ts"/>
92
/// <reference path="commandLineParser.ts"/>
103

114
module ts {
@@ -293,15 +286,15 @@ module ts {
293286
var checker = program.getTypeChecker(/*fullTypeCheckMode*/ true);
294287
var checkStart = new Date().getTime();
295288
errors = checker.getDiagnostics();
296-
if (checker.isEmitBlocked()) {
289+
if (program.isEmitBlocked()) {
297290
exitStatus = EmitReturnStatus.AllOutputGenerationSkipped;
298291
}
299292
else if (compilerOptions.noEmit) {
300293
exitStatus = EmitReturnStatus.Succeeded;
301294
}
302295
else {
303296
var emitStart = new Date().getTime();
304-
var emitOutput = checker.emitFiles();
297+
var emitOutput = program.emitFiles();
305298
var emitErrors = emitOutput.diagnostics;
306299
exitStatus = emitOutput.emitResultStatus;
307300
var reportStart = new Date().getTime();

src/compiler/types.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/// <reference path="core.ts"/>
2-
31
module ts {
42
export interface Map<T> {
53
[index: string]: T;
@@ -248,7 +246,6 @@ module ts {
248246
EnumMember,
249247
// Top-level nodes
250248
SourceFile,
251-
Program,
252249

253250
// Synthesized list
254251
SyntaxList,
@@ -925,15 +922,33 @@ module ts {
925922
identifiers: Map<string>;
926923
}
927924

928-
export interface Program {
925+
export interface ScriptReferenceHost {
926+
getCompilerOptions(): CompilerOptions;
929927
getSourceFile(filename: string): SourceFile;
928+
getCurrentDirectory(): string;
929+
}
930+
931+
export interface Program extends ScriptReferenceHost {
930932
getSourceFiles(): SourceFile[];
931-
getCompilerOptions(): CompilerOptions;
932933
getCompilerHost(): CompilerHost;
934+
933935
getDiagnostics(sourceFile?: SourceFile): Diagnostic[];
934936
getGlobalDiagnostics(): Diagnostic[];
935-
getTypeChecker(fullTypeCheckMode: boolean): TypeChecker;
937+
getDeclarationDiagnostics(sourceFile: SourceFile): Diagnostic[];
938+
939+
// Gets a type checker that can be used to semantically analyze source fils in the program.
940+
// The 'produceDiagnostics' flag determines if the checker will produce diagnostics while
941+
// analyzing the code. It can be set to 'false' to make many type checking operaitons
942+
// faster. With this flag set, the checker can avoid codepaths only necessary to produce
943+
// diagnostics, but not necessary to answer semantic questions about the code.
944+
//
945+
// If 'produceDiagnostics' is false, then any calls to get diagnostics from the TypeChecker
946+
// will throw an invalid operation exception.
947+
getTypeChecker(produceDiagnostics: boolean): TypeChecker;
936948
getCommonSourceDirectory(): string;
949+
950+
emitFiles(targetSourceFile?: SourceFile): EmitResult;
951+
isEmitBlocked(sourceFile?: SourceFile): boolean;
937952
}
938953

939954
export interface SourceMapSpan {
@@ -973,16 +988,22 @@ module ts {
973988
sourceMaps: SourceMapData[]; // Array of sourceMapData if compiler emitted sourcemaps
974989
}
975990

991+
export interface TypeCheckerHost {
992+
getCompilerOptions(): CompilerOptions;
993+
getCompilerHost(): CompilerHost;
994+
995+
getSourceFiles(): SourceFile[];
996+
getSourceFile(filename: string): SourceFile;
997+
}
998+
976999
export interface TypeChecker {
977-
getProgram(): Program;
1000+
getEmitResolver(): EmitResolver;
9781001
getDiagnostics(sourceFile?: SourceFile): Diagnostic[];
979-
getDeclarationDiagnostics(sourceFile: SourceFile): Diagnostic[];
9801002
getGlobalDiagnostics(): Diagnostic[];
9811003
getNodeCount(): number;
9821004
getIdentifierCount(): number;
9831005
getSymbolCount(): number;
9841006
getTypeCount(): number;
985-
emitFiles(targetSourceFile?: SourceFile): EmitResult;
9861007
getTypeOfSymbolAtLocation(symbol: Symbol, node: Node): Type;
9871008
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
9881009
getPropertiesOfType(type: Type): Symbol[];
@@ -1006,7 +1027,7 @@ module ts {
10061027
isImplementationOfOverload(node: FunctionLikeDeclaration): boolean;
10071028
isUndefinedSymbol(symbol: Symbol): boolean;
10081029
isArgumentsSymbol(symbol: Symbol): boolean;
1009-
isEmitBlocked(sourceFile?: SourceFile): boolean;
1030+
10101031
// Returns the constant value of this enum member, or 'undefined' if the enum member has a computed value.
10111032
getEnumMemberValue(node: EnumMember): number;
10121033
isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: string): boolean;
@@ -1088,7 +1109,6 @@ module ts {
10881109
}
10891110

10901111
export interface EmitResolver {
1091-
getProgram(): Program;
10921112
getLocalNameOfContainer(container: ModuleDeclaration | EnumDeclaration): string;
10931113
getExpressionNamePrefix(node: Identifier): string;
10941114
getExportAssignmentName(node: SourceFile): string;
@@ -1105,7 +1125,6 @@ module ts {
11051125
isEntityNameVisible(entityName: EntityName, enclosingDeclaration: Node): SymbolVisibilityResult;
11061126
// Returns the constant value this property access resolves to, or 'undefined' for a non-constant
11071127
getConstantValue(node: PropertyAccessExpression | ElementAccessExpression): number;
1108-
isEmitBlocked(sourceFile?: SourceFile): boolean;
11091128
isUnknownIdentifier(location: Node, name: string): boolean;
11101129
}
11111130

src/compiler/utilities.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,11 @@ module ts {
691691
return undefined;
692692
}
693693

694-
export function tryResolveScriptReference(program: Program, sourceFile: SourceFile, reference: FileReference) {
695-
if (!program.getCompilerOptions().noResolve) {
694+
export function tryResolveScriptReference(host: ScriptReferenceHost, sourceFile: SourceFile, reference: FileReference) {
695+
if (!host.getCompilerOptions().noResolve) {
696696
var referenceFileName = isRootedDiskPath(reference.filename) ? reference.filename : combinePaths(getDirectoryPath(sourceFile.filename), reference.filename);
697-
referenceFileName = getNormalizedAbsolutePath(referenceFileName, program.getCompilerHost().getCurrentDirectory());
698-
return program.getSourceFile(referenceFileName);
697+
referenceFileName = getNormalizedAbsolutePath(referenceFileName, host.getCurrentDirectory());
698+
return host.getSourceFile(referenceFileName);
699699
}
700700
}
701701

@@ -790,6 +790,21 @@ module ts {
790790
return false;
791791
}
792792

793+
export function createEmitHostFromProgram(program: Program): EmitHost {
794+
var compilerHost = program.getCompilerHost();
795+
return {
796+
getCanonicalFileName: compilerHost.getCanonicalFileName,
797+
getCommonSourceDirectory: program.getCommonSourceDirectory,
798+
getCompilerOptions: program.getCompilerOptions,
799+
getCurrentDirectory: compilerHost.getCurrentDirectory,
800+
getNewLine: compilerHost.getNewLine,
801+
getSourceFile: program.getSourceFile,
802+
getSourceFiles: program.getSourceFiles,
803+
isEmitBlocked: program.isEmitBlocked,
804+
writeFile: compilerHost.writeFile,
805+
};
806+
}
807+
793808
export function textSpanEnd(span: TextSpan) {
794809
return span.start + span.length
795810
}

0 commit comments

Comments
 (0)