blob: 9ecc02d4d5407858ed879d1bb10653ff67fa4d8f [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// Copyright 2021 The Tint Authors.
2//
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
dan sinclaird3b13692023-07-20 01:14:15 +000015#include "src/tint/lang/wgsl/sem/info.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000016
dan sinclaird3b13692023-07-20 01:14:15 +000017#include "src/tint/lang/wgsl/sem/function.h"
18#include "src/tint/lang/wgsl/sem/module.h"
19#include "src/tint/lang/wgsl/sem/statement.h"
20#include "src/tint/lang/wgsl/sem/value_expression.h"
dan sinclair22b4dd22023-07-21 00:40:07 +000021#include "src/tint/utils/rtti/switch.h"
James Price58532052023-01-25 01:24:46 +000022
dan sinclairc990b3c2022-04-07 16:04:35 +000023namespace tint::sem {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000024
25Info::Info() = default;
26
27Info::Info(Info&&) = default;
28
29Info::~Info() = default;
30
31Info& Info::operator=(Info&&) = default;
32
Ben Claytoncd52f382023-08-07 13:11:08 +000033core::DiagnosticSeverity Info::DiagnosticSeverity(const ast::Node* ast_node,
34 core::DiagnosticRule rule) const {
James Price58532052023-01-25 01:24:46 +000035 // Get the diagnostic severity modification for a node.
36 auto check = [&](auto* node) {
37 auto& severities = node->DiagnosticSeverities();
38 auto itr = severities.find(rule);
39 if (itr != severities.end()) {
40 return itr->second;
41 }
Ben Claytoncd52f382023-08-07 13:11:08 +000042 return core::DiagnosticSeverity::kUndefined;
James Price58532052023-01-25 01:24:46 +000043 };
44
45 // Get the diagnostic severity modification for a function.
46 auto check_func = [&](const sem::Function* func) {
47 auto severity = check(func);
Ben Claytoncd52f382023-08-07 13:11:08 +000048 if (severity != core::DiagnosticSeverity::kUndefined) {
James Price58532052023-01-25 01:24:46 +000049 return severity;
50 }
51
52 // No severity set on the function, so check the module instead.
53 return check(module_);
54 };
55
56 // Get the diagnostic severity modification for a statement.
57 auto check_stmt = [&](const sem::Statement* stmt) {
58 // Walk up the statement hierarchy, checking for diagnostic severity modifications.
59 while (true) {
60 auto severity = check(stmt);
Ben Claytoncd52f382023-08-07 13:11:08 +000061 if (severity != core::DiagnosticSeverity::kUndefined) {
James Price58532052023-01-25 01:24:46 +000062 return severity;
63 }
64 if (!stmt->Parent()) {
65 break;
66 }
67 stmt = stmt->Parent();
68 }
69
70 // No severity set on the statement, so check the function instead.
71 return check_func(stmt->Function());
72 };
73
74 // Query the diagnostic severity from the semantic node that corresponds to the AST node.
75 auto* sem = Get(ast_node);
Ben Claytonf848af22023-07-28 16:37:32 +000076 TINT_ASSERT(sem != nullptr);
James Price58532052023-01-25 01:24:46 +000077 auto severity = Switch(
78 sem, //
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000079 [&](const sem::ValueExpression* expr) { return check_stmt(expr->Stmt()); },
James Price58532052023-01-25 01:24:46 +000080 [&](const sem::Statement* stmt) { return check_stmt(stmt); },
81 [&](const sem::Function* func) { return check_func(func); },
82 [&](Default) {
83 // Use the global severity set on the module.
84 return check(module_);
85 });
Ben Claytoncd52f382023-08-07 13:11:08 +000086 TINT_ASSERT(severity != core::DiagnosticSeverity::kUndefined);
James Price58532052023-01-25 01:24:46 +000087 return severity;
88}
89
dan sinclairc990b3c2022-04-07 16:04:35 +000090} // namespace tint::sem