• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 ES2PANDA_PARSER_CORE_ETS_PARSER_TEMPLATES_H
17 #define ES2PANDA_PARSER_CORE_ETS_PARSER_TEMPLATES_H
18 
19 template <typename T>
SetFormattingFileName(T && fileName)20 void SetFormattingFileName(T &&fileName)
21 {
22     GetContext().SetFormattingFileName(std::forward<T>(fileName));
23 }
24 
25 template <typename T>
ProcessFormattedArg(std::vector<ir::AstNode * > & nodes,T && arg)26 void ProcessFormattedArg(std::vector<ir::AstNode *> &nodes, T &&arg)
27 {
28     if constexpr (std::is_convertible_v<std::decay_t<T>, ir::AstNode *>) {
29         nodes.emplace_back(std::forward<T>(arg));
30     } else if constexpr (std::is_same_v<std::decay_t<T>, util::StringView>) {
31         nodes.emplace_back(AllocNode<ir::Identifier>(std::forward<T>(arg), Allocator()));
32     } else if constexpr (std::is_same_v<std::decay_t<T>, util::UString>) {
33         nodes.emplace_back(AllocNode<ir::Identifier>(arg.View(), Allocator()));
34     } else if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
35         nodes.emplace_back(
36             AllocNode<ir::Identifier>(util::UString(std::forward<T>(arg), Allocator()).View(), Allocator()));
37     } else if constexpr (std::is_constructible_v<std::string, std::decay_t<T>>) {
38         nodes.emplace_back(AllocNode<ir::Identifier>(
39             util::UString(std::string {std::forward<T>(arg)}, Allocator()).View(), Allocator()));
40     } else if constexpr (std::is_convertible_v<std::decay_t<T>, checker::Type *>) {
41         nodes.emplace_back(AllocNode<ir::OpaqueTypeNode>(std::forward<T>(arg), Allocator()));
42     } else if constexpr (std::is_same_v<std::decay_t<T>, ArenaVector<ir::AstNode *>>) {
43         nodes.emplace_back(AllocNode<ir::TSInterfaceBody>(std::forward<T>(arg)));
44     } else if constexpr (std::is_same_v<std::decay_t<T>, ArenaVector<ir::Expression *>>) {
45         nodes.emplace_back(AllocNode<ir::SequenceExpression>(std::forward<T>(arg)));
46     } else if constexpr (std::is_same_v<std::decay_t<T>, ArenaVector<ir::Statement *>>) {
47         nodes.emplace_back(AllocNode<ir::BlockExpression>(std::forward<T>(arg)));
48     } else if constexpr (std::is_same_v<std::decay_t<T>, ArenaVector<checker::Type *>>) {
49         for (auto *type : arg) {
50             nodes.emplace_back(AllocNode<ir::OpaqueTypeNode>(type, Allocator()));
51         }
52     } else {
53         static_assert(STATIC_FALSE<T>, "Format argument has invalid type.");
54     }
55 }
56 
57 template <typename... Args>
CreateFormattedExpression(std::string_view const sourceCode,Args &&...args)58 ir::Expression *CreateFormattedExpression(std::string_view const sourceCode, Args &&...args)
59 {
60     std::vector<ir::AstNode *> insertingNodes {};
61     insertingNodes.reserve(sizeof...(Args));
62     (ProcessFormattedArg(insertingNodes, std::forward<Args>(args)), ...);
63     return CreateFormattedExpression(sourceCode, insertingNodes);
64 }
65 
66 template <typename... Args>
CreateFormattedStatements(std::string_view const sourceCode,Args &&...args)67 ArenaVector<ir::Statement *> CreateFormattedStatements(std::string_view const sourceCode, Args &&...args)
68 {
69     std::vector<ir::AstNode *> insertingNodes {};
70     insertingNodes.reserve(sizeof...(Args));
71     (ProcessFormattedArg(insertingNodes, std::forward<Args>(args)), ...);
72     return CreateFormattedStatements(sourceCode, insertingNodes);
73 }
74 
75 template <typename... Args>
CreateFormattedClassDeclaration(std::string_view sourceCode,bool allowStatic,Args &&...args)76 ir::Statement *CreateFormattedClassDeclaration(std::string_view sourceCode, bool allowStatic, Args &&...args)
77 {
78     std::vector<ir::AstNode *> insertingNodes {};
79     insertingNodes.reserve(sizeof...(Args));
80     (ProcessFormattedArg(insertingNodes, std::forward<Args>(args)), ...);
81     return CreateFormattedClassDeclaration(sourceCode, insertingNodes, allowStatic);
82 }
83 
84 template <typename... Args>
CreateFormattedClassElement(std::string_view sourceCode,ir::ClassDefinition * classDefinition,Args &&...args)85 ir::AstNode *CreateFormattedClassElement(std::string_view sourceCode, ir::ClassDefinition *classDefinition,
86                                          Args &&...args)
87 {
88     return CreateFormattedClassElement(sourceCode, classDefinition->Body(), classDefinition->Modifiers(),
89                                        std::forward<Args...>(args...));
90 }
91 
92 template <typename... Args>
CreateFormattedClassFieldDefinition(std::string_view const sourceCode,Args &&...args)93 ir::AstNode *CreateFormattedClassFieldDefinition(std::string_view const sourceCode, Args &&...args)
94 {
95     std::vector<ir::AstNode *> insertingNodes {};
96     insertingNodes.reserve(sizeof...(Args));
97     (ProcessFormattedArg(insertingNodes, std::forward<Args>(args)), ...);
98     return CreateFormattedClassFieldDefinition(sourceCode, insertingNodes);
99 }
100 
101 template <typename... Args>
CreateFormattedClassElement(std::string_view sourceCode,const ArenaVector<ir::AstNode * > & properties,ir::ClassDefinitionModifiers modifiers,Args &&...args)102 ir::AstNode *CreateFormattedClassElement(std::string_view sourceCode, const ArenaVector<ir::AstNode *> &properties,
103                                          ir::ClassDefinitionModifiers modifiers, Args &&...args)
104 {
105     std::vector<ir::AstNode *> insertingNodes {};
106     insertingNodes.reserve(sizeof...(Args));
107     (ProcessFormattedArg(insertingNodes, std::forward<Args>(args)), ...);
108     return CreateFormattedClassElement(sourceCode, insertingNodes, properties, modifiers);
109 }
110 
111 template <typename... Args>
CreateFormattedClassMethodDefinition(std::string_view const sourceCode,Args &&...args)112 ir::AstNode *CreateFormattedClassMethodDefinition(std::string_view const sourceCode, Args &&...args)
113 {
114     std::vector<ir::AstNode *> insertingNodes {};
115     insertingNodes.reserve(sizeof...(Args));
116     (ProcessFormattedArg(insertingNodes, std::forward<Args>(args)), ...);
117     return CreateFormattedClassMethodDefinition(sourceCode, insertingNodes);
118 }
119 
120 template <typename... Args>
CreateFormattedTopLevelStatement(std::string_view sourceCode,Args &&...args)121 ir::Statement *CreateFormattedTopLevelStatement(std::string_view sourceCode, Args &&...args)
122 {
123     std::vector<ir::AstNode *> insertingNodes {};
124     insertingNodes.reserve(sizeof...(Args));
125     (ProcessFormattedArg(insertingNodes, std::forward<Args>(args)), ...);
126     return CreateFormattedTopLevelStatement(sourceCode, insertingNodes);
127 }
128 
129 template <typename... Args>
CreateFormattedTypeAnnotation(std::string_view const sourceCode,Args &&...args)130 ir::TypeNode *CreateFormattedTypeAnnotation(std::string_view const sourceCode, Args &&...args)
131 {
132     std::vector<ir::AstNode *> insertingNodes {};
133     insertingNodes.reserve(sizeof...(Args));
134     (ProcessFormattedArg(insertingNodes, std::forward<Args>(args)), ...);
135     return CreateFormattedTypeAnnotation(sourceCode, insertingNodes);
136 }
137 
138 template <typename... Args>
CreateFormattedStatement(std::string_view const sourceCode,Args &&...args)139 ir::Statement *CreateFormattedStatement(std::string_view const sourceCode, Args &&...args)
140 {
141     std::vector<ir::AstNode *> insertingNodes {};
142     insertingNodes.reserve(sizeof...(Args));
143     (ProcessFormattedArg(insertingNodes, std::forward<Args>(args)), ...);
144     return CreateFormattedStatement(sourceCode, insertingNodes);
145 }
146 
147 #endif
148