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 ir::AstNode * node,const lexer::LineIndex & lineIndex)30 void Concurrent::SetConcurrent(ir::ScriptFunction *func, const ir::AstNode * node, const lexer::LineIndex &lineIndex)
31 {
32 // concurrent function should only be function declaration
33 if (!func->CanBeConcurrent()) {
34 ThrowInvalidConcurrentFunction(lineIndex, node, ConcurrentInvalidFlag::NOT_ORDINARY_FUNCTION);
35 }
36
37 func->AddFlag(ir::ScriptFunctionFlags::CONCURRENT);
38 }
39
ThrowInvalidConcurrentFunction(const lexer::LineIndex & lineIndex,const ir::AstNode * expr,ConcurrentInvalidFlag errFlag,util::StringView varName)40 void Concurrent::ThrowInvalidConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *expr,
41 ConcurrentInvalidFlag errFlag, util::StringView varName)
42 {
43 auto line = expr->Range().start.line;
44 auto column = (const_cast<lexer::LineIndex &>(lineIndex)).GetLocation(expr->Range().start).col - 1;
45 switch (errFlag) {
46 case ConcurrentInvalidFlag::NOT_ORDINARY_FUNCTION: {
47 throw Error {ErrorType::GENERIC, "Concurrent function should only be function declaration", line,
48 column};
49 break;
50 }
51 case ConcurrentInvalidFlag::NOT_IMPORT_VARIABLE: {
52 std::stringstream ss;
53 ss << "Concurrent function should only use import variable or local variable, '" << varName
54 << "' is not one of them";
55 throw Error {ErrorType::GENERIC, ss.str(), line, column};
56 break;
57 }
58 default:
59 break;
60 }
61 }
62
VerifyImportVarForConcurrentFunction(const lexer::LineIndex & lineIndex,const ir::AstNode * node,const binder::ScopeFindResult & result)63 void Concurrent::VerifyImportVarForConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *node,
64 const binder::ScopeFindResult &result)
65 {
66 if (!result.crossConcurrent) {
67 return;
68 }
69
70 if (result.variable->IsModuleVariable() && result.variable->Declaration()->IsImportDecl()) {
71 return;
72 }
73
74 ThrowInvalidConcurrentFunction(lineIndex, node, ConcurrentInvalidFlag::NOT_IMPORT_VARIABLE,
75 result.variable->Declaration()->Name());
76 }
77
78 } // namespace panda::es2panda::util