diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 87fc469310256e6f5cdb6c2ae77fe7c722b4cd85..a8537feb6b6fc218cbd65ea4cb62a82715883055 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -300,6 +300,8 @@ public: TypeRelationFlag throw_flag = TypeRelationFlag::NONE); Signature *ResolveConstructExpression(ETSObjectType *type, const ArenaVector &arguments, const lexer::SourcePosition &pos); + Signature *ResolveConstructExpressionParameterless(ETSObjectType *type,const ArenaVector &arguments, + const lexer::SourcePosition &pos); void CheckObjectLiteralArguments(Signature *sig, ArenaVector const &arguments); checker::ETSFunctionType *BuildFunctionSignature(ir::ScriptFunction *func, bool is_construct_sig = false); checker::ETSFunctionType *BuildMethodSignature(ir::MethodDefinition *method); diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index ed412dc6469d9ffd2ccddfa5f77ddb0856695366..4d5c060f0f48b53b8359dae9ca691bac6eee19f3 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -589,6 +589,11 @@ Signature *ETSChecker::ResolveConstructExpression(ETSObjectType *type, const Are return ValidateSignatures(type->ConstructSignatures(), nullptr, arguments, pos, "construct"); } +Signature *ETSChecker::ResolveConstructExpressionParameterless(ETSObjectType *type, const ArenaVector &arguments, + const lexer::SourcePosition &pos) +{ + return ValidateSignatures(type->ConstructSignatures(), nullptr, arguments, pos, "construct",TypeRelationFlag::NO_THROW); +} /* * Object literals do not get checked in the process of call resolution; we need to check them separately * afterwards. diff --git a/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp b/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp index b56d07341e770bf2117b4091904345c98e01cbfa..2dc87860dea02fb7565b7c77f7754292c4acf0a7 100644 --- a/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp +++ b/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp @@ -19,6 +19,9 @@ #include "ir/typeNode.h" #include "compiler/core/ETSGen.h" #include "checker/ETSchecker.h" +#include "ir/ets/etsTypeReference.h" +#include "ir/ets/etsTypeReferencePart.h" +#include "ir/expressions/identifier.h" namespace panda::es2panda::ir { void ETSNewArrayInstanceExpression::TransformChildren(const NodeTransformer &cb) @@ -51,6 +54,26 @@ void ETSNewArrayInstanceExpression::Compile([[maybe_unused]] compiler::ETSGen *e compiler::VReg dim = etsg->AllocReg(); etsg->ApplyConversionAndStoreAccumulator(this, dim, dimension_->TsType()); etsg->NewArray(this, arr, dim, TsType()); + + std::uint32_t elem_num; + if(dimension_->TsType()->IsIntType()) + { + elem_num = dimension_->TsType()->AsIntType()->GetValue(); + }else{ + elem_num = 0; + } + const auto index_reg = etsg->AllocReg(); + for(std::uint32_t i = 0;i < elem_num;i++) + { + etsg->LoadAccumulatorInt(this, i); + etsg->StoreAccumulator(this, index_reg); + const compiler::TargetTypeContext ttctx2(etsg,type_reference_->TsType()); + if(signature_ != nullptr) + { + etsg->InitObject(this,signature_,arguments_); + etsg->StoreArrayElement(this,arr,index_reg,type_reference_->TsType()); + } + } etsg->SetVRegType(arr, TsType()); etsg->LoadAccumulator(this, arr); } @@ -64,7 +87,13 @@ checker::Type *ETSNewArrayInstanceExpression::Check([[maybe_unused]] checker::ET { auto *element_type = type_reference_->GetType(checker); checker->ValidateArrayIndex(dimension_); - + if(element_type->IsETSObjectType()){ + auto signatures = element_type->AsETSObjectType()->ConstructSignatures(); + if(signatures.size() != 0) + { + signature_ = checker->ResolveConstructExpressionParameterless(element_type->AsETSObjectType(),arguments_,Start()); + } + } SetTsType(checker->CreateETSArrayType(element_type)); checker->CreateBuiltinArraySignature(TsType()->AsETSArrayType(), 1); return TsType(); @@ -77,7 +106,7 @@ ETSNewArrayInstanceExpression *ETSNewArrayInstanceExpression::Clone(ArenaAllocat auto *const type_ref = type_reference_ != nullptr ? type_reference_->Clone(allocator) : nullptr; auto *const dimension = dimension_ != nullptr ? dimension_->Clone(allocator)->AsExpression() : nullptr; - if (auto *const clone = allocator->New(type_ref, dimension); clone != nullptr) { + if (auto *const clone = allocator->New(allocator,type_ref, dimension); clone != nullptr) { if (type_ref != nullptr) { type_ref->SetParent(clone); } diff --git a/ets2panda/ir/ets/etsNewArrayInstanceExpression.h b/ets2panda/ir/ets/etsNewArrayInstanceExpression.h index 9cafcc56e83cd9a827e29f86a5ff96b4be39be7a..e1547e7fa8d0fdadca3b307643a5024474370ad2 100644 --- a/ets2panda/ir/ets/etsNewArrayInstanceExpression.h +++ b/ets2panda/ir/ets/etsNewArrayInstanceExpression.h @@ -17,7 +17,9 @@ #define ES2PANDA_IR_ETS_NEW_ARRAY_INSTANCE_EXPRESSION_H #include "ir/expression.h" - +namespace panda::es2panda::checker{ +class Signature; +} namespace panda::es2panda::ir { class ETSNewArrayInstanceExpression : public Expression { @@ -28,10 +30,11 @@ public: NO_COPY_SEMANTIC(ETSNewArrayInstanceExpression); NO_MOVE_SEMANTIC(ETSNewArrayInstanceExpression); - explicit ETSNewArrayInstanceExpression(ir::TypeNode *const type_reference, ir::Expression *const dimension) + explicit ETSNewArrayInstanceExpression(ArenaAllocator *allocator,ir::TypeNode *const type_reference, ir::Expression *const dimension) : Expression(AstNodeType::ETS_NEW_ARRAY_INSTANCE_EXPRESSION), type_reference_(type_reference), - dimension_(dimension) + dimension_(dimension), + arguments_(allocator->Adapter()) { } @@ -49,6 +52,8 @@ public: private: ir::TypeNode *type_reference_; ir::Expression *dimension_; + ArenaVector arguments_; + checker::Signature *signature_{}; }; } // namespace panda::es2panda::ir diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 1100af3be59744c72fa13015407b4cf5ab5d647c..14bdf1266baef2e75ccb55f027f6bdfa3a835734 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -3939,7 +3939,7 @@ ir::Expression *ETSParser::ParseNewExpression() ExpectToken(lexer::TokenType::PUNCTUATOR_RIGHT_SQUARE_BRACKET); if (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_LEFT_SQUARE_BRACKET) { - auto *arr_instance = AllocNode(type_reference, dimension); + auto *arr_instance = AllocNode(Allocator(),type_reference, dimension); arr_instance->SetRange({start, end_loc}); return arr_instance; } diff --git a/ets2panda/test/parser/ets/array_creation_expression-expected.txt b/ets2panda/test/parser/ets/array_creation_expression-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..21acef35322b283c9ea1225e30c781772678986d --- /dev/null +++ b/ets2panda/test/parser/ets/array_creation_expression-expected.txt @@ -0,0 +1,1742 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "y", + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 6 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 4, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 4, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ListTest", + "decorators": [], + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 15 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 6, + "column": 16 + }, + "end": { + "line": 6, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 6, + "column": 16 + }, + "end": { + "line": 6, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 6, + "column": 15 + }, + "end": { + "line": 6, + "column": 18 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 9, + "column": 9 + }, + "end": { + "line": 9, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 9, + "column": 14 + }, + "end": { + "line": 9, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 9, + "column": 9 + }, + "end": { + "line": 9, + "column": 17 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 10, + "loc": { + "start": { + "line": 9, + "column": 20 + }, + "end": { + "line": 9, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 9 + }, + "end": { + "line": 9, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 9, + "column": 9 + }, + "end": { + "line": 9, + "column": 23 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "data", + "decorators": [], + "loc": { + "start": { + "line": 10, + "column": 13 + }, + "end": { + "line": 10, + "column": 17 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 10, + "column": 24 + }, + "end": { + "line": 10, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 24 + }, + "end": { + "line": 10, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 24 + }, + "end": { + "line": 10, + "column": 26 + } + } + }, + "dimension": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 10, + "column": 26 + }, + "end": { + "line": 10, + "column": 30 + } + } + }, + "property": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 10, + "column": 31 + }, + "end": { + "line": 10, + "column": 34 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 10, + "column": 26 + }, + "end": { + "line": 10, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 20 + }, + "end": { + "line": 10, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 10, + "column": 13 + }, + "end": { + "line": 10, + "column": 35 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 10, + "column": 9 + }, + "end": { + "line": 10, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 8, + "column": 5 + }, + "end": { + "line": 11, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 7, + "column": 16 + }, + "end": { + "line": 11, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 7, + "column": 16 + }, + "end": { + "line": 11, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 7, + "column": 5 + }, + "end": { + "line": 11, + "column": 6 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "data", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 13 + }, + "end": { + "line": 12, + "column": 17 + } + } + }, + "accessibility": "private", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "TSArrayType", + "elementType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 12, + "column": 19 + }, + "end": { + "line": 12, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 19 + }, + "end": { + "line": 12, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 19 + }, + "end": { + "line": 12, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 12, + "column": 22 + }, + "end": { + "line": 12, + "column": 23 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 13, + "column": 13 + }, + "end": { + "line": 13, + "column": 16 + } + } + }, + "accessibility": "private", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 13, + "column": 18 + }, + "end": { + "line": 13, + "column": 21 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 6, + "column": 19 + }, + "end": { + "line": 14, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 6, + "column": 1 + }, + "end": { + "line": 14, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "s", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 26 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "n", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 26 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "i", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 23 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 20, + "column": 23 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 25 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "y", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewArrayInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "ListTest", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 25 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 26 + }, + "end": { + "line": 21, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 26 + }, + "end": { + "line": 21, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 26 + }, + "end": { + "line": 21, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 25 + }, + "end": { + "line": 21, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 17 + }, + "end": { + "line": 21, + "column": 33 + } + } + }, + "dimension": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 21, + "column": 33 + }, + "end": { + "line": 21, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 13 + }, + "end": { + "line": 21, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 35 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 22, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 25, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/array_creation_expression.ets b/ets2panda/test/parser/ets/array_creation_expression.ets new file mode 100644 index 0000000000000000000000000000000000000000..7e04ccf0590e26949362b45840601014b241d35d --- /dev/null +++ b/ets2panda/test/parser/ets/array_creation_expression.ets @@ -0,0 +1,24 @@ +class Point { + x: number; + y: number; +} + +class ListTest { + constructor() + { + this.num = 10; + let data = new T[this.num]; + } + private data: T[]; + private num: int; +} + +function main(): void { + let s = new string[3]; + let n = new number[3]; + let i = new Int[3]; + let x = new Point[3]; + let y = new ListTest[3]; +} + + diff --git a/ets2panda/test/runtime/ets/array_creation_expression.ets b/ets2panda/test/runtime/ets/array_creation_expression.ets new file mode 100644 index 0000000000000000000000000000000000000000..15c39e8af34a8c95076382c16b5b44bb8bf6b2f7 --- /dev/null +++ b/ets2panda/test/runtime/ets/array_creation_expression.ets @@ -0,0 +1,33 @@ +class Point { + x: number; + y: number; +} + +class ListTest { + constructor() + { + this.num = 10; + let data = new T[this.num] + } + private data: T; + public num: int; +} + +function main(): void { + let stringArr = new String[3]; + let A = stringArr[0] as String; + assert stringArr[0] == ""; + + let numberArr = new number[3]; + let B = numberArr[0] as number; + assert numberArr[0] == 0.0 + + let PointArr = new Point[3]; + let C = PointArr[0] as Point; + assert PointArr[0].x == 0; + assert PointArr[0].y == 0; + + let ListTestArr = new ListTest[3]; + let D = ListTestArr[0] as ListTest; + assert ListTestArr[0].num == 10; +}