From a9025889450b483e17db44b7e927a399c131a3a0 Mon Sep 17 00:00:00 2001 From: Ilya Trubachev Date: Mon, 7 Oct 2024 19:06:20 +0300 Subject: [PATCH] Support negative numbers in annotations Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IAV7EW Signed-off-by: Ilya Trubachev Change-Id: I6299aea6360b3371bab0c6e900ae67b88eef88bc --- es2panda/compiler/core/emitter/emitter.cpp | 38 ++++++++++++++++++- .../declaration-3d-array-enum-number.ts | 4 +- .../declaration-3d-array-number.ts | 2 +- .../declaration-array-enum-number.ts | 4 +- .../annotations/declaration-array-number.ts | 2 +- .../annotations/declaration-combination.ts | 6 +-- .../annotations/declaration-number.ts | 4 ++ .../declaration-usage-3d-array-enum-number.ts | 4 +- .../declaration-usage-3d-array-number.ts | 2 +- .../declaration-usage-array-enum-number.ts | 4 +- .../declaration-usage-array-number.ts | 2 +- .../declaration-usage-combination.ts | 10 ++--- .../declaration-usage-enum-number.ts | 4 +- .../annotations/declaration-usage-number.ts | 2 +- 14 files changed, 64 insertions(+), 24 deletions(-) diff --git a/es2panda/compiler/core/emitter/emitter.cpp b/es2panda/compiler/core/emitter/emitter.cpp index 1498b00b6d..1014a2ca68 100644 --- a/es2panda/compiler/core/emitter/emitter.cpp +++ b/es2panda/compiler/core/emitter/emitter.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -317,6 +318,16 @@ static std::vector ProcessArrayExpression static_cast(panda::panda_file::LiteralTag::STRING)}); literals.emplace_back( pandasm::LiteralArray::Literal {panda::panda_file::LiteralTag::STRING, stringValue}); + } else if (elem->IsUnaryExpression()) { + ASSERT(elem->AsUnaryExpression()->IsNegativeNumber()); + // In annotations other unary operators (+, !, ~) are evaluated in tsc. + // Only unary minus may be present in intermediate .ts + double doubleValue = (-1) * elem->AsUnaryExpression()->Argument()->AsNumberLiteral()->Number(); + literals.emplace_back( + pandasm::LiteralArray::Literal {panda::panda_file::LiteralTag::TAGVALUE, + static_cast(panda::panda_file::LiteralTag::DOUBLE)}); + literals.emplace_back( + pandasm::LiteralArray::Literal {panda::panda_file::LiteralTag::DOUBLE, doubleValue}); } else { UNREACHABLE(); } @@ -371,6 +382,14 @@ pandasm::AnnotationElement FunctionEmitter::CreateAnnotationElement(const std::s return pandasm::AnnotationElement { propName, std::make_unique( pandasm::ScalarValue::Create(stringValue))}; + } else if (initValue->IsUnaryExpression()) { + ASSERT(initValue->AsUnaryExpression()->IsNegativeNumber()); + // In annotations other unary operators (+, !, ~) are evaluated in tsc. + // Only unary minus may be present in intermediate .ts + double negNumberValue = (-1) * initValue->AsUnaryExpression()->Argument()->AsNumberLiteral()->Number(); + return pandasm::AnnotationElement { + propName, std::make_unique(pandasm::ScalarValue::Create( + negNumberValue))}; } else { UNREACHABLE(); } @@ -832,6 +851,14 @@ void Emitter::CreateEnumProp(const ir::ClassProperty *prop, const std::string &a double doubleValue = value->AsNumberLiteral()->Number(); annoRecordField.metadata->SetValue( panda::pandasm::ScalarValue::Create(doubleValue)); + } else if (value->IsUnaryExpression()) { + ASSERT(value->AsUnaryExpression()->IsNegativeNumber()); + // In annotations other unary operators (+, !, ~) are evaluated in tsc. + // Only unary minus may be present in intermediate .ts + annoRecordField.type = panda::pandasm::Type("f64", 0); + double doubleValue = (-1) * value->AsUnaryExpression()->Argument()->AsNumberLiteral()->Number(); + annoRecordField.metadata->SetValue( + panda::pandasm::ScalarValue::Create(doubleValue)); } else { UNREACHABLE(); } @@ -847,7 +874,16 @@ panda::pandasm::Field Emitter::CreateAnnotationProp(const ir::ClassProperty *pro if (propType == ir::AstNodeType::TS_NUMBER_KEYWORD) { annoRecordField.type = panda::pandasm::Type("f64", 0); if (value != nullptr) { - double doubleValue = value->AsNumberLiteral()->Number(); + double doubleValue = 0.0; + if (value->IsUnaryExpression()) { + ASSERT(value->AsUnaryExpression()->IsNegativeNumber()); + // In annotations other unary operators (+, !, ~) are evaluated in tsc. + // Only unary minus may be present in intermediate .ts + value = value->AsUnaryExpression()->Argument(); + doubleValue = (-1) * value->AsNumberLiteral()->Number(); + } else { + doubleValue = value->AsNumberLiteral()->Number(); + } annoRecordField.metadata->SetValue( panda::pandasm::ScalarValue::Create(doubleValue)); } diff --git a/es2panda/test/compiler/annotations/declaration-3d-array-enum-number.ts b/es2panda/test/compiler/annotations/declaration-3d-array-enum-number.ts index 744a142fe2..8f50c85fbf 100644 --- a/es2panda/test/compiler/annotations/declaration-3d-array-enum-number.ts +++ b/es2panda/test/compiler/annotations/declaration-3d-array-enum-number.ts @@ -15,7 +15,7 @@ const enum E { A = 42, - B = 314 + B = -314 } @interface Anno1 { @@ -29,5 +29,5 @@ const enum E { } @interface Anno3 { - a: E[][][] = [[[42, 314, 42]]]; + a: E[][][] = [[[42, -314, 42]]]; } diff --git a/es2panda/test/compiler/annotations/declaration-3d-array-number.ts b/es2panda/test/compiler/annotations/declaration-3d-array-number.ts index 9b4c19e954..e83e604801 100644 --- a/es2panda/test/compiler/annotations/declaration-3d-array-number.ts +++ b/es2panda/test/compiler/annotations/declaration-3d-array-number.ts @@ -22,5 +22,5 @@ } @interface Anno3 { - a: number[][][] = [[[1, 2, 3], [4, 5, 6]]]; + a: number[][][] = [[[1, -2, 3], [4, -5, 6]]]; } diff --git a/es2panda/test/compiler/annotations/declaration-array-enum-number.ts b/es2panda/test/compiler/annotations/declaration-array-enum-number.ts index 11b0554711..d947e16c08 100644 --- a/es2panda/test/compiler/annotations/declaration-array-enum-number.ts +++ b/es2panda/test/compiler/annotations/declaration-array-enum-number.ts @@ -15,7 +15,7 @@ const enum E { A = 42, - B = 314 + B = -314 } @interface Anno1 { @@ -29,5 +29,5 @@ const enum E { } @interface Anno3 { - a: E[] = [42, 314, 42]; + a: E[] = [42, -314, 42]; } diff --git a/es2panda/test/compiler/annotations/declaration-array-number.ts b/es2panda/test/compiler/annotations/declaration-array-number.ts index 20b83d1843..d573f6ebb3 100644 --- a/es2panda/test/compiler/annotations/declaration-array-number.ts +++ b/es2panda/test/compiler/annotations/declaration-array-number.ts @@ -22,5 +22,5 @@ } @interface Anno3 { - a: number[] = [1, 2, 3]; + a: number[] = [1, -2, 3]; } diff --git a/es2panda/test/compiler/annotations/declaration-combination.ts b/es2panda/test/compiler/annotations/declaration-combination.ts index 79cb3c6032..15fa1399e4 100644 --- a/es2panda/test/compiler/annotations/declaration-combination.ts +++ b/es2panda/test/compiler/annotations/declaration-combination.ts @@ -15,7 +15,7 @@ const enum E { A = 1, - B = 2, + B = -2, C = 3 } @@ -26,10 +26,10 @@ const enum E1 { @interface Anno { a: number - b: number[] = [13, 10] + b: number[] = [13, -10] c: string d: boolean - e: E[] = [1, 2, 3] + e: E[] = [1, -2, 3] f: number[] h: E = new Number(10) as number // no initializer i: E[][][] = [[new Array(0)]] // no initializer diff --git a/es2panda/test/compiler/annotations/declaration-number.ts b/es2panda/test/compiler/annotations/declaration-number.ts index e5c48f9af6..47f96358bc 100644 --- a/es2panda/test/compiler/annotations/declaration-number.ts +++ b/es2panda/test/compiler/annotations/declaration-number.ts @@ -19,4 +19,8 @@ @interface Anno2 { a: number = 42; +} + +@interface Anno3 { + a: number = -314; } \ No newline at end of file diff --git a/es2panda/test/compiler/annotations/declaration-usage-3d-array-enum-number.ts b/es2panda/test/compiler/annotations/declaration-usage-3d-array-enum-number.ts index f30eb62a6f..4b13545fde 100644 --- a/es2panda/test/compiler/annotations/declaration-usage-3d-array-enum-number.ts +++ b/es2panda/test/compiler/annotations/declaration-usage-3d-array-enum-number.ts @@ -15,14 +15,14 @@ const enum E { A = 1, - B = 2 + B = -2 } @interface Anno { a: E[][][] = [[new Array(0)]]; } -@#Anno({a: [[[1, 2, 1]]]}) +@#Anno({a: [[[1, -2, 1]]]}) class A { @#Anno({a: [[new Array(1)]]}) foo() {} diff --git a/es2panda/test/compiler/annotations/declaration-usage-3d-array-number.ts b/es2panda/test/compiler/annotations/declaration-usage-3d-array-number.ts index 31f9b27ced..7cfa6910e0 100644 --- a/es2panda/test/compiler/annotations/declaration-usage-3d-array-number.ts +++ b/es2panda/test/compiler/annotations/declaration-usage-3d-array-number.ts @@ -17,7 +17,7 @@ a: number[][][]; } -@#Anno({a: [[[1, 2, 3]]]}) +@#Anno({a: [[[1, -2, 3]]]}) class A { @#Anno({a: [[new Array()]]}) foo() {} diff --git a/es2panda/test/compiler/annotations/declaration-usage-array-enum-number.ts b/es2panda/test/compiler/annotations/declaration-usage-array-enum-number.ts index e8b230a5ad..6caa2583ef 100644 --- a/es2panda/test/compiler/annotations/declaration-usage-array-enum-number.ts +++ b/es2panda/test/compiler/annotations/declaration-usage-array-enum-number.ts @@ -15,14 +15,14 @@ const enum E { A = 1, - B = 2, + B = -2, } @interface Anno { a: E[] = new Array(0); } -@#Anno({a: [1, 2, 1]}) +@#Anno({a: [1, -2, 1]}) class A { @#Anno({a: new Array(1)}) foo() {} diff --git a/es2panda/test/compiler/annotations/declaration-usage-array-number.ts b/es2panda/test/compiler/annotations/declaration-usage-array-number.ts index 56d78f3258..66a06d2564 100644 --- a/es2panda/test/compiler/annotations/declaration-usage-array-number.ts +++ b/es2panda/test/compiler/annotations/declaration-usage-array-number.ts @@ -17,7 +17,7 @@ a: number[]; } -@#Anno({a: [1, 2, 3]}) +@#Anno({a: [1, -2, 3]}) class A { @#Anno({a: new Array()}) foo() {} diff --git a/es2panda/test/compiler/annotations/declaration-usage-combination.ts b/es2panda/test/compiler/annotations/declaration-usage-combination.ts index ea6f4026f7..6f9f913c04 100644 --- a/es2panda/test/compiler/annotations/declaration-usage-combination.ts +++ b/es2panda/test/compiler/annotations/declaration-usage-combination.ts @@ -15,7 +15,7 @@ const enum E { A = 1, - B = 2, + B = -2, C = 3 } @@ -26,10 +26,10 @@ const enum E1 { @interface Anno { a: number - b: number[] = [13, 10] + b: number[] = [13, -10] c: string d: boolean - e: E[] = [1, 2, 3] + e: E[] = [1, -2, 3] f: number[] h: E = new Number(10) as number // no initializer i: E[][][] = [[new Array(0)]] // no initializer @@ -37,8 +37,8 @@ const enum E1 { k: E1[][][] = [[new Array(2)]] // no initializer } -@#Anno({a: 20, b: [13, 10], c: "ab", d: true, e: [1, 2, 3], f: new Array(), g: [[[0]]], h: 10, i: [[new Array(1)]], j: "B", k: [[new Array(3)]]}) +@#Anno({a: 20, b: [-13, 10], c: "ab", d: true, e: [-1, 2, 3], f: new Array(), g: [[[0]]], h: 10, i: [[new Array(1)]], j: "B", k: [[new Array(3)]]}) class A { - @#Anno({a: 10, b: [1, 2, 3], c: "cde", d: true, e: [1, 2, 3], f: [1], g: [[[0], [1]]], h: 10, i: [[[10], [20]]], j: "B", k: [[["A"], ["B"]]]}) + @#Anno({a: -10, b: [1, 2, -3], c: "cde", d: true, e: [1, -2, 3], f: [1], g: [[[0], [1]]], h: -10, i: [[[10], [20]]], j: "B", k: [[["A"], ["B"]]]}) foo() {} } diff --git a/es2panda/test/compiler/annotations/declaration-usage-enum-number.ts b/es2panda/test/compiler/annotations/declaration-usage-enum-number.ts index d6e0b6e2b2..c82baf2e44 100644 --- a/es2panda/test/compiler/annotations/declaration-usage-enum-number.ts +++ b/es2panda/test/compiler/annotations/declaration-usage-enum-number.ts @@ -15,7 +15,7 @@ const enum E { A = 1, - B = 2 + B = -2 } @interface Anno { @@ -24,6 +24,6 @@ const enum E { @#Anno({a : 1}) class A { - @#Anno({a : 2}) + @#Anno({a : -2}) foo() {} } \ No newline at end of file diff --git a/es2panda/test/compiler/annotations/declaration-usage-number.ts b/es2panda/test/compiler/annotations/declaration-usage-number.ts index 59654b5af6..a2024b4aed 100644 --- a/es2panda/test/compiler/annotations/declaration-usage-number.ts +++ b/es2panda/test/compiler/annotations/declaration-usage-number.ts @@ -19,6 +19,6 @@ @#Anno({a : 42}) class A { - @#Anno({a : 314}) + @#Anno({a : -314}) foo() {} } \ No newline at end of file -- Gitee