aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/ast/rust-collect-lang-items.cc
blob: 11c3297d2a9d3ced8390e3b53a28da2cbc2449bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (C) 2024 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include "rust-collect-lang-items.h"
#include "optional.h"
#include "rust-ast-collector.h"
#include "rust-ast.h"
#include "rust-attribute-values.h"
#include "rust-attributes.h"
#include "rust-hir-map.h"
#include "rust-item.h"

namespace Rust {
namespace AST {

template <typename T>
tl::optional<LangItem::Kind>
get_lang_item_attr (const T &maybe_lang_item)
{
  for (const auto &attr : maybe_lang_item.get_outer_attrs ())
    {
      const auto &str_path = attr.get_path ().as_string ();
      if (!Analysis::Attributes::is_known (str_path))
	{
	  rust_error_at (attr.get_locus (), "unknown attribute %qs",
			 str_path.c_str ());
	  continue;
	}

      bool is_lang_item = str_path == Values::Attributes::LANG
			  && attr.has_attr_input ()
			  && attr.get_attr_input ().get_attr_input_type ()
			       == AST::AttrInput::AttrInputType::LITERAL;

      if (is_lang_item)
	{
	  auto &literal
	    = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
	  const auto &lang_item_type_str = literal.get_literal ().as_string ();

	  return LangItem::Parse (lang_item_type_str);
	}
    }

  return tl::nullopt;
}

template <typename T>
void
CollectLangItems::maybe_add_lang_item (const T &item)
{
  if (auto lang_item = get_lang_item_attr (item))
    mappings.insert_lang_item_node (lang_item.value (), item.get_node_id ());
}

void
CollectLangItems::visit (AST::Trait &item)
{
  maybe_add_lang_item (item);

  DefaultASTVisitor::visit (item);
}

void
CollectLangItems::visit (AST::TraitItemType &item)
{
  maybe_add_lang_item (item);

  DefaultASTVisitor::visit (item);
}

void
CollectLangItems::visit (AST::Function &item)
{
  maybe_add_lang_item (item);

  DefaultASTVisitor::visit (item);
}

void
CollectLangItems::visit (AST::StructStruct &item)
{
  maybe_add_lang_item (item);

  DefaultASTVisitor::visit (item);
}

} // namespace AST
} // namespace Rust