• 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 "tsAsExpression.h"
17 
18 #include <typescript/checker.h>
19 #include <ir/expressions/identifier.h>
20 #include <ir/expressions/literal.h>
21 #include <ir/expressions/memberExpression.h>
22 #include <ir/expressions/unaryExpression.h>
23 #include <ir/typeNode.h>
24 
25 namespace panda::es2panda::ir {
26 
Iterate(const NodeTraverser & cb) const27 void TSAsExpression::Iterate(const NodeTraverser &cb) const
28 {
29     cb(expression_);
30     cb(typeAnnotation_);
31 }
32 
Dump(ir::AstDumper * dumper) const33 void TSAsExpression::Dump(ir::AstDumper *dumper) const
34 {
35     dumper->Add({{"type", "TSAsExpression"}, {"expression", expression_}, {"typeAnnotation", typeAnnotation_}});
36 }
37 
Compile(compiler::PandaGen * pg) const38 void TSAsExpression::Compile(compiler::PandaGen *pg) const
39 {
40     expression_->Compile(pg);
41 }
42 
IsValidConstAssertionArgument(checker::Checker * checker,const ir::AstNode * arg)43 static bool IsValidConstAssertionArgument(checker::Checker *checker, const ir::AstNode *arg)
44 {
45     switch (arg->Type()) {
46         case ir::AstNodeType::NUMBER_LITERAL:
47         case ir::AstNodeType::STRING_LITERAL:
48         case ir::AstNodeType::BIGINT_LITERAL:
49         case ir::AstNodeType::BOOLEAN_LITERAL:
50         case ir::AstNodeType::ARRAY_EXPRESSION:
51         case ir::AstNodeType::OBJECT_EXPRESSION:
52         case ir::AstNodeType::TEMPLATE_LITERAL: {
53             return true;
54         }
55         case ir::AstNodeType::UNARY_EXPRESSION: {
56             const ir::UnaryExpression *unaryExpr = arg->AsUnaryExpression();
57             lexer::TokenType op = unaryExpr->OperatorType();
58             const ir::Expression *unaryArg = unaryExpr->Argument();
59             return (op == lexer::TokenType::PUNCTUATOR_MINUS && unaryArg->IsLiteral() &&
60                     (unaryArg->AsLiteral()->IsNumberLiteral() || unaryArg->AsLiteral()->IsBigIntLiteral())) ||
61                    (op == lexer::TokenType::PUNCTUATOR_PLUS && unaryArg->IsLiteral() &&
62                     unaryArg->AsLiteral()->IsNumberLiteral());
63         }
64         case ir::AstNodeType::MEMBER_EXPRESSION: {
65             const ir::MemberExpression *memberExpr = arg->AsMemberExpression();
66             if (memberExpr->Object()->IsIdentifier()) {
67                 binder::ScopeFindResult result = checker->Scope()->Find(memberExpr->Object()->AsIdentifier()->Name());
68                 constexpr auto enumLiteralType = checker::EnumLiteralType::EnumLiteralTypeKind::LITERAL;
69                 if (result.variable && result.variable->TsType()->HasTypeFlag(checker::TypeFlag::ENUM_LITERAL) &&
70                     result.variable->TsType()->AsEnumLiteralType()->Kind() == enumLiteralType) {
71                     return true;
72                 }
73             }
74             return false;
75         }
76         default:
77             return false;
78     }
79 }
80 
Check(checker::Checker * checker) const81 checker::Type *TSAsExpression::Check(checker::Checker *checker) const
82 {
83     if (isConst_) {
84         auto context = checker::SavedCheckerContext(checker, checker::CheckerStatus::IN_CONST_CONTEXT);
85         checker::Type *exprType = expression_->Check(checker);
86 
87         if (!IsValidConstAssertionArgument(checker, expression_)) {
88             checker->ThrowTypeError(
89                 "A 'const' assertions can only be applied to references to enum members, or string, number, "
90                 "boolean, array, or object literals.",
91                 expression_->Start());
92         }
93 
94         return exprType;
95     }
96 
97     auto context = checker::SavedCheckerContext(checker, checker::CheckerStatus::NO_OPTS);
98 
99     typeAnnotation_->Check(checker);
100     checker::Type *exprType = checker->GetBaseTypeOfLiteralType(expression_->Check(checker));
101     checker::Type *targetType = typeAnnotation_->AsTypeNode()->GetType(checker);
102 
103     checker->IsTypeComparableTo(
104         targetType, exprType,
105         {"Conversion of type '", exprType, "' to type '", targetType,
106          "' may be a mistake because neither type sufficiently overlaps with the other. If this was ",
107          "intentional, convert the expression to 'unknown' first."},
108         Start());
109 
110     return targetType;
111 }
112 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)113 void TSAsExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
114 {
115     expression_ = std::get<ir::AstNode *>(cb(expression_))->AsExpression();
116     typeAnnotation_ = std::get<ir::AstNode *>(cb(typeAnnotation_))->AsExpression();
117 }
118 
119 }  // namespace panda::es2panda::ir
120