13af6ab5fSopenharmony_ci/** 23af6ab5fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#include "memberExpression.h" 173af6ab5fSopenharmony_ci 183af6ab5fSopenharmony_ci#include <compiler/core/pandagen.h> 193af6ab5fSopenharmony_ci#include <typescript/checker.h> 203af6ab5fSopenharmony_ci#include <ir/astDump.h> 213af6ab5fSopenharmony_ci#include <ir/base/classDefinition.h> 223af6ab5fSopenharmony_ci#include <ir/expressions/identifier.h> 233af6ab5fSopenharmony_ci#include <ir/expressions/privateIdentifier.h> 243af6ab5fSopenharmony_ci#include <ir/expressions/literals/numberLiteral.h> 253af6ab5fSopenharmony_ci#include <ir/expressions/literals/stringLiteral.h> 263af6ab5fSopenharmony_ci 273af6ab5fSopenharmony_cinamespace panda::es2panda::ir { 283af6ab5fSopenharmony_ci 293af6ab5fSopenharmony_civoid MemberExpression::Iterate(const NodeTraverser &cb) const 303af6ab5fSopenharmony_ci{ 313af6ab5fSopenharmony_ci cb(object_); 323af6ab5fSopenharmony_ci cb(property_); 333af6ab5fSopenharmony_ci} 343af6ab5fSopenharmony_ci 353af6ab5fSopenharmony_civoid MemberExpression::Dump(ir::AstDumper *dumper) const 363af6ab5fSopenharmony_ci{ 373af6ab5fSopenharmony_ci dumper->Add({{"type", "MemberExpression"}, 383af6ab5fSopenharmony_ci {"object", object_}, 393af6ab5fSopenharmony_ci {"property", property_}, 403af6ab5fSopenharmony_ci {"computed", computed_}, 413af6ab5fSopenharmony_ci {"optional", optional_}}); 423af6ab5fSopenharmony_ci} 433af6ab5fSopenharmony_ci 443af6ab5fSopenharmony_civoid MemberExpression::CompileObject(compiler::PandaGen *pg, compiler::VReg dest) const 453af6ab5fSopenharmony_ci{ 463af6ab5fSopenharmony_ci object_->Compile(pg); 473af6ab5fSopenharmony_ci pg->StoreAccumulator(this, dest); 483af6ab5fSopenharmony_ci pg->GetOptionalChain()->CheckNullish(optional_, dest); 493af6ab5fSopenharmony_ci} 503af6ab5fSopenharmony_ci 513af6ab5fSopenharmony_cicompiler::Operand MemberExpression::CompileKey(compiler::PandaGen *pg) const 523af6ab5fSopenharmony_ci{ 533af6ab5fSopenharmony_ci return pg->ToPropertyKey(property_, computed_); 543af6ab5fSopenharmony_ci} 553af6ab5fSopenharmony_ci 563af6ab5fSopenharmony_civoid MemberExpression::Compile(compiler::PandaGen *pg) const 573af6ab5fSopenharmony_ci{ 583af6ab5fSopenharmony_ci compiler::RegScope rs(pg); 593af6ab5fSopenharmony_ci compiler::VReg objReg = pg->AllocReg(); 603af6ab5fSopenharmony_ci Compile(pg, objReg); 613af6ab5fSopenharmony_ci} 623af6ab5fSopenharmony_ci 633af6ab5fSopenharmony_civoid MemberExpression::Compile(compiler::PandaGen *pg, compiler::VReg objReg) const 643af6ab5fSopenharmony_ci{ 653af6ab5fSopenharmony_ci CompileObject(pg, objReg); 663af6ab5fSopenharmony_ci if (AccessPrivateProperty()) { 673af6ab5fSopenharmony_ci auto name = property_->AsPrivateIdentifier()->Name(); 683af6ab5fSopenharmony_ci auto result = pg->Scope()->FindPrivateName(name); 693af6ab5fSopenharmony_ci if (!result.result.isMethod) { 703af6ab5fSopenharmony_ci pg->LoadAccumulator(this, objReg); 713af6ab5fSopenharmony_ci pg->LoadPrivateProperty(this, result.lexLevel, result.result.slot); 723af6ab5fSopenharmony_ci return; 733af6ab5fSopenharmony_ci } 743af6ab5fSopenharmony_ci if (result.result.isSetter) { 753af6ab5fSopenharmony_ci pg->ThrowTypeError(this, "Property is not defined with Getter"); 763af6ab5fSopenharmony_ci } 773af6ab5fSopenharmony_ci if (result.result.isStatic) { 783af6ab5fSopenharmony_ci pg->LoadLexicalVar(this, result.lexLevel, result.result.validateMethodSlot); 793af6ab5fSopenharmony_ci pg->Equal(this, objReg); 803af6ab5fSopenharmony_ci pg->ThrowTypeErrorIfFalse(this, "Object does not have private property"); 813af6ab5fSopenharmony_ci } else { 823af6ab5fSopenharmony_ci pg->LoadAccumulator(this, objReg); 833af6ab5fSopenharmony_ci pg->LoadPrivateProperty(this, result.lexLevel, result.result.validateMethodSlot); 843af6ab5fSopenharmony_ci } 853af6ab5fSopenharmony_ci 863af6ab5fSopenharmony_ci if (result.result.isGetter) { 873af6ab5fSopenharmony_ci pg->LoadAccumulator(this, objReg); 883af6ab5fSopenharmony_ci pg->LoadPrivateProperty(this, result.lexLevel, result.result.slot); 893af6ab5fSopenharmony_ci return; 903af6ab5fSopenharmony_ci } 913af6ab5fSopenharmony_ci pg->LoadLexicalVar(this, result.lexLevel, result.result.slot); 923af6ab5fSopenharmony_ci return; 933af6ab5fSopenharmony_ci } 943af6ab5fSopenharmony_ci compiler::Operand prop = CompileKey(pg); 953af6ab5fSopenharmony_ci 963af6ab5fSopenharmony_ci if (object_->IsSuperExpression()) { 973af6ab5fSopenharmony_ci pg->LoadSuperProperty(this, objReg, prop); 983af6ab5fSopenharmony_ci } else { 993af6ab5fSopenharmony_ci pg->LoadObjProperty(this, objReg, prop); 1003af6ab5fSopenharmony_ci } 1013af6ab5fSopenharmony_ci} 1023af6ab5fSopenharmony_ci 1033af6ab5fSopenharmony_cichecker::Type *MemberExpression::Check(checker::Checker *checker) const 1043af6ab5fSopenharmony_ci{ 1053af6ab5fSopenharmony_ci checker::Type *baseType = checker->CheckNonNullType(object_->Check(checker), object_->Start()); 1063af6ab5fSopenharmony_ci 1073af6ab5fSopenharmony_ci if (computed_) { 1083af6ab5fSopenharmony_ci checker::Type *indexType = property_->Check(checker); 1093af6ab5fSopenharmony_ci checker::Type *indexedAccessType = checker->GetPropertyTypeForIndexType(baseType, indexType); 1103af6ab5fSopenharmony_ci 1113af6ab5fSopenharmony_ci if (indexedAccessType) { 1123af6ab5fSopenharmony_ci return indexedAccessType; 1133af6ab5fSopenharmony_ci } 1143af6ab5fSopenharmony_ci 1153af6ab5fSopenharmony_ci if (!indexType->HasTypeFlag(checker::TypeFlag::STRING_LIKE | checker::TypeFlag::NUMBER_LIKE)) { 1163af6ab5fSopenharmony_ci checker->ThrowTypeError({"Type ", indexType, " cannot be used as index type"}, property_->Start()); 1173af6ab5fSopenharmony_ci } 1183af6ab5fSopenharmony_ci 1193af6ab5fSopenharmony_ci if (indexType->IsNumberType()) { 1203af6ab5fSopenharmony_ci checker->ThrowTypeError("No index signature with a parameter of type 'string' was found on type this type", 1213af6ab5fSopenharmony_ci Start()); 1223af6ab5fSopenharmony_ci } 1233af6ab5fSopenharmony_ci 1243af6ab5fSopenharmony_ci if (indexType->IsStringType()) { 1253af6ab5fSopenharmony_ci checker->ThrowTypeError("No index signature with a parameter of type 'number' was found on type this type", 1263af6ab5fSopenharmony_ci Start()); 1273af6ab5fSopenharmony_ci } 1283af6ab5fSopenharmony_ci 1293af6ab5fSopenharmony_ci switch (property_->Type()) { 1303af6ab5fSopenharmony_ci case ir::AstNodeType::IDENTIFIER: { 1313af6ab5fSopenharmony_ci checker->ThrowTypeError( 1323af6ab5fSopenharmony_ci {"Property ", property_->AsIdentifier()->Name(), " does not exist on this type."}, 1333af6ab5fSopenharmony_ci property_->Start()); 1343af6ab5fSopenharmony_ci } 1353af6ab5fSopenharmony_ci case ir::AstNodeType::NUMBER_LITERAL: { 1363af6ab5fSopenharmony_ci checker->ThrowTypeError( 1373af6ab5fSopenharmony_ci {"Property ", property_->AsNumberLiteral()->Str(), " does not exist on this type."}, 1383af6ab5fSopenharmony_ci property_->Start()); 1393af6ab5fSopenharmony_ci } 1403af6ab5fSopenharmony_ci case ir::AstNodeType::STRING_LITERAL: { 1413af6ab5fSopenharmony_ci checker->ThrowTypeError( 1423af6ab5fSopenharmony_ci {"Property ", property_->AsStringLiteral()->Str(), " does not exist on this type."}, 1433af6ab5fSopenharmony_ci property_->Start()); 1443af6ab5fSopenharmony_ci } 1453af6ab5fSopenharmony_ci default: { 1463af6ab5fSopenharmony_ci UNREACHABLE(); 1473af6ab5fSopenharmony_ci } 1483af6ab5fSopenharmony_ci } 1493af6ab5fSopenharmony_ci } 1503af6ab5fSopenharmony_ci 1513af6ab5fSopenharmony_ci binder::Variable *prop = checker->GetPropertyOfType(baseType, property_->AsIdentifier()->Name()); 1523af6ab5fSopenharmony_ci 1533af6ab5fSopenharmony_ci if (prop) { 1543af6ab5fSopenharmony_ci checker::Type *propType = checker->GetTypeOfVariable(prop); 1553af6ab5fSopenharmony_ci if (prop->HasFlag(binder::VariableFlags::READONLY)) { 1563af6ab5fSopenharmony_ci propType->AddTypeFlag(checker::TypeFlag::READONLY); 1573af6ab5fSopenharmony_ci } 1583af6ab5fSopenharmony_ci 1593af6ab5fSopenharmony_ci return propType; 1603af6ab5fSopenharmony_ci } 1613af6ab5fSopenharmony_ci 1623af6ab5fSopenharmony_ci if (baseType->IsObjectType()) { 1633af6ab5fSopenharmony_ci checker::ObjectType *objType = baseType->AsObjectType(); 1643af6ab5fSopenharmony_ci 1653af6ab5fSopenharmony_ci if (objType->StringIndexInfo()) { 1663af6ab5fSopenharmony_ci checker::Type *indexType = objType->StringIndexInfo()->GetType(); 1673af6ab5fSopenharmony_ci if (objType->StringIndexInfo()->Readonly()) { 1683af6ab5fSopenharmony_ci indexType->AddTypeFlag(checker::TypeFlag::READONLY); 1693af6ab5fSopenharmony_ci } 1703af6ab5fSopenharmony_ci 1713af6ab5fSopenharmony_ci return indexType; 1723af6ab5fSopenharmony_ci } 1733af6ab5fSopenharmony_ci } 1743af6ab5fSopenharmony_ci 1753af6ab5fSopenharmony_ci checker->ThrowTypeError({"Property ", property_->AsIdentifier()->Name(), " does not exist on this type."}, 1763af6ab5fSopenharmony_ci property_->Start()); 1773af6ab5fSopenharmony_ci return nullptr; 1783af6ab5fSopenharmony_ci} 1793af6ab5fSopenharmony_ci 1803af6ab5fSopenharmony_civoid MemberExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) 1813af6ab5fSopenharmony_ci{ 1823af6ab5fSopenharmony_ci object_ = std::get<ir::AstNode *>(cb(object_))->AsExpression(); 1833af6ab5fSopenharmony_ci property_ = std::get<ir::AstNode *>(cb(property_))->AsExpression(); 1843af6ab5fSopenharmony_ci} 1853af6ab5fSopenharmony_ci 1863af6ab5fSopenharmony_ci} // namespace panda::es2panda::ir 187