• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ir/base/scriptFunction.h>
19 
20 namespace panda::es2panda::util {
21 
SetConcurrent(ir::ScriptFunction * func,const ir::AstNode * node,const lexer::LineIndex & lineIndex)22 void Concurrent::SetConcurrent(ir::ScriptFunction *func, const ir::AstNode * node, const lexer::LineIndex &lineIndex)
23 {
24     // concurrent function should only be function declaration
25     if (!func->CanBeConcurrent()) {
26         ThrowInvalidConcurrentFunction(lineIndex, node, ConcurrentInvalidFlag::NOT_ORDINARY_FUNCTION);
27     }
28 
29     func->AddFlag(ir::ScriptFunctionFlags::CONCURRENT);
30 }
31 
ThrowInvalidConcurrentFunction(const lexer::LineIndex & lineIndex,const ir::AstNode * expr,ConcurrentInvalidFlag errFlag,util::StringView varName)32 void Concurrent::ThrowInvalidConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *expr,
33                                                 ConcurrentInvalidFlag errFlag, util::StringView varName)
34 {
35     auto line = expr->Range().start.line;
36     auto column = (const_cast<lexer::LineIndex &>(lineIndex)).GetLocation(expr->Range().start).col - 1;
37     switch (errFlag) {
38         case ConcurrentInvalidFlag::NOT_ORDINARY_FUNCTION: {
39             throw Error {ErrorType::SYNTAX, "Concurrent function should only be function declaration", line,
40                          column};
41             break;
42         }
43         case ConcurrentInvalidFlag::NOT_IMPORT_VARIABLE: {
44             std::stringstream ss;
45             ss << "Concurrent function should only use import variable or local variable, '" << varName
46                << "' is not one of them";
47             throw Error {ErrorType::SYNTAX, ss.str(), line, column};
48             break;
49         }
50         default:
51             break;
52     }
53 }
54 
ProcessConcurrent(const lexer::LineIndex & lineIndex,const ir::AstNode * node,const binder::ScopeFindResult & result,parser::Program * program)55 void Concurrent::ProcessConcurrent(const lexer::LineIndex &lineIndex, const ir::AstNode *node,
56                                    const binder::ScopeFindResult &result, parser::Program *program)
57 {
58     if (!result.concurrentFunc) {
59         return;
60     }
61 
62     if (result.variable->IsModuleVariable() && result.variable->Declaration()->IsImportDecl()) {
63         CollectRelativeModule(result, program);
64         return;
65     }
66 
67     ThrowInvalidConcurrentFunction(lineIndex, node, ConcurrentInvalidFlag::NOT_IMPORT_VARIABLE,
68                                    result.variable->Declaration()->Name());
69 }
70 
CollectRelativeModule(const binder::ScopeFindResult & result,parser::Program * program)71 void Concurrent::CollectRelativeModule(const binder::ScopeFindResult &result, parser::Program *program)
72 {
73     int moduleRequestIdx = program->ModuleRecord()->GetModuleRequestIdx(result.variable->Name());
74     if (moduleRequestIdx != parser::SourceTextModuleRecord::INVALID_MODULEREQUEST_ID) {
75         result.concurrentFunc->AddConcurrentModuleRequest(moduleRequestIdx);
76     }
77 }
78 
79 } // namespace panda::es2panda::util