• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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 "checker.h"
17 
18 #include <ir/expression.h>
19 #include <ir/statements/blockStatement.h>
20 #include <parser/program/program.h>
21 #include <util/helpers.h>
22 #include <binder/binder.h>
23 #include <binder/scope.h>
24 #include <binder/variable.h>
25 #include <es2panda.h>
26 
27 #include <cstdint>
28 #include <initializer_list>
29 #include <memory>
30 
31 namespace panda::es2panda::checker {
32 
Checker(ArenaAllocator * allocator,binder::Binder * binder)33 Checker::Checker(ArenaAllocator *allocator, binder::Binder *binder)
34     : allocator_(allocator),
35       binder_(binder),
36       rootNode_(binder->TopScope()->Node()->AsBlockStatement()),
37       scope_(binder->TopScope()),
38       context_(CheckerStatus::NO_OPTS)
39 {
40     scopeStack_.push_back(scope_);
41     globalTypes_ = allocator_->New<GlobalTypesHolder>(allocator_);
42     relation_ = allocator_->New<TypeRelation>(this);
43 }
44 
StartChecker()45 void Checker::StartChecker()
46 {
47     ASSERT(rootNode_->IsProgram());
48     rootNode_->Check(this);
49 }
50 
ThrowTypeError(std::initializer_list<TypeErrorMessageElement> list,const lexer::SourcePosition & pos)51 void Checker::ThrowTypeError(std::initializer_list<TypeErrorMessageElement> list, const lexer::SourcePosition &pos)
52 {
53     std::stringstream ss;
54 
55     for (const auto &it : list) {
56         if (std::holds_alternative<char *>(it)) {
57             ss << std::get<char *>(it);
58         } else if (std::holds_alternative<util::StringView>(it)) {
59             ss << std::get<util::StringView>(it);
60         } else if (std::holds_alternative<lexer::TokenType>(it)) {
61             ss << TokenToString(std::get<lexer::TokenType>(it));
62         } else if (std::holds_alternative<const Type *>(it)) {
63             std::get<const Type *>(it)->ToString(ss);
64         } else if (std::holds_alternative<AsSrc>(it)) {
65             std::get<AsSrc>(it).GetType()->ToStringAsSrc(ss);
66         } else if (std::holds_alternative<size_t>(it)) {
67             ss << std::to_string(std::get<size_t>(it));
68         } else {
69             UNREACHABLE();
70         }
71     }
72 
73     std::string err = ss.str();
74     ThrowTypeError(err, pos);
75 }
76 
ThrowTypeError(std::string_view message,const lexer::SourcePosition & pos)77 void Checker::ThrowTypeError(std::string_view message, const lexer::SourcePosition &pos)
78 {
79     lexer::LineIndex index(binder_->Program()->SourceCode());
80     lexer::SourceLocation loc = index.GetLocation(pos);
81 
82     throw Error {ErrorType::TYPE, message, loc.line, loc.col};
83 }
84 
CheckTypeCached(const ir::Expression * expr)85 Type *Checker::CheckTypeCached(const ir::Expression *expr)
86 {
87     auto res = nodeCache_.find(expr);
88     if (res != nodeCache_.end()) {
89         return res->second;
90     }
91 
92     Type *returnType = expr->Check(this);
93     nodeCache_.insert({expr, returnType});
94 
95     return returnType;
96 }
97 
98 }  // namespace panda::es2panda::checker
99