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 #ifndef ARK_DYNAMICCALLINFO_H
17 #define ARK_DYNAMICCALLINFO_H
18 
19 #include <optional>
20 
21 #include "varbinder/ETSBinder.h"
22 #include "ir/expression.h"
23 
24 namespace ark::es2panda::checker {
25 
26 class DynamicCall {
27     using NameHolder = ArenaVector<util::StringView>;
28 
29 public:
30     struct Result {
31         const ir::AstNode *obj;
32         const NameHolder name;  // NOLINT(readability-identifier-naming)
33     };
34 
35     /**
36      * Resolve callee
37      * @param varbinder
38      * @param callee expression used to call method
39      * @return callee and name from which should be used to produce call
40      */
41     static Result ResolveCall(const varbinder::ETSBinder *varbinder, const ir::Expression *callee);
IsByValue(const varbinder::ETSBinder *varbinder, const ir::Expression *callee)42     static bool IsByValue(const varbinder::ETSBinder *varbinder, const ir::Expression *callee)
43     {
44         return ResolveCall(varbinder, callee).name.empty();
45     }
46 
47     /**
48      * Example: A[0].C.D => return: A[0], name: ".C.D"
49      * @param expr member expression
50      * @param name to store result
51      * @return object with remaining member expression
52      */
53     static Result SqueezeExpr(ArenaAllocator *allocator, const ir::MemberExpression *expr);
54 
55 private:
56     static const ir::Expression *SqueezeExpr(const ir::MemberExpression *expr, NameHolder &name);
57 };
58 
59 }  // namespace ark::es2panda::checker
60 #endif  // ARK_DYNAMICCALLINFO_H
61