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