Skip to content

Commit 865aa0a

Browse files
committed
Add new Builtin attributes mappings
This class keeps track of all known builtin attributes and specifies which pass they are handled at. This replaces the checks we added for outer attributes during hir lowering making it a more maintainable data structure.
1 parent 9bdc546 commit 865aa0a

File tree

5 files changed

+152
-30
lines changed

5 files changed

+152
-30
lines changed

gcc/rust/Make-lang.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ GRS_OBJS = \
7777
rust/rust-macro-invoc-lexer.o \
7878
rust/rust-hir-full-test.o \
7979
rust/rust-hir-map.o \
80+
rust/rust-attributes.o \
8081
rust/rust-abi.o \
8182
rust/rust-ast-lower.o \
8283
rust/rust-ast-lower-pattern.o \

gcc/rust/hir/rust-ast-lower-base.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "rust-ast-visitor.h"
2525
#include "rust-hir-map.h"
2626
#include "rust-hir-full.h"
27+
#include "rust-attributes.h"
2728

2829
namespace Rust {
2930
namespace HIR {
@@ -230,9 +231,13 @@ class ASTLoweringBase : public AST::ASTVisitor
230231
virtual void visit (AST::BareFunctionType &type) {}
231232

232233
protected:
233-
ASTLoweringBase () : mappings (Analysis::Mappings::get ()) {}
234+
ASTLoweringBase ()
235+
: mappings (Analysis::Mappings::get ()),
236+
attr_mappings (Analysis::BuiltinAttributeMappings::get ())
237+
{}
234238

235239
Analysis::Mappings *mappings;
240+
Analysis::BuiltinAttributeMappings *attr_mappings;
236241

237242
HIR::Lifetime lower_lifetime (AST::Lifetime &lifetime)
238243
{
@@ -287,10 +292,10 @@ class ASTLoweringBase : public AST::ASTVisitor
287292
void handle_lang_item_attribute (const HIR::Item &item,
288293
const AST::Attribute &attr);
289294

290-
static bool is_known_attribute (const std::string &attribute_path);
295+
bool is_known_attribute (const std::string &attribute_path) const;
291296

292-
static bool
293-
attribute_handled_in_another_pass (const std::string &attribute_path);
297+
bool
298+
attribute_handled_in_another_pass (const std::string &attribute_path) const;
294299
};
295300

296301
} // namespace HIR

gcc/rust/hir/rust-ast-lower.cc

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -653,39 +653,24 @@ ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item,
653653
}
654654

655655
bool
656-
ASTLoweringBase::is_known_attribute (const std::string &attribute_path)
656+
ASTLoweringBase::is_known_attribute (const std::string &attribute_path) const
657657
{
658-
if (attribute_path.compare ("inline") == 0)
659-
return true;
660-
else if (attribute_path.compare ("cfg") == 0)
661-
return true;
662-
else if (attribute_path.compare ("cfg_attr") == 0)
663-
return true;
664-
else if (attribute_path.compare ("allow") == 0)
665-
return true;
666-
else if (attribute_path.compare ("lang") == 0)
667-
return true;
668-
669-
return false;
658+
const auto &lookup = attr_mappings->lookup_builtin (attribute_path);
659+
return !lookup.is_error ();
670660
}
671661

672662
bool
673663
ASTLoweringBase::attribute_handled_in_another_pass (
674-
const std::string &attribute_path)
664+
const std::string &attribute_path) const
675665
{
676-
// handled during code-generation
677-
if (attribute_path.compare ("inline") == 0)
678-
return true;
679-
680-
// handled during previous expansion pass
681-
else if (attribute_path.compare ("cfg") == 0)
682-
return true;
683-
else if (attribute_path.compare ("cfg_attr") == 0)
684-
return true;
685-
else if (attribute_path.compare ("allow") == 0)
686-
return true;
666+
const auto &lookup = attr_mappings->lookup_builtin (attribute_path);
667+
if (lookup.is_error ())
668+
return false;
687669

688-
return false;
670+
if (lookup.handler == Analysis::CompilerPass::UNKNOWN)
671+
return false;
672+
673+
return lookup.handler != Analysis::CompilerPass::HIR_LOWERING;
689674
}
690675

691676
} // namespace HIR

gcc/rust/util/rust-attributes.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (C) 2020-2022 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#include "rust-attributes.h"
20+
21+
namespace Rust {
22+
namespace Analysis {
23+
24+
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#256
25+
static const BuiltinAttrDefinition __definitions[] = {
26+
{"inline", CODE_GENERATION}, {"cfg", EXPANSION}, {"cfg_attr", EXPANSION},
27+
{"allow", STATIC_ANALYSIS}, {"lang", HIR_LOWERING},
28+
};
29+
30+
BuiltinAttributeMappings *
31+
BuiltinAttributeMappings::get ()
32+
{
33+
static BuiltinAttributeMappings *instance = nullptr;
34+
if (instance == nullptr)
35+
instance = new BuiltinAttributeMappings ();
36+
37+
return instance;
38+
}
39+
40+
const BuiltinAttrDefinition &
41+
BuiltinAttributeMappings::lookup_builtin (const std::string &attr_name) const
42+
{
43+
auto it = mappings.find (attr_name);
44+
if (it == mappings.end ())
45+
return BuiltinAttrDefinition::error_node ();
46+
47+
return it->second;
48+
}
49+
50+
BuiltinAttributeMappings::BuiltinAttributeMappings ()
51+
{
52+
size_t ndefinitions = sizeof (__definitions) / sizeof (BuiltinAttrDefinition);
53+
for (size_t i = 0; i < ndefinitions; i++)
54+
{
55+
const BuiltinAttrDefinition &def = __definitions[i];
56+
mappings.insert ({def.name, def});
57+
}
58+
}
59+
60+
} // namespace Analysis
61+
} // namespace Rust

gcc/rust/util/rust-attributes.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (C) 2020-2022 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#include "rust-system.h"
20+
21+
namespace Rust {
22+
namespace Analysis {
23+
24+
enum CompilerPass
25+
{
26+
UNKNOWN,
27+
28+
EXPANSION,
29+
NAME_RESOLUTION,
30+
HIR_LOWERING,
31+
TYPE_CHECK,
32+
STATIC_ANALYSIS,
33+
CODE_GENERATION
34+
};
35+
36+
struct BuiltinAttrDefinition
37+
{
38+
std::string name;
39+
CompilerPass handler;
40+
41+
static BuiltinAttrDefinition get_error ()
42+
{
43+
return BuiltinAttrDefinition{"", UNKNOWN};
44+
}
45+
46+
static BuiltinAttrDefinition &error_node ()
47+
{
48+
static BuiltinAttrDefinition error_node = get_error ();
49+
return error_node;
50+
}
51+
52+
bool is_error () const { return name.empty (); }
53+
};
54+
55+
class BuiltinAttributeMappings
56+
{
57+
public:
58+
static BuiltinAttributeMappings *get ();
59+
60+
const BuiltinAttrDefinition &
61+
lookup_builtin (const std::string &attr_name) const;
62+
63+
private:
64+
BuiltinAttributeMappings ();
65+
66+
std::map<std::string, const BuiltinAttrDefinition> mappings;
67+
};
68+
69+
} // namespace Analysis
70+
} // namespace Rust

0 commit comments

Comments
 (0)