1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "enumPostCheckLowering.h"
17 #include "checker/types/ets/etsEnumType.h"
18 #include "checker/ETSchecker.h"
19 #include "checker/types/type.h"
20 #include "compiler/lowering/util.h"
21 #include "varbinder/ETSBinder.h"
22 #include "varbinder/variable.h"
23
24 namespace ark::es2panda::compiler {
25
FindEnclosingClass(ir::AstNode * ast)26 static ir::ClassDeclaration *FindEnclosingClass(ir::AstNode *ast)
27 {
28 for (ir::AstNode *curr = ast->Parent(); curr != nullptr; curr = curr->Parent()) {
29 if (curr->IsClassDeclaration()) {
30 return curr->AsClassDeclaration();
31 }
32 }
33 UNREACHABLE();
34 }
35
CallStaticEnumMethod(checker::ETSChecker * checker,checker::ETSEnumType * enumType,checker::ETSEnumType::Method (checker::ETSEnumType::* getMethod)()const,ir::Expression * argument)36 static ir::CallExpression *CallStaticEnumMethod(checker::ETSChecker *checker, checker::ETSEnumType *enumType,
37 checker::ETSEnumType::Method (checker::ETSEnumType::*getMethod)() const,
38 ir::Expression *argument)
39 {
40 auto classDef = enumType->BoxedType()->AsETSObjectType()->GetDeclNode()->AsClassDefinition();
41 auto methodName = (enumType->*getMethod)().memberProxyType->Name();
42
43 auto classId = checker->AllocNode<ir::Identifier>(classDef->Ident()->Name(), checker->Allocator());
44 auto methodId = checker->AllocNode<ir::Identifier>(methodName, checker->Allocator());
45 auto callee = checker->AllocNode<ir::MemberExpression>(classId, methodId, ir::MemberExpressionKind::PROPERTY_ACCESS,
46 false, false);
47
48 ArenaVector<ir::Expression *> callArguments({argument}, checker->Allocator()->Adapter());
49 return checker->AllocNode<ir::CallExpression>(callee, std::move(callArguments), nullptr, false);
50 }
51
CallInstanceEnumMethod(checker::ETSChecker * checker,checker::ETSEnumType * enumType,checker::ETSEnumType::Method (checker::ETSEnumType::* getMethod)()const,ir::Expression * thisArg)52 static ir::CallExpression *CallInstanceEnumMethod(checker::ETSChecker *checker, checker::ETSEnumType *enumType,
53 checker::ETSEnumType::Method (checker::ETSEnumType::*getMethod)()
54 const,
55 ir::Expression *thisArg)
56 {
57 auto methodName = (enumType->*getMethod)().memberProxyType->Name();
58
59 auto methodId = checker->AllocNode<ir::Identifier>(methodName, checker->Allocator());
60 auto callee = checker->AllocNode<ir::MemberExpression>(thisArg, methodId, ir::MemberExpressionKind::PROPERTY_ACCESS,
61 false, false);
62
63 ArenaVector<ir::Expression *> callArguments({}, checker->Allocator()->Adapter());
64 return checker->AllocNode<ir::CallExpression>(callee, std::move(callArguments), nullptr, false);
65 }
66
GenerateValueOfCall(checker::ETSChecker * checker,ir::AstNode * const node)67 static ir::CallExpression *GenerateValueOfCall(checker::ETSChecker *checker, ir::AstNode *const node)
68 {
69 ASSERT(node->IsExpression());
70 auto expr = node->AsExpression();
71 auto parent = expr->Parent();
72 parent->AddAstNodeFlags(ir::AstNodeFlags::RECHECK);
73
74 checker::ETSEnumType *enumIf;
75
76 if (!expr->TsType()->IsETSEnumType()) {
77 expr->RemoveBoxingUnboxingFlags(ir::BoxingUnboxingFlags::UNBOX_TO_ENUM);
78 enumIf = expr->TsType()->AsETSObjectType()->GetUnboxedEnumType();
79 expr = CallInstanceEnumMethod(checker, enumIf, &checker::ETSEnumType::UnboxMethod, expr);
80 } else {
81 enumIf = expr->TsType()->AsETSEnumType();
82 }
83
84 auto *callExpr = CallStaticEnumMethod(checker, enumIf, &checker::ETSEnumType::ValueOfMethod, expr);
85 callExpr->SetParent(parent);
86
87 auto *calleClass = FindEnclosingClass(expr);
88
89 auto *varBinder = checker->VarBinder()->AsETSBinder();
90
91 auto *nearestScope = NearestScope(parent);
92 auto lexScope = varbinder::LexicalScope<varbinder::Scope>::Enter(varBinder, nearestScope);
93 varBinder->ResolveReferencesForScopeWithContext(callExpr, nearestScope);
94
95 auto checkerCtx = checker::SavedCheckerContext(checker, checker::CheckerStatus::IN_CLASS,
96 calleClass->Definition()->TsType()->AsETSObjectType());
97 auto scopeCtx = checker::ScopeContext(checker, nearestScope);
98
99 callExpr->Check(checker);
100 node->RemoveAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF);
101 return callExpr;
102 }
103
Perform(public_lib::Context * ctx,parser::Program * program)104 bool EnumPostCheckLoweringPhase::Perform(public_lib::Context *ctx, parser::Program *program)
105 {
106 if (program->Extension() != ScriptExtension::ETS) {
107 return true;
108 }
109
110 for (auto &[_, extPrograms] : program->ExternalSources()) {
111 (void)_;
112 for (auto *extProg : extPrograms) {
113 Perform(ctx, extProg);
114 }
115 }
116 program->Ast()->TransformChildrenRecursivelyPostorder(
117 // clang-format off
118 [ctx](ir::AstNode *const node) -> ir::AstNode* {
119 if (node->HasAstNodeFlags(ir::AstNodeFlags::RECHECK)) {
120 if (node->IsExpression()) {
121 node->AsExpression()->SetTsType(nullptr); // force recheck
122 }
123 node->Check(ctx->checker->AsETSChecker());
124 node->RemoveAstNodeFlags(ir::AstNodeFlags::RECHECK);
125 }
126 if (node->HasAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF)) {
127 return GenerateValueOfCall(ctx->checker->AsETSChecker(), node);
128 }
129 if (node->HasAstNodeFlags(ir::AstNodeFlags::GENERATE_GET_NAME)) {
130 ASSERT(node->IsMemberExpression());
131 auto memberExpr = node->AsMemberExpression();
132
133 auto *enumIf = memberExpr->Object()->TsType()->AsETSEnumType();
134 auto *callExpr = CallStaticEnumMethod(ctx->checker->AsETSChecker(), enumIf,
135 // CC-OFFNXT(G.FMT.06-CPP) project code style
136 &checker::ETSEnumType::GetNameMethod, memberExpr->Property());
137
138 callExpr->SetParent(node->Parent());
139 callExpr->Check(ctx->checker->AsETSChecker());
140 node->RemoveAstNodeFlags(ir::AstNodeFlags::GENERATE_GET_NAME);
141 return callExpr;
142 }
143 return node;
144 },
145 // clang-format on
146 Name());
147 return true;
148 }
149
150 } // namespace ark::es2panda::compiler
151