#include "rust-parse.h"
#include "rust-operators.h"
#include "rust-dir-owner.h"
+#include "rust-attribute-values.h"
/* Compilation unit used for various AST-related functions that would make
* the headers too long if they were defined inline and don't receive any
/* assume that cfg predicate actually can exist, i.e. attribute has cfg or
* cfg_attr path */
if (!has_attr_input ()
- || (path.as_string () != "cfg" && path.as_string () != "cfg_attr"))
+ || (path.as_string () != Values::Attributes::CFG
+ && path.as_string () != Values::Attributes::CFG_ATTR))
{
// DEBUG message
rust_debug (
std::vector<Attribute>
Attribute::separate_cfg_attrs () const
{
- if (!has_attr_input () || path.as_string () != "cfg_attr")
+ if (!has_attr_input () || path.as_string () != Values::Attributes::CFG_ATTR)
return {};
// assume that it has already been parsed
#include "rust-hir-path-probe.h"
#include "rust-type-util.h"
#include "rust-compile-implitem.h"
+#include "rust-attribute-values.h"
#include "fold-const.h"
#include "stringpool.h"
bool inline should_mangle_item (const tree fndecl)
{
- return lookup_attribute ("no_mangle", DECL_ATTRIBUTES (fndecl)) == NULL_TREE;
+ return lookup_attribute (Values::Attributes::NO_MANGLE,
+ DECL_ATTRIBUTES (fndecl))
+ == NULL_TREE;
}
void
// is it inline?
for (const auto &attr : attrs)
{
- bool is_inline = attr.get_path ().as_string ().compare ("inline") == 0;
+ bool is_inline
+ = attr.get_path ().as_string () == Values::Attributes::INLINE;
bool is_must_use
- = attr.get_path ().as_string ().compare ("must_use") == 0;
- bool is_cold = attr.get_path ().as_string ().compare ("cold") == 0;
+ = attr.get_path ().as_string () == Values::Attributes::MUST_USE;
+ bool is_cold = attr.get_path ().as_string () == Values::Attributes::COLD;
bool is_link_section
- = attr.get_path ().as_string ().compare ("link_section") == 0;
- bool no_mangle = attr.get_path ().as_string ().compare ("no_mangle") == 0;
+ = attr.get_path ().as_string () == Values::Attributes::LINK_SECTION;
+ bool no_mangle
+ = attr.get_path ().as_string () == Values::Attributes::NO_MANGLE;
bool is_deprecated
- = attr.get_path ().as_string ().compare ("deprecated") == 0;
+ = attr.get_path ().as_string () == Values::Attributes::DEPRECATED;
if (is_inline)
{
// simple #[cold]
if (!attr.has_attr_input ())
{
- tree cold = get_identifier ("cold");
+ tree cold = get_identifier (Values::Attributes::COLD);
// this will get handled by the GCC backend later
DECL_ATTRIBUTES (fndecl)
= tree_cons (cold, NULL_TREE, DECL_ATTRIBUTES (fndecl));
return;
}
- DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("no_mangle"), NULL_TREE,
- DECL_ATTRIBUTES (fndecl));
+ DECL_ATTRIBUTES (fndecl)
+ = tree_cons (get_identifier (Values::Attributes::NO_MANGLE), NULL_TREE,
+ DECL_ATTRIBUTES (fndecl));
}
void
{
tree attr_list = build_tree_list (NULL_TREE, value);
DECL_ATTRIBUTES (fndecl)
- = tree_cons (get_identifier ("deprecated"), attr_list,
+ = tree_cons (get_identifier (Values::Attributes::DEPRECATED), attr_list,
DECL_ATTRIBUTES (fndecl));
}
}
#include "rust-hir-expr.h"
#include "rust-hir-stmt.h"
#include "rust-hir-item.h"
+#include "rust-attribute-values.h"
namespace Rust {
namespace Privacy {
if (segments.size () != 1)
continue;
auto name = segments.at (0).get_segment_name ();
- if (name == "proc_macro" || name == "proc_macro_attribute"
- || name == "proc_macro_derive")
+ if (name == Values::Attributes::PROC_MACRO
+ || name == Values::Attributes::PROC_MACRO_ATTRIBUTE
+ || name == Values::Attributes::PROC_MACRO_DERIVE)
return name;
}
#include "rust-hir-expr.h"
#include "rust-hir-stmt.h"
#include "rust-hir-item.h"
+#include "rust-attribute-values.h"
namespace Rust {
namespace HIR {
if (std::any_of (fn->get_outer_attrs ().begin (),
fn->get_outer_attrs ().end (),
[] (const AST::Attribute &attr) {
- return attr.get_path ().as_string () == "target_feature";
+ return attr.get_path ().as_string ()
+ == Values::Attributes::TARGET_FEATURE;
}))
rust_error_at (locus,
"call to function with %<#[target_feature]%> requires "
#include "rust-cfg-strip.h"
#include "rust-ast-full.h"
#include "rust-session-manager.h"
+#include "rust-attribute-values.h"
namespace Rust {
for (const auto &attr : attrs)
{
- if (attr.get_path () == "cfg" && !attr.check_cfg_predicate (session))
+ if (attr.get_path () == Values::Attributes::CFG
+ && !attr.check_cfg_predicate (session))
return true;
}
return false;
// TODO: maybe have something that strips cfg attributes that evaluate true?
for (auto &attr : attrs)
{
- if (attr.get_path () == "cfg")
+ if (attr.get_path () == Values::Attributes::CFG)
{
if (!attr.is_parsed_to_meta_item ())
attr.parse_attr_to_meta_item ();
for (std::size_t i = 0; i < attrs.size (); i++)
{
auto &attr = attrs[i];
- if (attr.get_path () == "cfg_attr")
+ if (attr.get_path () == Values::Attributes::CFG_ATTR)
{
if (!attr.is_parsed_to_meta_item ())
attr.parse_attr_to_meta_item ();
#include "rust-macro.h"
#include "rust-parse.h"
#include "rust-session-manager.h"
+#include "rust-attribute-values.h"
namespace Rust {
#include "rust-ast-lower-type.h"
#include "rust-ast-lower-pattern.h"
#include "rust-ast-lower-extern.h"
+#include "rust-attribute-values.h"
namespace Rust {
namespace HIR {
continue;
}
- bool is_lang_item = str_path.compare ("lang") == 0
+ 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;
- bool is_doc_item = str_path.compare ("doc") == 0;
+ bool is_doc_item = str_path == Values::Attributes::DOC;
if (is_doc_item)
handle_doc_item_attribute (item, attr);
{
auto is_export = false;
for (const auto &attr : def.get_outer_attrs ())
- if (attr.get_path ().as_string () == "macro_export")
+ if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
is_export = true;
if (is_export)
#include "rust-hir-type.h"
#include "rust-hir.h"
#include <string>
+#include "rust-attribute-values.h"
namespace Rust {
namespace HIR {
Dump::visit (AST::Attribute &attribute)
{
// Special, no begin/end as this is called by do_inner_attrs.
- put_field ("path", attribute.get_path ().as_string ());
+ put_field (Values::Attributes::PATH, attribute.get_path ().as_string ());
std::string str = "none";
if (attribute.has_attr_input ())
#include "rust-diagnostics.h"
#include "rust-make-unique.h"
#include "rust-dir-owner.h"
+#include "rust-attribute-values.h"
namespace Rust {
// Left binding powers of operations.
{
const_TokenPtr token = lexer.peek_token ();
location_t locus = token->get_locus ();
- AST::SimplePathSegment segment ("doc", locus);
+ AST::SimplePathSegment segment (Values::Attributes::DOC, locus);
std::vector<AST::SimplePathSegment> segments;
segments.push_back (std::move (segment));
AST::SimplePath attr_path (std::move (segments), false, locus);
#include "rust-linemap.h"
#include "rust-diagnostics.h"
#include "rust-token.h"
+#include "rust-attribute-values.h"
namespace Rust {
AST::Attribute path_attr = AST::Attribute::create_empty ();
for (const auto &attr : inner_attrs)
{
- if (attr.get_path ().as_string () == "path")
+ if (attr.get_path ().as_string () == Values::Attributes::PATH)
{
path_attr = attr;
break;
for (const auto &attr : outer_attrs)
{
- if (attr.get_path ().as_string () == "path")
+ if (attr.get_path ().as_string () == Values::Attributes::PATH)
{
path_attr = attr;
break;
#include "rust-ast-full.h"
#include "rust-name-resolver.h"
#include "rust-macro-builtins.h"
+#include "rust-attribute-values.h"
namespace Rust {
namespace Resolver {
is_macro_use_module (const AST::Module &mod)
{
for (const auto &attr : mod.get_outer_attrs ())
- if (attr.get_path ().as_string () == "macro_use")
+ if (attr.get_path ().as_string () == Values::Attributes::MACRO_USE)
return true;
return false;
bool is_builtin
= std::any_of (outer_attrs.begin (), outer_attrs.end (),
[] (AST::Attribute attr) {
- return attr.get_path () == "rustc_builtin_macro";
+ return attr.get_path ()
+ == Values::Attributes::RUSTC_BUILTIN_MACRO;
});
if (is_builtin)
#include "rust-toplevel-name-resolver-2.0.h"
#include "rust-ast-full.h"
#include "rust-hir-map.h"
+#include "rust-attribute-values.h"
namespace Rust {
namespace Resolver2_0 {
is_macro_export (AST::MacroRulesDefinition &def)
{
for (const auto &attr : def.get_outer_attrs ())
- if (attr.get_path ().as_string () == "macro_export")
+ if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
return true;
return false;
#include "rust-cfg-strip.h"
#include "rust-expand-visitor.h"
#include "rust-unicode.h"
+#include "rust-attribute-values.h"
#include "diagnostic.h"
#include "input.h"
{
// create "macro use" attribute for use on extern crate item to enable
// loading macros from it
- AST::Attribute attr (AST::SimplePath::from_str ("macro_use",
- UNDEF_LOCATION),
+ AST::Attribute attr (AST::SimplePath::from_str (
+ Values::Attributes::MACRO_USE, UNDEF_LOCATION),
nullptr);
// create "extern crate" item with the name
#include "rust-hir-type-check-type.h"
#include "rust-hir-trait-resolve.h"
#include "rust-type-util.h"
+#include "rust-attribute-values.h"
namespace Rust {
namespace Resolver {
for (const auto &attr : attrs)
{
- bool is_repr = attr.get_path ().as_string ().compare ("repr") == 0;
+ bool is_repr = attr.get_path ().as_string () == Values::Attributes::REPR;
if (is_repr)
{
const AST::AttrInput &input = attr.get_attr_input ();
--- /dev/null
+#ifndef RUST_ATTRIBUTES_VALUE_H
+#define RUST_ATTRIBUTES_VALUE_H
+
+namespace Rust {
+namespace Values {
+// TODO: Change this to a namespace + inline constexpr in the future
+class Attributes
+{
+public:
+ static constexpr auto &INLINE = "inline";
+ static constexpr auto &COLD = "cold";
+ static constexpr auto &CFG = "cfg";
+ static constexpr auto &CFG_ATTR = "cfg_attr";
+ static constexpr auto &DEPRECATED = "deprecated";
+ static constexpr auto &ALLOW = "allow";
+ static constexpr auto &ALLOW_INTERNAL_UNSTABLE = "allow_internal_unstable";
+ static constexpr auto &DOC = "doc";
+ static constexpr auto &MUST_USE = "must_use";
+ static constexpr auto &LANG = "lang";
+ static constexpr auto &LINK_SECTION = "link_section";
+ static constexpr auto &NO_MANGLE = "no_mangle";
+ static constexpr auto &REPR = "repr";
+ static constexpr auto &RUSTC_BUILTIN_MACRO = "rustc_builtin_macro";
+ static constexpr auto &PATH = "path";
+ static constexpr auto &MACRO_USE = "macro_use";
+ static constexpr auto &MACRO_EXPORT = "macro_export";
+ static constexpr auto &PROC_MACRO = "proc_macro";
+ static constexpr auto &PROC_MACRO_DERIVE = "proc_macro_derive";
+ static constexpr auto &PROC_MACRO_ATTRIBUTE = "proc_macro_attribute";
+ static constexpr auto &TARGET_FEATURE = "target_feature";
+ // From now on, these are reserved by the compiler and gated through
+ // #![feature(rustc_attrs)]
+ static constexpr auto &RUSTC_INHERIT_OVERFLOW_CHECKS
+ = "rustc_inherit_overflow_checks";
+ static constexpr auto &STABLE = "stable";
+};
+} // namespace Values
+} // namespace Rust
+
+#endif /* !RUST_ATTRIBUTES_VALUE_H */
#include "rust-ast-full.h"
#include "rust-diagnostics.h"
#include "rust-unicode.h"
+#include "rust-attribute-values.h"
namespace Rust {
namespace Analysis {
+using Attrs = Values::Attributes;
+
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248
static const BuiltinAttrDefinition __definitions[]
- = {{"inline", CODE_GENERATION},
- {"cold", CODE_GENERATION},
- {"cfg", EXPANSION},
- {"cfg_attr", EXPANSION},
- {"deprecated", STATIC_ANALYSIS},
- {"allow", STATIC_ANALYSIS},
- {"allow_internal_unstable", STATIC_ANALYSIS},
- {"doc", HIR_LOWERING},
- {"must_use", STATIC_ANALYSIS},
- {"lang", HIR_LOWERING},
- {"link_section", CODE_GENERATION},
- {"no_mangle", CODE_GENERATION},
- {"repr", CODE_GENERATION},
- {"rustc_builtin_macro", EXPANSION},
- {"path", EXPANSION},
- {"macro_use", NAME_RESOLUTION},
- {"macro_export", NAME_RESOLUTION},
- {"proc_macro", EXPANSION},
- {"proc_macro_derive", EXPANSION},
- {"proc_macro_attribute", EXPANSION},
+ = {{Attrs::INLINE, CODE_GENERATION},
+ {Attrs::COLD, CODE_GENERATION},
+ {Attrs::CFG, EXPANSION},
+ {Attrs::CFG_ATTR, EXPANSION},
+ {Attrs::DEPRECATED, STATIC_ANALYSIS},
+ {Attrs::ALLOW, STATIC_ANALYSIS},
+ {Attrs::ALLOW_INTERNAL_UNSTABLE, STATIC_ANALYSIS},
+ {Attrs::DOC, HIR_LOWERING},
+ {Attrs::MUST_USE, STATIC_ANALYSIS},
+ {Attrs::LANG, HIR_LOWERING},
+ {Attrs::LINK_SECTION, CODE_GENERATION},
+ {Attrs::NO_MANGLE, CODE_GENERATION},
+ {Attrs::REPR, CODE_GENERATION},
+ {Attrs::RUSTC_BUILTIN_MACRO, EXPANSION},
+ {Attrs::PATH, EXPANSION},
+ {Attrs::MACRO_USE, NAME_RESOLUTION},
+ {Attrs::MACRO_EXPORT, NAME_RESOLUTION},
+ {Attrs::PROC_MACRO, EXPANSION},
+ {Attrs::PROC_MACRO_DERIVE, EXPANSION},
+ {Attrs::PROC_MACRO_ATTRIBUTE, EXPANSION},
// FIXME: This is not implemented yet, see
// https://github.com/Rust-GCC/gccrs/issues/1475
- {"target_feature", CODE_GENERATION},
+ {Attrs::TARGET_FEATURE, CODE_GENERATION},
// From now on, these are reserved by the compiler and gated through
// #![feature(rustc_attrs)]
- {"rustc_inherit_overflow_checks", CODE_GENERATION},
- {"stable", STATIC_ANALYSIS}};
+ {Attrs::RUSTC_INHERIT_OVERFLOW_CHECKS, CODE_GENERATION},
+ {Attrs::STABLE, STATIC_ANALYSIS}};
BuiltinAttributeMappings *
BuiltinAttributeMappings::get ()
return false;
auto name = result.name;
- return name == "proc_macro" || name == "proc_macro_derive"
- || name == "proc_macro_attribute";
+ return name == Attrs::PROC_MACRO || name == Attrs::PROC_MACRO_DERIVE
+ || name == Attrs::PROC_MACRO_ATTRIBUTE;
}
// Emit an error when one encountered attribute is either #[proc_macro],
// TODO: Add checks here for each builtin attribute
// TODO: Have an enum of builtins as well, switching on strings is annoying
// and costly
- if (result.name == "doc")
+ if (result.name == Attrs::DOC)
check_doc_attribute (attribute);
}
auto name = result.name.c_str ();
- if (result.name == "proc_macro_derive")
+ if (result.name == Attrs::PROC_MACRO_DERIVE)
{
if (!attribute.has_attr_input ())
{
}
check_crate_type (name, attribute);
}
- else if (result.name == "proc_macro"
- || result.name == "proc_macro_attribute")
+ else if (result.name == Attrs::PROC_MACRO
+ || result.name == Attrs::PROC_MACRO_ATTRIBUTE)
{
check_crate_type (name, attribute);
}
#include "rust-hir-full.h"
#include "rust-macro-builtins.h"
#include "rust-mapping-common.h"
+#include "rust-attribute-values.h"
namespace Rust {
namespace Analysis {
bool should_be_builtin
= std::any_of (outer_attrs.begin (), outer_attrs.end (),
[] (AST::Attribute attr) {
- return attr.get_path () == "rustc_builtin_macro";
+ return attr.get_path ()
+ == Values::Attributes::RUSTC_BUILTIN_MACRO;
});
if (should_be_builtin)
{