Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt non-android specific changes from android branch #692

Merged
merged 7 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions src.compiler/BuilderHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
import * as ts from 'typescript';

export function setMethodBody(m: ts.MethodDeclaration, body: ts.FunctionBody): ts.MethodDeclaration {
return ts.factory.updateMethodDeclaration(
m,
m.decorators,
m.modifiers,
m.asteriskToken,
m.name,
m.questionToken,
m.typeParameters,
m.parameters,
m.type,
body
);
}

export function createNodeFromSource<T extends ts.Node>(source: string, kind: ts.SyntaxKind): T {
const sourceFile = ts.createSourceFile(
'temp.ts',
source.trim(),
ts.ScriptTarget.Latest,
/*setParentNodes */ true,
ts.ScriptKind.TS
);
const node = findNode(sourceFile, kind);
if (!node) {
throw new Error(
`Could not parse TS source to ${ts.SyntaxKind[kind]}, node count was ${sourceFile.getChildCount()}`
);
}
return markNodeSynthesized(node) as T;
}

function findNode(node: ts.Node, kind: ts.SyntaxKind): ts.Node | null {
if (node.kind === kind) {
return node;
}

for (const c of node.getChildren()) {
const f = findNode(c, kind);
if (f) {
return f;
}
}

return null;
}

export function addNewLines(stmts: ts.Statement[]) {
return stmts.map(stmt => ts.addSyntheticTrailingComment(stmt, ts.SyntaxKind.SingleLineCommentTrivia, '', true));
}
export function getTypeWithNullableInfo(checker: ts.TypeChecker, node: ts.TypeNode | undefined) {
if(!node) {
if (!node) {
return {
isNullable: false,
type: {} as ts.Type
Expand All @@ -20,7 +67,12 @@ export function getTypeWithNullableInfo(checker: ts.TypeChecker, node: ts.TypeNo
} else if (ts.isLiteralTypeNode(t) && t.literal.kind === ts.SyntaxKind.NullKeyword) {
isNullable = true;
} else if (type !== null) {
throw new Error('Multi union types on JSON settings not supported: ' + node.getSourceFile().fileName + ':' + node.getText());
throw new Error(
'Multi union types on JSON settings not supported: ' +
node.getSourceFile().fileName +
':' +
node.getText()
);
} else {
type = checker.getTypeAtLocation(t);
}
Expand Down Expand Up @@ -52,7 +104,6 @@ export function unwrapArrayItemType(type: ts.Type, typeChecker: ts.TypeChecker):
return null;
}


export function isPrimitiveType(type: ts.Type | null) {
if (!type) {
return false;
Expand Down Expand Up @@ -85,7 +136,7 @@ export function isNumberType(type: ts.Type | null) {
if (hasFlag(type, ts.TypeFlags.Number)) {
return true;
}

return false;
}

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

export function isMap(type: ts.Type | null): boolean {
return !!(type && type.symbol?.name === 'Map');
}
}

function markNodeSynthesized(node: ts.Node): ts.Node {
for(const c of node.getChildren()) {
markNodeSynthesized(c);
}
ts.setTextRange(node, {
pos: -1,
end: -1
});
return node;
}
13 changes: 12 additions & 1 deletion src.compiler/csharp/CSharpAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum SyntaxKind {
PrimitiveTypeNode,
EnumMember,
ArrayTypeNode,
MapTypeNode,

Block,
EmptyStatement,
Expand Down Expand Up @@ -138,6 +139,7 @@ export interface NamedTypeDeclaration extends NamedElement, DocumentedElement, N
typeParameters?: TypeParameterDeclaration[];
visibility: Visibility;
partial: boolean;
hasVirtualMembersOrSubClasses: boolean;
}

export interface ClassDeclaration extends NamedTypeDeclaration {
Expand Down Expand Up @@ -236,7 +238,7 @@ export interface UnresolvedTypeNode extends TypeNode {
typeArguments?: UnresolvedTypeNode[];
}

export type TypeReferenceType = NamedTypeDeclaration | TypeParameterDeclaration | PrimitiveTypeNode | string;
export type TypeReferenceType = NamedTypeDeclaration | TypeParameterDeclaration | TypeNode | string;
export interface TypeReference extends TypeNode {
reference: TypeReferenceType;
typeArguments?: TypeNode[];
Expand All @@ -246,6 +248,13 @@ export interface ArrayTypeNode extends TypeNode {
elementType: TypeNode;
}

export interface MapTypeNode extends TypeNode {
keyType: TypeNode;
keyIsValueType: boolean;
valueType: TypeNode;
valueIsValueType:boolean;
}

export interface FunctionTypeNode extends TypeNode {
parameterTypes: TypeNode[];
returnType: TypeNode;
Expand Down Expand Up @@ -492,6 +501,7 @@ export interface CatchClause extends Node {
}

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

export function isBlock(node: Node): node is Block { return node.nodeType === SyntaxKind.Block; }
export function isEmptyStatement(node: Node): node is EmptyStatement { return node.nodeType === SyntaxKind.EmptyStatement; }
Expand Down
36 changes: 26 additions & 10 deletions src.compiler/csharp/CSharpAstPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,10 @@ export default class CSharpAstPrinter extends AstPrinterBase {
if (d.isAbstract) {
this.write('abstract ');
}

if (d.isVirtual) {
else if (d.isVirtual) {
this.write('virtual ');
}

if (d.isOverride) {
else if (d.isOverride) {
this.write('override ');
}

Expand Down Expand Up @@ -351,12 +349,10 @@ export default class CSharpAstPrinter extends AstPrinterBase {
if (d.isAbstract) {
this.write('abstract ');
}

if (d.isVirtual) {
else if (d.isVirtual) {
this.write('virtual ');
}

if (d.isOverride) {
else if (d.isOverride) {
this.write('override ');
}
}
Expand Down Expand Up @@ -483,7 +479,7 @@ export default class CSharpAstPrinter extends AstPrinterBase {
this.write('System.Collections.IList');
} else {
if (forNew) {
this.write('AlphaTab.Core.List<');
this.write('AlphaTab.Collections.List<');
} else {
this.write('System.Collections.Generic.IList<');
}
Expand All @@ -492,6 +488,26 @@ export default class CSharpAstPrinter extends AstPrinterBase {
}
}

break;
case cs.SyntaxKind.MapTypeNode:
const mapType = type as cs.MapTypeNode;
if (!mapType.valueIsValueType) {
if (forNew) {
this.write('AlphaTab.Collections.Map<');
} else {
this.write('AlphaTab.Collections.IMap<');
}
} else {
if (forNew) {
this.write('AlphaTab.Collections.ValueTypeMap<');
} else {
this.write('AlphaTab.Collections.IValueTypeMap<');
}
}
this.writeType(mapType.keyType);
this.write(', ');
this.writeType(mapType.valueType);
this.write('>');
break;
case cs.SyntaxKind.FunctionTypeNode:
const functionType = type as cs.FunctionTypeNode;
Expand Down Expand Up @@ -699,7 +715,7 @@ export default class CSharpAstPrinter extends AstPrinterBase {

protected writeNonNullExpression(expr: cs.NonNullExpression) {
this.writeExpression(expr.expression);
if(!cs.isNonNullExpression(expr)) {
if (!cs.isNonNullExpression(expr)) {
this.write('!');
}
}
Expand Down
Loading