diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 25eee1652d3c5669a649c356c61fd7733b41ff85..a620bca35387a70f1cf4409aa420fb4ca3256431 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -2073,12 +2073,18 @@ void ETSChecker::GenerateGetterSetterBody(ArenaVector &stmts, A bool isSetter) { auto *classDef = field->Parent()->AsClassDefinition(); - auto *thisExpression = Allocator()->New(); - thisExpression->SetParent(classDef); - thisExpression->SetTsType(classDef->TsType()); + + ir::Expression *baseExpression; + if ((field->Modifiers() & ir::ModifierFlags::SUPER_OWNER) != 0U) { + baseExpression = Allocator()->New(); + } else { + baseExpression = Allocator()->New(); + } + baseExpression->SetParent(classDef); + baseExpression->SetTsType(classDef->TsType()); auto *memberExpression = - AllocNode(thisExpression, field->Key()->AsIdentifier()->Clone(Allocator(), nullptr), + AllocNode(baseExpression, field->Key()->AsIdentifier()->Clone(Allocator(), nullptr), ir::MemberExpressionKind::PROPERTY_ACCESS, false, false); memberExpression->SetTsType(field->TsType()); memberExpression->SetPropVar(field->Key()->Variable()->AsLocalVariable()); @@ -2155,6 +2161,7 @@ ir::MethodDefinition *ETSChecker::GenerateDefaultGetterSetter(ir::ClassProperty checker->Allocator(), property->Key()->AsIdentifier()->Name(), property->Key()->AsIdentifier()->Variable()->Declaration()->Node()); auto *var = functionScope->AddDecl(checker->Allocator(), decl, ScriptExtension::ETS); + var->AddFlag(varbinder::VariableFlags::METHOD); methodIdent->SetVariable(var); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) @@ -2174,10 +2181,8 @@ ir::MethodDefinition *ETSChecker::GenerateDefaultGetterSetter(ir::ClassProperty paramScope->BindNode(func); functionScope->BindNode(func); - { - auto classCtx = varbinder::LexicalScope::Enter(checker->VarBinder(), classScope); - checker->VarBinder()->AsETSBinder()->ResolveMethodDefinition(method); - } + auto classCtx = varbinder::LexicalScope::Enter(checker->VarBinder(), classScope); + checker->VarBinder()->AsETSBinder()->ResolveMethodDefinition(method); functionScope->BindName(classScope->Node()->AsClassDefinition()->InternalName()); method->Check(checker); @@ -2185,6 +2190,30 @@ ir::MethodDefinition *ETSChecker::GenerateDefaultGetterSetter(ir::ClassProperty return method; } +ir::ClassProperty *GetImplementationClassProp(ETSChecker *checker, ir::ClassProperty *interfaceProp, + ir::ClassProperty *originalProp, ETSObjectType *classType) +{ + bool isSuperOwner = ((originalProp->Modifiers() & ir::ModifierFlags::SUPER_OWNER) != 0U); + if (!isSuperOwner) { + auto *const classDef = classType->GetDeclNode()->AsClassDefinition(); + auto *const scope = checker->Scope()->AsClassScope(); + auto *const classProp = checker->ClassPropToImplementationProp( + interfaceProp->Clone(checker->Allocator(), originalProp->Parent()), scope); + classType->AddProperty(classProp->Key()->Variable()->AsLocalVariable()); + classDef->Body().push_back(classProp); + return classProp; + } + + auto *const classProp = classType + ->GetProperty(interfaceProp->Key()->AsIdentifier()->Name(), + PropertySearchFlags::SEARCH_ALL | PropertySearchFlags::SEARCH_IN_BASE) + ->Declaration() + ->Node() + ->AsClassProperty(); + classProp->AddModifier(ir::ModifierFlags::SUPER_OWNER); + return classProp; +} + void ETSChecker::GenerateGetterSetterPropertyAndMethod(ir::ClassProperty *originalProp, ETSObjectType *classType) { auto *const classDef = classType->GetDeclNode()->AsClassDefinition(); @@ -2196,10 +2225,7 @@ void ETSChecker::GenerateGetterSetterPropertyAndMethod(ir::ClassProperty *origin scope->InstanceFieldScope()->EraseBinding(interfaceProp->Key()->AsIdentifier()->Name()); interfaceProp->SetRange(originalProp->Range()); - auto *const classProp = - ClassPropToImplementationProp(interfaceProp->Clone(Allocator(), originalProp->Parent()), scope); - classType->AddProperty(classProp->Key()->Variable()->AsLocalVariable()); - classDef->Body().push_back(classProp); + auto *const classProp = GetImplementationClassProp(this, interfaceProp, originalProp, classType); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) ir::MethodDefinition *getter = GenerateDefaultGetterSetter(interfaceProp, classProp, scope, false, this); diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index 384573a26975c7e07e8e4b3eff6edc71ab77430c..ffd5440ed378c8b7428c5a1fbecef2dc91034558 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -764,6 +764,41 @@ void ETSChecker::ValidateOverriding(ETSObjectType *classType, const lexer::Sourc } } + if (functionOverridden) { + continue; + } + + auto superClassType = classType->SuperType(); + while (!functionOverridden && superClassType != nullptr) { + for (auto *field : superClassType->Fields()) { + if (field->Name() == (*it)->Name()) { + auto *newProp = + field->Declaration()->Node()->Clone(Allocator(), classType->GetDeclNode())->AsClassProperty(); + newProp->AddModifier(ir::ModifierFlags::SUPER_OWNER); + newProp->AddModifier(isGetter && isSetter ? ir::ModifierFlags::GETTER_SETTER + : isGetter ? ir::ModifierFlags::GETTER + : ir::ModifierFlags::SETTER); + auto *newFieldDecl = Allocator()->New(newProp->Key()->AsIdentifier()->Name()); + newFieldDecl->BindNode(newProp); + + auto newFieldVar = classType->GetDeclNode() + ->Scope() + ->AsClassScope() + ->InstanceFieldScope() + ->AddDecl(Allocator(), newFieldDecl, ScriptExtension::ETS) + ->AsLocalVariable(); + newFieldVar->AddFlag(varbinder::VariableFlags::PROPERTY); + newFieldVar->AddFlag(varbinder::VariableFlags::PUBLIC); + classType->AddProperty(newFieldVar); + it = abstractsToBeImplemented.erase(it); + functionOverridden = true; + break; + } + } + + superClassType = superClassType->SuperType(); + } + if (!functionOverridden) { it++; } @@ -771,9 +806,19 @@ void ETSChecker::ValidateOverriding(ETSObjectType *classType, const lexer::Sourc if (!abstractsToBeImplemented.empty() && throwError) { auto unimplementedSignature = abstractsToBeImplemented.front()->CallSignatures().front(); + auto containingObjectName = GetContainingObjectNameFromSignature(unimplementedSignature); + if (unimplementedSignature->HasSignatureFlag(SignatureFlags::GETTER)) { + ThrowTypeError({classType->Name(), " is not abstract and does not implement getter for ", + unimplementedSignature->Function()->Id()->Name(), " property in ", containingObjectName}, + pos); + } else if (unimplementedSignature->HasSignatureFlag(SignatureFlags::SETTER)) { + ThrowTypeError({classType->Name(), " is not abstract and does not implement setter for ", + unimplementedSignature->Function()->Id()->Name(), " property in ", containingObjectName}, + pos); + } ThrowTypeError({classType->Name(), " is not abstract and does not override abstract method ", unimplementedSignature->Function()->Id()->Name(), unimplementedSignature, " in ", - GetContainingObjectNameFromSignature(unimplementedSignature)}, + containingObjectName}, pos); } @@ -1530,15 +1575,12 @@ std::vector ETSChecker::ResolveMemberReference(const ir::Member varbinder::Variable *globalFunctionVar = nullptr; - if (memberExpr->Parent()->IsCallExpression()) { - if (memberExpr->Parent()->AsCallExpression()->Callee() == memberExpr) { - globalFunctionVar = ResolveInstanceExtension(memberExpr); - } + if (memberExpr->Parent()->IsCallExpression() && memberExpr->Parent()->AsCallExpression()->Callee() == memberExpr) { + globalFunctionVar = ResolveInstanceExtension(memberExpr); } else if (target->GetDeclNode() != nullptr && target->GetDeclNode()->IsClassDefinition() && !target->GetDeclNode()->AsClassDefinition()->IsClassDefinitionChecked()) { this->CheckClassDefinition(target->GetDeclNode()->AsClassDefinition()); } - const auto *const targetRef = GetTargetRef(memberExpr); auto searchFlag = GetSearchFlags(memberExpr, targetRef); @@ -1630,17 +1672,18 @@ void ETSChecker::CheckValidInheritance(ETSObjectType *classType, ir::ClassDefini auto *found = classType->SuperType()->GetProperty(it->Name(), searchFlag); ETSObjectType *interfaceFound = nullptr; - if (found == nullptr) { - auto interfaceList = GetInterfacesOfClass(classType); - for (auto *interface : interfaceList) { - auto *propertyFound = interface->GetProperty(it->Name(), searchFlag); - if (propertyFound == nullptr) { - continue; - } - found = propertyFound; - interfaceFound = interface; - break; + if (found != nullptr) { + CheckProperties(classType, classDef, it, found, interfaceFound); + } + auto interfaceList = GetInterfacesOfClass(classType); + for (auto *interface : interfaceList) { + auto *propertyFound = interface->GetProperty(it->Name(), searchFlag); + if (propertyFound == nullptr) { + continue; } + found = propertyFound; + interfaceFound = interface; + break; } if (found == nullptr) { continue; @@ -1653,11 +1696,26 @@ void ETSChecker::CheckValidInheritance(ETSObjectType *classType, ir::ClassDefini void ETSChecker::CheckProperties(ETSObjectType *classType, ir::ClassDefinition *classDef, varbinder::LocalVariable *it, varbinder::LocalVariable *found, ETSObjectType *interfaceFound) { + if (found->TsType() == nullptr) { + GetTypeOfVariable(found); + } + if (!IsSameDeclarationType(it, found)) { if (IsVariableStatic(it) != IsVariableStatic(found)) { return; } + if (it->TsType()->IsETSFunctionType()) { + auto getter = it->TsType()->AsETSFunctionType()->FindGetter(); + if (getter != nullptr && getter->ReturnType() == found->TsType()) { + return; + } + auto setter = it->TsType()->AsETSFunctionType()->FindSetter(); + if (setter != nullptr && setter->Params().front()->TsType() == found->TsType()) { + return; + } + } + const char *targetType {}; if (it->HasFlag(varbinder::VariableFlags::PROPERTY)) { diff --git a/ets2panda/ir/astNodeFlags.h b/ets2panda/ir/astNodeFlags.h index ffd34027a957eabbb6404145013bab429c7373cb..4378757bc015dcff85da92f4572a81f1f3641732 100644 --- a/ets2panda/ir/astNodeFlags.h +++ b/ets2panda/ir/astNodeFlags.h @@ -54,6 +54,7 @@ enum class ModifierFlags : uint32_t { DEFAULT_EXPORT = 1U << 23U, EXPORT_TYPE = 1U << 24U, EXTERNAL = 1U << 25U, + SUPER_OWNER = 1U << 26U, ACCESS = PUBLIC | PROTECTED | PRIVATE | INTERNAL, ALL = STATIC | ASYNC | ACCESS | DECLARE | READONLY | ABSTRACT, ALLOWED_IN_CTOR_PARAMETER = ACCESS | READONLY, diff --git a/ets2panda/test/parser/ets/interface_with_different_type-expected.txt b/ets2panda/test/parser/ets/interface_with_different_type-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..c961801813487d2efad72695247bee0e5e55487c --- /dev/null +++ b/ets2panda/test/parser/ets/interface_with_different_type-expected.txt @@ -0,0 +1,1273 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "declare": true, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": null, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 22, + "column": 9 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Abstract", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 24 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 19 + } + } + }, + "value": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 23, + "column": 31 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 30 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + "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": 23, + "column": 21 + }, + "end": { + "line": 23, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 21 + }, + "end": { + "line": 23, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 21 + }, + "end": { + "line": 23, + "column": 29 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + { + "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": 24, + "column": 2 + }, + "end": { + "line": 24, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 7 + }, + "end": { + "line": 26, + "column": 11 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Abstract", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 26, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 26, + "column": 30 + } + } + }, + "implements": [], + "body": [ + { + "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": 27, + "column": 2 + }, + "end": { + "line": 27, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 29 + }, + "end": { + "line": 27, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 27, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 7 + }, + "end": { + "line": 29, + "column": 14 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 23 + }, + "end": { + "line": 29, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 23 + }, + "end": { + "line": 29, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 23 + }, + "end": { + "line": 29, + "column": 38 + } + } + }, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 39 + }, + "end": { + "line": 29, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 39 + }, + "end": { + "line": 29, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 39 + }, + "end": { + "line": 29, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 39 + }, + "end": { + "line": 29, + "column": 50 + } + } + } + ], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 19 + } + } + }, + "value": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 30, + "column": 31 + }, + "end": { + "line": 30, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 30 + }, + "end": { + "line": 30, + "column": 32 + } + } + }, + "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": 30, + "column": 21 + }, + "end": { + "line": 30, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 21 + }, + "end": { + "line": 30, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 21 + }, + "end": { + "line": 30, + "column": 29 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 32 + } + } + }, + { + "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": 31, + "column": 2 + }, + "end": { + "line": 31, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 49 + }, + "end": { + "line": 31, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 1 + }, + "end": { + "line": 31, + "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": 33, + "column": 10 + }, + "end": { + "line": 33, + "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": 33, + "column": 10 + }, + "end": { + "line": 33, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "d", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 9 + }, + "end": { + "line": 34, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 34, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 17 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 34, + "column": 13 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 9 + }, + "end": { + "line": 35, + "column": 2 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 34, + "column": 5 + }, + "end": { + "line": 35, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 14 + }, + "end": { + "line": 35, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 1 + }, + "end": { + "line": 35, + "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": 36, + "column": 1 + } + } +} +TypeError: Cannot inherit from interface Interface because field instance_field is inherited with a different declaration type [interface_with_different_type.ets:16:1] diff --git a/ets2panda/test/parser/ets/interface_with_different_type.ets b/ets2panda/test/parser/ets/interface_with_different_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..e3aef72cb34df429a3e7f6d47c10e955e1cdcf9a --- /dev/null +++ b/ets2panda/test/parser/ets/interface_with_different_type.ets @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +interface Interface { + instance_field(): void { + return + } +} + +abstract class Abstract { + instance_field: number = -1 +} + +class Base extends Abstract { +} + +class Derived extends Base implements Interface { + instance_field: number = -1 +} + +function main() { + let d = new Derived +} diff --git a/ets2panda/test/parser/ets/missing_implementation_1-expected.txt b/ets2panda/test/parser/ets/missing_implementation_1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..9d2edc59a66b7ae11c86776009fc0f87affd65b3 --- /dev/null +++ b/ets2panda/test/parser/ets/missing_implementation_1-expected.txt @@ -0,0 +1,1113 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + } + ], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "instance_field_", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + "value": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 21, + "column": 38 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + "accessibility": "private", + "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": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "get", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 24, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 24, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 24, + "column": 33 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 25, + "column": 12 + }, + "end": { + "line": 25, + "column": 16 + } + } + }, + "property": { + "type": "Identifier", + "name": "instance_field_", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 17 + }, + "end": { + "line": 25, + "column": 32 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 12 + }, + "end": { + "line": 25, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 32 + }, + "end": { + "line": 26, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 26, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 26, + "column": 4 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 26, + "column": 4 + } + } + }, + { + "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": 28, + "column": 2 + }, + "end": { + "line": 28, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 36 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 28, + "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 + } + } + } + ], + "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": 29, + "column": 1 + } + } +} +TypeError: Derived is not abstract and does not implement setter for instance_field property in Interface [missing_implementation_1.ets:20:36] diff --git a/ets2panda/test/parser/ets/missing_implementation_1.ets b/ets2panda/test/parser/ets/missing_implementation_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..dcf28f2c0c5073b211c61eb0ebe47bd1c720ea69 --- /dev/null +++ b/ets2panda/test/parser/ets/missing_implementation_1.ets @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +interface Interface { + instance_field: number +} + +class Derived implements Interface { + private instance_field_: number = -1; + + + get instance_field(): number { + return this.instance_field_; + } + +} diff --git a/ets2panda/test/parser/ets/missing_implementation_2-expected.txt b/ets2panda/test/parser/ets/missing_implementation_2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..a7fe24d20931eb12190c00479d1a373573395a33 --- /dev/null +++ b/ets2panda/test/parser/ets/missing_implementation_2-expected.txt @@ -0,0 +1,1185 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Interface", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 37 + } + } + } + ], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "instance_field_", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 26 + } + } + }, + "value": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 21, + "column": 38 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + "accessibility": "private", + "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": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 39 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "set", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "instance_field", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 38 + }, + "end": { + "line": 24, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 38 + }, + "end": { + "line": 24, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 38 + }, + "end": { + "line": 24, + "column": 45 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 22 + }, + "end": { + "line": 24, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 22 + }, + "end": { + "line": 24, + "column": 45 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 9 + } + } + }, + "property": { + "type": "Identifier", + "name": "instance_field_", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 10 + }, + "end": { + "line": 25, + "column": 25 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 25 + } + } + }, + "right": { + "type": "Identifier", + "name": "instance_field", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 28 + }, + "end": { + "line": 25, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 43 + } + } + }, + { + "type": "ReturnStatement", + "argument": null, + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 46 + }, + "end": { + "line": 27, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 27, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 27, + "column": 4 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 21 + }, + "end": { + "line": 27, + "column": 4 + } + } + }, + { + "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": 29, + "column": 2 + }, + "end": { + "line": 29, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 36 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 29, + "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 + } + } + } + ], + "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": 30, + "column": 1 + } + } +} +TypeError: Derived is not abstract and does not implement getter for instance_field property in Interface [missing_implementation_2.ets:20:36] diff --git a/ets2panda/test/parser/ets/missing_implementation_2.ets b/ets2panda/test/parser/ets/missing_implementation_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..a76dbb3607c6bfab8557571f2715938a723f5fbb --- /dev/null +++ b/ets2panda/test/parser/ets/missing_implementation_2.ets @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +interface Interface { + instance_field: number +} + +class Derived implements Interface { + private instance_field_: number = -1; + + + set instance_field(instance_field: number) { + this.instance_field_ = instance_field; + return; + } + +} diff --git a/ets2panda/test/runtime/ets/multisource_inheritance-2.ets b/ets2panda/test/runtime/ets/multisource_inheritance-2.ets new file mode 100644 index 0000000000000000000000000000000000000000..38a8825924e1cfdc76823bc1811dea0c2f4d67a5 --- /dev/null +++ b/ets2panda/test/runtime/ets/multisource_inheritance-2.ets @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +interface Interface { + instance_field: number +} + +abstract class Abstract { + instance_field: number = -1 + +} + + +class Derived extends Abstract implements Interface { + +} + +function main() { + let d = new Derived + assert d.instance_field == -1 + + d.instance_field = 3 + assert d.instance_field == 3 +} + diff --git a/ets2panda/test/runtime/ets/multisource_inheritance.ets b/ets2panda/test/runtime/ets/multisource_inheritance.ets new file mode 100644 index 0000000000000000000000000000000000000000..d6c03ef44b729b4dd2a3091d25cbc19f8e596179 --- /dev/null +++ b/ets2panda/test/runtime/ets/multisource_inheritance.ets @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + interface Interface { + instance_field: number +} + +abstract class Abstract { + instance_field: number = -1 +} + +class Base extends Abstract { +} + +class Derived extends Base implements Interface { + instance_field: number = -1 + + public method(): number { + this.instance_field -= 1 + super.instance_field += 1 + return super.instance_field + } +} + +function main() { + let d = new Derived + assert d.method() == 0 + assert d.instance_field == -2 +}