Skip to content

Commit 21ede57

Browse files
authored
Adapt non-android specific changes from android branch (#692)
1 parent 2165171 commit 21ede57

File tree

237 files changed

+4934
-3592
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

237 files changed

+4934
-3592
lines changed

src.compiler/BuilderHelpers.ts

+67-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,57 @@
11
import * as ts from 'typescript';
22

3+
export function setMethodBody(m: ts.MethodDeclaration, body: ts.FunctionBody): ts.MethodDeclaration {
4+
return ts.factory.updateMethodDeclaration(
5+
m,
6+
m.decorators,
7+
m.modifiers,
8+
m.asteriskToken,
9+
m.name,
10+
m.questionToken,
11+
m.typeParameters,
12+
m.parameters,
13+
m.type,
14+
body
15+
);
16+
}
17+
18+
export function createNodeFromSource<T extends ts.Node>(source: string, kind: ts.SyntaxKind): T {
19+
const sourceFile = ts.createSourceFile(
20+
'temp.ts',
21+
source.trim(),
22+
ts.ScriptTarget.Latest,
23+
/*setParentNodes */ true,
24+
ts.ScriptKind.TS
25+
);
26+
const node = findNode(sourceFile, kind);
27+
if (!node) {
28+
throw new Error(
29+
`Could not parse TS source to ${ts.SyntaxKind[kind]}, node count was ${sourceFile.getChildCount()}`
30+
);
31+
}
32+
return markNodeSynthesized(node) as T;
33+
}
34+
35+
function findNode(node: ts.Node, kind: ts.SyntaxKind): ts.Node | null {
36+
if (node.kind === kind) {
37+
return node;
38+
}
39+
40+
for (const c of node.getChildren()) {
41+
const f = findNode(c, kind);
42+
if (f) {
43+
return f;
44+
}
45+
}
46+
47+
return null;
48+
}
49+
350
export function addNewLines(stmts: ts.Statement[]) {
451
return stmts.map(stmt => ts.addSyntheticTrailingComment(stmt, ts.SyntaxKind.SingleLineCommentTrivia, '', true));
552
}
653
export function getTypeWithNullableInfo(checker: ts.TypeChecker, node: ts.TypeNode | undefined) {
7-
if(!node) {
54+
if (!node) {
855
return {
956
isNullable: false,
1057
type: {} as ts.Type
@@ -20,7 +67,12 @@ export function getTypeWithNullableInfo(checker: ts.TypeChecker, node: ts.TypeNo
2067
} else if (ts.isLiteralTypeNode(t) && t.literal.kind === ts.SyntaxKind.NullKeyword) {
2168
isNullable = true;
2269
} else if (type !== null) {
23-
throw new Error('Multi union types on JSON settings not supported: ' + node.getSourceFile().fileName + ':' + node.getText());
70+
throw new Error(
71+
'Multi union types on JSON settings not supported: ' +
72+
node.getSourceFile().fileName +
73+
':' +
74+
node.getText()
75+
);
2476
} else {
2577
type = checker.getTypeAtLocation(t);
2678
}
@@ -52,7 +104,6 @@ export function unwrapArrayItemType(type: ts.Type, typeChecker: ts.TypeChecker):
52104
return null;
53105
}
54106

55-
56107
export function isPrimitiveType(type: ts.Type | null) {
57108
if (!type) {
58109
return false;
@@ -85,7 +136,7 @@ export function isNumberType(type: ts.Type | null) {
85136
if (hasFlag(type, ts.TypeFlags.Number)) {
86137
return true;
87138
}
88-
139+
89140
return false;
90141
}
91142

@@ -116,4 +167,15 @@ export function hasFlag(type: ts.Type, flag: ts.TypeFlags): boolean {
116167

117168
export function isMap(type: ts.Type | null): boolean {
118169
return !!(type && type.symbol?.name === 'Map');
119-
}
170+
}
171+
172+
function markNodeSynthesized(node: ts.Node): ts.Node {
173+
for(const c of node.getChildren()) {
174+
markNodeSynthesized(c);
175+
}
176+
ts.setTextRange(node, {
177+
pos: -1,
178+
end: -1
179+
});
180+
return node;
181+
}

src.compiler/csharp/CSharpAst.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export enum SyntaxKind {
2121
PrimitiveTypeNode,
2222
EnumMember,
2323
ArrayTypeNode,
24+
MapTypeNode,
2425

2526
Block,
2627
EmptyStatement,
@@ -138,6 +139,7 @@ export interface NamedTypeDeclaration extends NamedElement, DocumentedElement, N
138139
typeParameters?: TypeParameterDeclaration[];
139140
visibility: Visibility;
140141
partial: boolean;
142+
hasVirtualMembersOrSubClasses: boolean;
141143
}
142144

143145
export interface ClassDeclaration extends NamedTypeDeclaration {
@@ -236,7 +238,7 @@ export interface UnresolvedTypeNode extends TypeNode {
236238
typeArguments?: UnresolvedTypeNode[];
237239
}
238240

239-
export type TypeReferenceType = NamedTypeDeclaration | TypeParameterDeclaration | PrimitiveTypeNode | string;
241+
export type TypeReferenceType = NamedTypeDeclaration | TypeParameterDeclaration | TypeNode | string;
240242
export interface TypeReference extends TypeNode {
241243
reference: TypeReferenceType;
242244
typeArguments?: TypeNode[];
@@ -246,6 +248,13 @@ export interface ArrayTypeNode extends TypeNode {
246248
elementType: TypeNode;
247249
}
248250

251+
export interface MapTypeNode extends TypeNode {
252+
keyType: TypeNode;
253+
keyIsValueType: boolean;
254+
valueType: TypeNode;
255+
valueIsValueType:boolean;
256+
}
257+
249258
export interface FunctionTypeNode extends TypeNode {
250259
parameterTypes: TypeNode[];
251260
returnType: TypeNode;
@@ -492,6 +501,7 @@ export interface CatchClause extends Node {
492501
}
493502

494503
// Node Tests
504+
export function isNode(node: any): node is Node { return typeof(node) === 'object' && 'nodeType' in node; }
495505
export function isSourceFile(node: Node): node is SourceFile { return node.nodeType === SyntaxKind.SourceFile; }
496506
export function isUsingDeclaration(node: Node): node is UsingDeclaration { return node.nodeType === SyntaxKind.UsingDeclaration; }
497507
export function isNamespaceDeclaration(node: Node): node is NamespaceDeclaration { return node.nodeType === SyntaxKind.NamespaceDeclaration; }
@@ -511,6 +521,7 @@ export function isFunctionTypeNode(node: Node): node is FunctionTypeNode { retur
511521
export function isPrimitiveTypeNode(node: Node): node is PrimitiveTypeNode { return node.nodeType === SyntaxKind.PrimitiveTypeNode; }
512522
export function isEnumMember(node: Node): node is EnumMember { return node.nodeType === SyntaxKind.EnumMember; }
513523
export function isArrayTypeNode(node: Node): node is ArrayTypeNode { return node.nodeType === SyntaxKind.ArrayTypeNode; }
524+
export function isMapTypeNode(node: Node): node is MapTypeNode { return node.nodeType === SyntaxKind.MapTypeNode; }
514525

515526
export function isBlock(node: Node): node is Block { return node.nodeType === SyntaxKind.Block; }
516527
export function isEmptyStatement(node: Node): node is EmptyStatement { return node.nodeType === SyntaxKind.EmptyStatement; }

src.compiler/csharp/CSharpAstPrinter.ts

+26-10
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,10 @@ export default class CSharpAstPrinter extends AstPrinterBase {
258258
if (d.isAbstract) {
259259
this.write('abstract ');
260260
}
261-
262-
if (d.isVirtual) {
261+
else if (d.isVirtual) {
263262
this.write('virtual ');
264263
}
265-
266-
if (d.isOverride) {
264+
else if (d.isOverride) {
267265
this.write('override ');
268266
}
269267

@@ -351,12 +349,10 @@ export default class CSharpAstPrinter extends AstPrinterBase {
351349
if (d.isAbstract) {
352350
this.write('abstract ');
353351
}
354-
355-
if (d.isVirtual) {
352+
else if (d.isVirtual) {
356353
this.write('virtual ');
357354
}
358-
359-
if (d.isOverride) {
355+
else if (d.isOverride) {
360356
this.write('override ');
361357
}
362358
}
@@ -483,7 +479,7 @@ export default class CSharpAstPrinter extends AstPrinterBase {
483479
this.write('System.Collections.IList');
484480
} else {
485481
if (forNew) {
486-
this.write('AlphaTab.Core.List<');
482+
this.write('AlphaTab.Collections.List<');
487483
} else {
488484
this.write('System.Collections.Generic.IList<');
489485
}
@@ -492,6 +488,26 @@ export default class CSharpAstPrinter extends AstPrinterBase {
492488
}
493489
}
494490

491+
break;
492+
case cs.SyntaxKind.MapTypeNode:
493+
const mapType = type as cs.MapTypeNode;
494+
if (!mapType.valueIsValueType) {
495+
if (forNew) {
496+
this.write('AlphaTab.Collections.Map<');
497+
} else {
498+
this.write('AlphaTab.Collections.IMap<');
499+
}
500+
} else {
501+
if (forNew) {
502+
this.write('AlphaTab.Collections.ValueTypeMap<');
503+
} else {
504+
this.write('AlphaTab.Collections.IValueTypeMap<');
505+
}
506+
}
507+
this.writeType(mapType.keyType);
508+
this.write(', ');
509+
this.writeType(mapType.valueType);
510+
this.write('>');
495511
break;
496512
case cs.SyntaxKind.FunctionTypeNode:
497513
const functionType = type as cs.FunctionTypeNode;
@@ -699,7 +715,7 @@ export default class CSharpAstPrinter extends AstPrinterBase {
699715

700716
protected writeNonNullExpression(expr: cs.NonNullExpression) {
701717
this.writeExpression(expr.expression);
702-
if(!cs.isNonNullExpression(expr)) {
718+
if (!cs.isNonNullExpression(expr)) {
703719
this.write('!');
704720
}
705721
}

0 commit comments

Comments
 (0)