Skip to content

Commit

Permalink
Copy monomorphization from compiler to services
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Apr 2, 2024
1 parent 7c1af0b commit 14249be
Show file tree
Hide file tree
Showing 108 changed files with 112 additions and 334 deletions.
7 changes: 7 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8154,6 +8154,7 @@ export interface ObjectAllocator {
}

function Symbol(this: Symbol, flags: SymbolFlags, name: __String) {
// Note: must match SymbolObject in src/services/services.ts
this.flags = flags;
this.escapedName = name;
this.declarations = undefined;
Expand All @@ -8171,20 +8172,23 @@ function Symbol(this: Symbol, flags: SymbolFlags, name: __String) {
}

function Type(this: Type, checker: TypeChecker, flags: TypeFlags) {
// Note: must match TypeObject in src/services/services.ts
this.flags = flags;
if (Debug.isDebugging || tracing) {
this.checker = checker;
}
}

function Signature(this: Signature, checker: TypeChecker, flags: SignatureFlags) {
// Note: must match SignatureObject in src/services/services.ts
this.flags = flags;
if (Debug.isDebugging) {
this.checker = checker;
}
}

function Node(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number) {
// Note: must match NodeObject in src/services/services.ts
this.pos = pos;
this.end = end;
this.kind = kind;
Expand All @@ -8198,6 +8202,7 @@ function Node(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number) {
}

function Token(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number) {
// Note: must match TokenOrIdentifierObject in src/services/services.ts
this.pos = pos;
this.end = end;
this.kind = kind;
Expand All @@ -8209,6 +8214,7 @@ function Token(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number)
}

function Identifier(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number) {
// Note: must match TokenOrIdentifierObject in src/services/services.ts
this.pos = pos;
this.end = end;
this.kind = kind;
Expand All @@ -8221,6 +8227,7 @@ function Identifier(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: num
}

function SourceMapSource(this: SourceMapSource, fileName: string, text: string, skipTrivia?: (pos: number) => number) {
// Note: must match SourceMapSourceObject in src/services/services.ts
this.fileName = fileName;
this.text = text;
this.skipTrivia = skipTrivia || (pos => pos);
Expand Down
106 changes: 71 additions & 35 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
EditorOptions,
EditorSettings,
ElementAccessExpression,
EmitNode,
EmitTextWriter,
emptyArray,
emptyOptions,
Expand Down Expand Up @@ -289,6 +290,7 @@ import {
SymbolDisplayPart,
SymbolFlags,
symbolName,
SymbolTable,
SyntaxKind,
SyntaxList,
sys,
Expand Down Expand Up @@ -333,7 +335,7 @@ import * as classifier2020 from "./classifier2020";
/** The version of the language service API */
export const servicesVersion = "0.8";

function createNode<TKind extends SyntaxKind>(kind: TKind, pos: number, end: number, parent: Node): NodeObject | TokenObject<TKind> | IdentifierObject | PrivateIdentifierObject {
function createNode<TKind extends SyntaxKind>(kind: TKind, pos: number, end: number, parent: Node): NodeObject<TKind> | TokenObject<TKind> | IdentifierObject | PrivateIdentifierObject {
const node = isNodeKind(kind) ? new NodeObject(kind, pos, end) :
kind === SyntaxKind.Identifier ? new IdentifierObject(SyntaxKind.Identifier, pos, end) :
kind === SyntaxKind.PrivateIdentifier ? new PrivateIdentifierObject(SyntaxKind.PrivateIdentifier, pos, end) :
Expand All @@ -343,8 +345,8 @@ function createNode<TKind extends SyntaxKind>(kind: TKind, pos: number, end: num
return node;
}

class NodeObject implements Node {
public kind: SyntaxKind;
class NodeObject<TKind extends SyntaxKind> implements Node {
public kind: TKind;
public pos: number;
public end: number;
public flags: NodeFlags;
Expand All @@ -355,15 +357,21 @@ class NodeObject implements Node {
public jsDoc?: JSDoc[];
public original?: Node;
private _children: Node[] | undefined;
public id?: number;
public emitNode?: EmitNode;

constructor(kind: SyntaxKind, pos: number, end: number) {
constructor(kind: TKind, pos: number, end: number) {
// Note: must match Node in src/compiler/utilities.ts
this.pos = pos;
this.end = end;
this.kind = kind;
this.id = 0;
this.flags = NodeFlags.None;
this.modifierFlagsCache = ModifierFlags.None;
this.transformFlags = TransformFlags.None;
this.parent = undefined!;
this.kind = kind;
this.original = undefined;
this.emitNode = undefined;
}

private assertHasRealPosition(message?: string) {
Expand Down Expand Up @@ -534,25 +542,30 @@ function createSyntaxList(nodes: NodeArray<Node>, parent: Node): Node {
return list;
}

class TokenOrIdentifierObject implements Node {
public kind!: SyntaxKind;
class TokenOrIdentifierObject<TKind extends SyntaxKind> implements Node {
public kind: TKind;
public pos: number;
public end: number;
public flags: NodeFlags;
public modifierFlagsCache: ModifierFlags;
public modifierFlagsCache!: ModifierFlags;
public transformFlags: TransformFlags;
public parent: Node;
public symbol!: Symbol;
public jsDocComments?: JSDoc[];
public id?: number;
public emitNode?: EmitNode | undefined;

constructor(pos: number, end: number) {
constructor(kind: TKind, pos: number, end: number) {
// Set properties in same order as NodeObject
// Note: must match Token and Identifier in src/compiler/utilities.ts
this.pos = pos;
this.end = end;
this.kind = kind;
this.id = 0;
this.flags = NodeFlags.None;
this.modifierFlagsCache = ModifierFlags.None;
this.transformFlags = TransformFlags.None;
this.parent = undefined!;
this.emitNode = undefined;
}

public getSourceFile(): SourceFile {
Expand Down Expand Up @@ -622,11 +635,17 @@ class TokenOrIdentifierObject implements Node {
class SymbolObject implements Symbol {
flags: SymbolFlags;
escapedName: __String;
declarations!: Declaration[];
valueDeclaration!: Declaration;
id = 0;
mergeId = 0;
declarations?: Declaration[];
valueDeclaration?: Declaration;
members?: SymbolTable;
exports?: SymbolTable;
id: number;
mergeId: number;
parent?: Symbol;
exportSymbol?: Symbol;
constEnumOnlyModule: boolean | undefined;
isReferenced?: SymbolFlags;
lastAssignmentPos?: number;

// Undefined is used to indicate the value has not been computed. If, after computing, the
// symbol has no doc comment, then the empty array will be returned.
Expand All @@ -640,8 +659,21 @@ class SymbolObject implements Symbol {
contextualSetAccessorTags?: JSDocTagInfo[];

constructor(flags: SymbolFlags, name: __String) {
// Note: must match Symbol in src/compiler/types.ts
this.flags = flags;
this.escapedName = name;
this.declarations = undefined;
this.valueDeclaration = undefined;
this.id = 0;
this.mergeId = 0;
this.parent = undefined;
this.members = undefined;
this.exports = undefined;
this.exportSymbol = undefined;
this.constEnumOnlyModule = undefined;
this.isReferenced = undefined;
this.lastAssignmentPos = undefined;
(this as any).links = undefined; // used by TransientSymbol
}

getFlags(): SymbolFlags {
Expand Down Expand Up @@ -732,17 +764,13 @@ class SymbolObject implements Symbol {
}
}

class TokenObject<TKind extends SyntaxKind> extends TokenOrIdentifierObject implements Token<TKind> {
public override kind: TKind;

class TokenObject<TKind extends SyntaxKind> extends TokenOrIdentifierObject<TKind> implements Token<TKind> {
constructor(kind: TKind, pos: number, end: number) {
super(pos, end);
this.kind = kind;
super(kind, pos, end);
}
}

class IdentifierObject extends TokenOrIdentifierObject implements Identifier {
public override kind: SyntaxKind.Identifier = SyntaxKind.Identifier;
class IdentifierObject extends TokenOrIdentifierObject<SyntaxKind.Identifier> implements Identifier {
public escapedText!: __String;
declare _primaryExpressionBrand: any;
declare _memberExpressionBrand: any;
Expand All @@ -754,33 +782,31 @@ class IdentifierObject extends TokenOrIdentifierObject implements Identifier {
declare _jsdocContainerBrand: any;
declare _flowContainerBrand: any;
/** @internal */ typeArguments!: NodeArray<TypeNode>;
constructor(_kind: SyntaxKind.Identifier, pos: number, end: number) {
super(pos, end);
constructor(kind: SyntaxKind.Identifier, pos: number, end: number) {
super(kind, pos, end);
}

get text(): string {
return idText(this);
}
}
IdentifierObject.prototype.kind = SyntaxKind.Identifier;
class PrivateIdentifierObject extends TokenOrIdentifierObject implements PrivateIdentifier {
public override kind: SyntaxKind.PrivateIdentifier = SyntaxKind.PrivateIdentifier;

class PrivateIdentifierObject extends TokenOrIdentifierObject<SyntaxKind.PrivateIdentifier> implements PrivateIdentifier {
public escapedText!: __String;
declare _primaryExpressionBrand: any;
declare _memberExpressionBrand: any;
declare _leftHandSideExpressionBrand: any;
declare _updateExpressionBrand: any;
declare _unaryExpressionBrand: any;
declare _expressionBrand: any;
constructor(_kind: SyntaxKind.PrivateIdentifier, pos: number, end: number) {
super(pos, end);
constructor(kind: SyntaxKind.PrivateIdentifier, pos: number, end: number) {
super(kind, pos, end);
}

get text(): string {
return idText(this);
}
}
PrivateIdentifierObject.prototype.kind = SyntaxKind.PrivateIdentifier;

class TypeObject implements Type {
checker: TypeChecker;
Expand All @@ -789,8 +815,9 @@ class TypeObject implements Type {
id!: number;
symbol!: Symbol;
constructor(checker: TypeChecker, flags: TypeFlags) {
this.checker = checker;
// Note: must match Type in src/compiler/types.ts
this.flags = flags;
this.checker = checker;
}
getFlags(): TypeFlags {
return this.flags;
Expand Down Expand Up @@ -897,8 +924,9 @@ class SignatureObject implements Signature {
jsDocTags?: JSDocTagInfo[]; // same

constructor(checker: TypeChecker, flags: SignatureFlags) {
this.checker = checker;
// Note: must match Signature in src/compiler/types.ts
this.flags = flags;
this.checker = checker;
}

getDeclaration(): SignatureDeclaration {
Expand Down Expand Up @@ -1002,8 +1030,7 @@ function findBaseOfDeclaration<T>(checker: TypeChecker, declaration: Declaration
});
}

class SourceFileObject extends NodeObject implements SourceFile {
public override kind: SyntaxKind.SourceFile = SyntaxKind.SourceFile;
class SourceFileObject extends NodeObject<SyntaxKind.SourceFile> implements SourceFile {
declare _declarationBrand: any;
declare _localsContainerBrand: any;
public fileName!: string;
Expand Down Expand Up @@ -1053,7 +1080,7 @@ class SourceFileObject extends NodeObject implements SourceFile {
public localJsxFactory: EntityName | undefined;
public localJsxNamespace: __String | undefined;

constructor(kind: SyntaxKind, pos: number, end: number) {
constructor(kind: SyntaxKind.SourceFile, pos: number, end: number) {
super(kind, pos, end);
}

Expand Down Expand Up @@ -1248,8 +1275,17 @@ class SourceFileObject extends NodeObject implements SourceFile {
}

class SourceMapSourceObject implements SourceMapSource {
fileName: string;
text: string;
skipTrivia?: ((pos: number) => number) | undefined;
lineMap!: number[];
constructor(public fileName: string, public text: string, public skipTrivia?: (pos: number) => number) {}

constructor(fileName: string, text: string, skipTrivia?: (pos: number) => number) {
// Note: must match SourceMapSource in src/compiler/types.ts
this.fileName = fileName;
this.text = text;
this.skipTrivia = skipTrivia || (pos => pos);
}

public getLineAndCharacterOfPosition(pos: number): LineAndCharacter {
return getLineAndCharacterOfPosition(this, pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"kind": "Identifier",
"pos": 8,
"end": 13,
"modifierFlagsCache": 0,
"transformFlags": 0,
"escapedText": "param"
},
Expand All @@ -25,7 +24,6 @@
"kind": "Identifier",
"pos": 14,
"end": 18,
"modifierFlagsCache": 0,
"transformFlags": 0,
"escapedText": "this"
},
Expand Down
Loading

0 comments on commit 14249be

Please sign in to comment.