1 /*
2 * Copyright (c) 2022 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 "concurrent.h"
17
18 #include <binder/scope.h>
19 #include <compiler/core/compilerContext.h>
20 #include <compiler/core/pandagen.h>
21 #include <ir/astNode.h>
22 #include <ir/base/scriptFunction.h>
23 #include <ir/expressions/literals/stringLiteral.h>
24 #include <ir/statements/blockStatement.h>
25 #include <ir/statements/expressionStatement.h>
26 #include <lexer/token/sourceLocation.h>
27
28 namespace panda::es2panda::util {
29
SetConcurrent(ir::ScriptFunction * func,const lexer::LineIndex & lineIndex)30 void Concurrent::SetConcurrent(ir::ScriptFunction *func, const lexer::LineIndex &lineIndex)
31 {
32 auto *body = func->Body();
33 if (!body) {
34 return;
35 }
36
37 if (body->IsExpression()) {
38 return;
39 }
40
41 auto &statements = body->AsBlockStatement()->Statements();
42 if (statements.empty()) {
43 return;
44 }
45
46 /**
47 * verify the firstStmt
48 * if IsExpressionStatement(firstStmt) == false
49 * return;
50 * else
51 * if (firstStmt->IsStringLiteral() && strVal == "use concurrent")
52 * [fall through]
53 * else
54 * return
55 */
56 const auto *stmt = statements.front();
57 if (!stmt->IsExpressionStatement()) {
58 return;
59 }
60
61 auto *expr = stmt->AsExpressionStatement()->GetExpression();
62 if (!expr->IsStringLiteral()) {
63 return;
64 }
65
66 if (!expr->AsStringLiteral()->Str().Is(USE_CONCURRENT)) {
67 return;
68 }
69
70 // concurrent function should only be function declaration
71 if (!func->CanBeConcurrent()) {
72 ThrowInvalidConcurrentFunction(lineIndex, stmt, ConcurrentInvalidFlag::NOT_ORDINARY_FUNCTION);
73 }
74
75 func->AddFlag(ir::ScriptFunctionFlags::CONCURRENT);
76 }
77
ThrowInvalidConcurrentFunction(const lexer::LineIndex & lineIndex,const ir::AstNode * expr,ConcurrentInvalidFlag errFlag)78 void Concurrent::ThrowInvalidConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *expr,
79 ConcurrentInvalidFlag errFlag)
80 {
81 auto line = expr->Range().start.line;
82 auto column = (const_cast<lexer::LineIndex &>(lineIndex)).GetLocation(expr->Range().start).col - 1;
83 switch (errFlag) {
84 case ConcurrentInvalidFlag::NOT_ORDINARY_FUNCTION: {
85 throw Error {ErrorType::GENERIC, "Concurrent function should only be function declaration", line,
86 column};
87 break;
88 }
89 case ConcurrentInvalidFlag::NOT_IMPORT_VARIABLE: {
90 throw Error {ErrorType::GENERIC, "Concurrent function should only use import variable or local variable",
91 line, column};
92 break;
93 }
94 default:
95 break;
96 }
97 }
98
VerifyImportVarForConcurrentFunction(const lexer::LineIndex & lineIndex,const ir::AstNode * node,const binder::ScopeFindResult & result)99 void Concurrent::VerifyImportVarForConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *node,
100 const binder::ScopeFindResult &result)
101 {
102 if (!result.crossConcurrent) {
103 return;
104 }
105
106 if (result.variable->IsModuleVariable() && result.variable->Declaration()->IsImportDecl()) {
107 return;
108 }
109
110 ThrowInvalidConcurrentFunction(lineIndex, node, ConcurrentInvalidFlag::NOT_IMPORT_VARIABLE);
111 }
112
113 } // namespace panda::es2panda::util