diff options
Diffstat (limited to 'gcc/rust/util')
| -rw-r--r-- | gcc/rust/util/bi-map.h | 1 | ||||
| -rw-r--r-- | gcc/rust/util/fnv-hash.h | 1 | ||||
| -rw-r--r-- | gcc/rust/util/rust-attribute-values.h | 7 | ||||
| -rw-r--r-- | gcc/rust/util/rust-attributes.cc | 94 | ||||
| -rw-r--r-- | gcc/rust/util/rust-attributes.h | 15 | ||||
| -rw-r--r-- | gcc/rust/util/rust-canonical-path.h | 2 | ||||
| -rw-r--r-- | gcc/rust/util/rust-dump.h | 2 | ||||
| -rw-r--r-- | gcc/rust/util/rust-ggc.cc | 3 | ||||
| -rw-r--r-- | gcc/rust/util/rust-ggc.h | 4 | ||||
| -rw-r--r-- | gcc/rust/util/rust-inline-visitor.h | 1 | ||||
| -rw-r--r-- | gcc/rust/util/rust-lang-item.h | 5 | ||||
| -rw-r--r-- | gcc/rust/util/rust-token-converter.cc | 4 | ||||
| -rw-r--r-- | gcc/rust/util/rust-unwrap-segment.h | 20 | 
13 files changed, 127 insertions, 32 deletions
diff --git a/gcc/rust/util/bi-map.h b/gcc/rust/util/bi-map.h index 54870f7..4af157f 100644 --- a/gcc/rust/util/bi-map.h +++ b/gcc/rust/util/bi-map.h @@ -17,6 +17,7 @@  // <http://www.gnu.org/licenses/>.  #include "rust-system.h" +#include "optional.h"  #ifndef BIMAP_H  #define BIMAP_H diff --git a/gcc/rust/util/fnv-hash.h b/gcc/rust/util/fnv-hash.h index 6d2ec01..e51b66a 100644 --- a/gcc/rust/util/fnv-hash.h +++ b/gcc/rust/util/fnv-hash.h @@ -15,6 +15,7 @@  // 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-system.h"  #ifndef RUST_FNV_HASH_H  #define RUST_FNV_HASH_H diff --git a/gcc/rust/util/rust-attribute-values.h b/gcc/rust/util/rust-attribute-values.h index 367044a..a22664a 100644 --- a/gcc/rust/util/rust-attribute-values.h +++ b/gcc/rust/util/rust-attribute-values.h @@ -36,6 +36,7 @@ public:    static constexpr auto &DOC = "doc";    static constexpr auto &MUST_USE = "must_use";    static constexpr auto &LANG = "lang"; +  static constexpr auto &LINK_NAME = "link_name";    static constexpr auto &LINK_SECTION = "link_section";    static constexpr auto &NO_MANGLE = "no_mangle";    static constexpr auto &REPR = "repr"; @@ -48,6 +49,8 @@ public:    static constexpr auto &PROC_MACRO_DERIVE = "proc_macro_derive";    static constexpr auto &PROC_MACRO_ATTRIBUTE = "proc_macro_attribute"; +  static constexpr auto &DERIVE = "derive"; +    static constexpr auto &TARGET_FEATURE = "target_feature";    // From now on, these are reserved by the compiler and gated through    // #![feature(rustc_attrs)] @@ -60,6 +63,8 @@ public:    static constexpr auto &RUSTC_PROMOTABLE = "rustc_promotable";    static constexpr auto &RUSTC_CONST_STABLE = "rustc_const_stable";    static constexpr auto &RUSTC_CONST_UNSTABLE = "rustc_const_unstable"; +  static constexpr auto &RUSTC_ALLOW_CONST_FN_UNSTABLE +    = "rustc_allow_const_fn_unstable";    static constexpr auto &RUSTC_SPECIALIZATION_TRAIT      = "rustc_specialization_trait"; @@ -88,8 +93,6 @@ public:    static constexpr auto &TEST = "test"; -  static constexpr auto &SIMD_TEST = "simd_test"; -    static constexpr auto &RUSTC_ARGS_REQUIRED_CONST      = "rustc_args_required_const";  }; diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index c846c2d..9621100 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -38,6 +38,31 @@ Attributes::is_known (const std::string &attribute_path)    return !lookup.is_error ();  } +tl::optional<std::string> +Attributes::extract_string_literal (const AST::Attribute &attr) +{ +  if (!attr.has_attr_input ()) +    return tl::nullopt; + +  auto &attr_input = attr.get_attr_input (); + +  if (attr_input.get_attr_input_type () +      != AST::AttrInput::AttrInputType::LITERAL) +    return tl::nullopt; + +  auto &literal_expr +    = static_cast<AST::AttrInputLiteral &> (attr_input).get_literal (); + +  auto lit_type = literal_expr.get_lit_type (); + +  // TODO: bring escape sequence handling out of lexing? +  if (lit_type != AST::Literal::LitType::STRING +      && lit_type != AST::Literal::LitType::RAW_STRING) +    return tl::nullopt; + +  return literal_expr.as_string (); +} +  using Attrs = Values::Attributes;  // https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248 @@ -53,6 +78,7 @@ static const BuiltinAttrDefinition __definitions[]       {Attrs::DOC, HIR_LOWERING},       {Attrs::MUST_USE, STATIC_ANALYSIS},       {Attrs::LANG, HIR_LOWERING}, +     {Attrs::LINK_NAME, CODE_GENERATION},       {Attrs::LINK_SECTION, CODE_GENERATION},       {Attrs::NO_MANGLE, CODE_GENERATION},       {Attrs::REPR, CODE_GENERATION}, @@ -64,6 +90,8 @@ static const BuiltinAttrDefinition __definitions[]       {Attrs::PROC_MACRO, EXPANSION},       {Attrs::PROC_MACRO_DERIVE, EXPANSION},       {Attrs::PROC_MACRO_ATTRIBUTE, EXPANSION}, + +     {Attrs::DERIVE, EXPANSION},       // FIXME: This is not implemented yet, see       // https://github.com/Rust-GCC/gccrs/issues/1475       {Attrs::TARGET_FEATURE, CODE_GENERATION}, @@ -78,6 +106,7 @@ static const BuiltinAttrDefinition __definitions[]       {Attrs::RUSTC_PROMOTABLE, CODE_GENERATION},       {Attrs::RUSTC_CONST_STABLE, STATIC_ANALYSIS},       {Attrs::RUSTC_CONST_UNSTABLE, STATIC_ANALYSIS}, +     {Attrs::RUSTC_ALLOW_CONST_FN_UNSTABLE, STATIC_ANALYSIS},       {Attrs::PRELUDE_IMPORT, NAME_RESOLUTION},       {Attrs::TRACK_CALLER, CODE_GENERATION},       {Attrs::RUSTC_SPECIALIZATION_TRAIT, TYPE_CHECK}, @@ -100,8 +129,7 @@ static const BuiltinAttrDefinition __definitions[]       {Attrs::NON_EXHAUSTIVE, TYPE_CHECK},       {Attrs::RUSTFMT, EXTERNAL}, -     {Attrs::TEST, CODE_GENERATION}, -     {Attrs::SIMD_TEST, CODE_GENERATION}}; +     {Attrs::TEST, CODE_GENERATION}};  BuiltinAttributeMappings *  BuiltinAttributeMappings::get () @@ -144,6 +172,7 @@ AttributeChecker::go (AST::Crate &crate)  void  AttributeChecker::visit (AST::Crate &crate)  { +  check_inner_attributes (crate.get_inner_attrs ());    check_attributes (crate.get_inner_attrs ());    for (auto &item : crate.items) @@ -214,8 +243,8 @@ check_doc_attribute (const AST::Attribute &attribute)      {        rust_error_at (  	attribute.get_locus (), -	// FIXME: Improve error message here. Rustc has a very good one -	"%<#[doc]%> cannot be an empty attribute"); +	"valid forms for the attribute are " +	"%<#[doc(hidden|inline|...)]%> and %<#[doc = \" string \"]%>");        return;      } @@ -300,6 +329,20 @@ check_proc_macro_non_root (AST::AttrVec attributes, location_t loc)  void  AttributeChecker::check_attribute (const AST::Attribute &attribute)  { +  if (!attribute.empty_input ()) +    { +      const auto &attr_input = attribute.get_attr_input (); +      auto type = attr_input.get_attr_input_type (); +      if (type == AST::AttrInput::AttrInputType::TOKEN_TREE) +	{ +	  const auto &option = static_cast<const AST::DelimTokenTree &> ( +	    attribute.get_attr_input ()); +	  std::unique_ptr<AST::AttrInputMetaItemContainer> meta_item ( +	    option.parse_to_meta_item ()); +	  AST::DefaultASTVisitor::visit (meta_item); +	} +    } +    BuiltinAttrDefinition result;    // This checker does not check non-builtin attributes @@ -314,6 +357,15 @@ AttributeChecker::check_attribute (const AST::Attribute &attribute)  }  void +AttributeChecker::check_inner_attributes (const AST::AttrVec &attributes) +{ +  for (auto &attr : attributes) +    if (attr.is_derive ()) +      rust_error_at (attr.get_locus (), +		     "derive attribute cannot be used at crate level"); +} + +void  AttributeChecker::check_attributes (const AST::AttrVec &attributes)  {    for (auto &attr : attributes) @@ -329,10 +381,6 @@ AttributeChecker::visit (AST::DelimTokenTree &)  {}  void -AttributeChecker::visit (AST::AttrInputMetaItemContainer &) -{} - -void  AttributeChecker::visit (AST::IdentifierExpr &)  {} @@ -395,8 +443,16 @@ AttributeChecker::visit (AST::MetaItemLitExpr &)  {}  void -AttributeChecker::visit (AST::MetaItemPathExpr &) -{} +AttributeChecker::visit (AST::MetaItemPathExpr &attribute) +{ +  if (!attribute.get_expr ().is_literal ()) +    { +      rust_error_at (attribute.get_expr ().get_locus (), +		     "malformed %<path%> attribute input"); +      rust_inform (attribute.get_expr ().get_locus (), +		   "must be of the form: %<#[path = \"file\"]%>"); +    } +}  void  AttributeChecker::visit (AST::BorrowExpr &) @@ -622,6 +678,7 @@ AttributeChecker::visit (AST::TypeBoundWhereClauseItem &)  void  AttributeChecker::visit (AST::Module &module)  { +  check_attributes (module.get_outer_attrs ());    check_proc_macro_non_function (module.get_outer_attrs ());    for (auto &item : module.get_items ())      { @@ -774,10 +831,6 @@ AttributeChecker::visit (AST::StaticItem &item)  }  void -AttributeChecker::visit (AST::TraitItemConst &) -{} - -void  AttributeChecker::visit (AST::TraitItemType &)  {} @@ -785,6 +838,7 @@ void  AttributeChecker::visit (AST::Trait &trait)  {    check_proc_macro_non_function (trait.get_outer_attrs ()); +  check_attributes (trait.get_outer_attrs ());  }  void @@ -841,10 +895,6 @@ AttributeChecker::visit (AST::MetaItemPath &)  {}  void -AttributeChecker::visit (AST::MetaItemSeq &) -{} - -void  AttributeChecker::visit (AST::MetaWord &)  {} @@ -920,11 +970,11 @@ AttributeChecker::visit (AST::StructPattern &)  // void AttributeChecker::visit(TupleStructItems& ){}  void -AttributeChecker::visit (AST::TupleStructItemsNoRange &) +AttributeChecker::visit (AST::TupleStructItemsNoRest &)  {}  void -AttributeChecker::visit (AST::TupleStructItemsRange &) +AttributeChecker::visit (AST::TupleStructItemsHasRest &)  {}  void @@ -934,11 +984,11 @@ AttributeChecker::visit (AST::TupleStructPattern &)  // void AttributeChecker::visit(TuplePatternItems& ){}  void -AttributeChecker::visit (AST::TuplePatternItemsMultiple &) +AttributeChecker::visit (AST::TuplePatternItemsNoRest &)  {}  void -AttributeChecker::visit (AST::TuplePatternItemsRanged &) +AttributeChecker::visit (AST::TuplePatternItemsHasRest &)  {}  void diff --git a/gcc/rust/util/rust-attributes.h b/gcc/rust/util/rust-attributes.h index db8fe23..b10a080 100644 --- a/gcc/rust/util/rust-attributes.h +++ b/gcc/rust/util/rust-attributes.h @@ -29,6 +29,8 @@ class Attributes  {  public:    static bool is_known (const std::string &attribute_path); +  static tl::optional<std::string> +  extract_string_literal (const AST::Attribute &attr);  };  enum CompilerPass @@ -104,13 +106,14 @@ private:    void check_attribute (const AST::Attribute &attribute);    /* Check the validity of all given attributes */ + +  void check_inner_attributes (const AST::AttrVec &attributes);    void check_attributes (const AST::AttrVec &attributes);    // rust-ast.h    void visit (AST::Crate &crate) override;    void visit (AST::Token &tok) override;    void visit (AST::DelimTokenTree &delim_tok_tree) override; -  void visit (AST::AttrInputMetaItemContainer &input) override;    void visit (AST::IdentifierExpr &ident_expr) override;    void visit (AST::Lifetime &lifetime) override;    void visit (AST::LifetimeParam &lifetime_param) override; @@ -203,7 +206,6 @@ private:    void visit (AST::Union &union_item) override;    void visit (AST::ConstantItem &const_item) override;    void visit (AST::StaticItem &static_item) override; -  void visit (AST::TraitItemConst &item) override;    void visit (AST::TraitItemType &item) override;    void visit (AST::Trait &trait) override;    void visit (AST::InherentImpl &impl) override; @@ -219,7 +221,6 @@ private:    void visit (AST::MacroRulesDefinition &rules_def) override;    void visit (AST::MacroInvocation ¯o_invoc) override;    void visit (AST::MetaItemPath &meta_item) override; -  void visit (AST::MetaItemSeq &meta_item) override;    void visit (AST::MetaWord &meta_item) override;    void visit (AST::MetaNameValueStr &meta_item) override;    void visit (AST::MetaListPaths &meta_item) override; @@ -242,12 +243,12 @@ private:    void visit (AST::StructPatternFieldIdent &field) override;    void visit (AST::StructPattern &pattern) override;    // void visit(TupleStructItems& tuple_items) override; -  void visit (AST::TupleStructItemsNoRange &tuple_items) override; -  void visit (AST::TupleStructItemsRange &tuple_items) override; +  void visit (AST::TupleStructItemsNoRest &tuple_items) override; +  void visit (AST::TupleStructItemsHasRest &tuple_items) override;    void visit (AST::TupleStructPattern &pattern) override;    // void visit(TuplePatternItems& tuple_items) override; -  void visit (AST::TuplePatternItemsMultiple &tuple_items) override; -  void visit (AST::TuplePatternItemsRanged &tuple_items) override; +  void visit (AST::TuplePatternItemsNoRest &tuple_items) override; +  void visit (AST::TuplePatternItemsHasRest &tuple_items) override;    void visit (AST::TuplePattern &pattern) override;    void visit (AST::GroupedPattern &pattern) override;    void visit (AST::SlicePattern &pattern) override; diff --git a/gcc/rust/util/rust-canonical-path.h b/gcc/rust/util/rust-canonical-path.h index 079ae76..4c4d9df 100644 --- a/gcc/rust/util/rust-canonical-path.h +++ b/gcc/rust/util/rust-canonical-path.h @@ -114,6 +114,8 @@ public:        return CanonicalPath (other.segs, crate_num);      std::vector<std::pair<NodeId, std::string>> copy (segs); +    copy.reserve (other.segs.size ()); +      for (auto &s : other.segs)        copy.push_back (s); diff --git a/gcc/rust/util/rust-dump.h b/gcc/rust/util/rust-dump.h index c2d9c4d..111174b 100644 --- a/gcc/rust/util/rust-dump.h +++ b/gcc/rust/util/rust-dump.h @@ -16,6 +16,8 @@  // along with GCC; see the file COPYING3.  If not see  // <http://www.gnu.org/licenses/>. +#include "rust-system.h" +  // Common definitions useful for textual dump of IRs (AST and HIR).  #ifndef RUST_DUMP_H diff --git a/gcc/rust/util/rust-ggc.cc b/gcc/rust/util/rust-ggc.cc index 0722af2..46220a2 100644 --- a/gcc/rust/util/rust-ggc.cc +++ b/gcc/rust/util/rust-ggc.cc @@ -17,6 +17,7 @@  // <http://www.gnu.org/licenses/>.  #include "rust-ggc.h" +#include "rust-ast.h"  #include "stringpool.h"  namespace Rust { @@ -29,6 +30,8 @@ Ident::Ident (const std::string &str)    : inner (get_identifier_with_length (str.c_str (), str.length ()))  {} +Ident::Ident (const Rust::Identifier &ident) : Ident (ident.as_string ()) {} +  bool  Ident::operator== (const std::string &other) const  { diff --git a/gcc/rust/util/rust-ggc.h b/gcc/rust/util/rust-ggc.h index da28ede..da4ede1 100644 --- a/gcc/rust/util/rust-ggc.h +++ b/gcc/rust/util/rust-ggc.h @@ -24,6 +24,9 @@  namespace Rust { +// forward declare +class Identifier; +  namespace GGC {  class Ident @@ -33,6 +36,7 @@ class Ident  public:    Ident (const char *str);    Ident (const std::string &str); +  Ident (const Rust::Identifier &ident);    bool operator== (const Ident &other) const { return inner == other.inner; }    bool operator== (const std::string &other) const; diff --git a/gcc/rust/util/rust-inline-visitor.h b/gcc/rust/util/rust-inline-visitor.h index 2a35aa6..a00cd2c 100644 --- a/gcc/rust/util/rust-inline-visitor.h +++ b/gcc/rust/util/rust-inline-visitor.h @@ -15,6 +15,7 @@  // 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-system.h"  // An improved implementation of the inline visitor.  // Original idea from https://members.accu.org/index.php/articles/2021 diff --git a/gcc/rust/util/rust-lang-item.h b/gcc/rust/util/rust-lang-item.h index 67a5d9c..7b9f498 100644 --- a/gcc/rust/util/rust-lang-item.h +++ b/gcc/rust/util/rust-lang-item.h @@ -21,6 +21,9 @@  #include "optional.h"  #include "bi-map.h" +#ifndef RUST_LANG_ITEM_H +#define RUST_LANG_ITEM_H +  namespace Rust {  class LangItem @@ -185,3 +188,5 @@ template <> struct hash<Rust::LangItem::Kind>    }  };  } // namespace std + +#endif // RUST_LANG_ITEM_H diff --git a/gcc/rust/util/rust-token-converter.cc b/gcc/rust/util/rust-token-converter.cc index 52172f4..0865bf9 100644 --- a/gcc/rust/util/rust-token-converter.cc +++ b/gcc/rust/util/rust-token-converter.cc @@ -102,7 +102,9 @@ ProcMacro::TokenStream  convert (const std::vector<const_TokenPtr> &tokens)  {    std::vector<ProcMacro::TokenStream> trees; -  trees.push_back (ProcMacro::TokenStream::make_tokenstream ()); +  trees.reserve (tokens.size ()); + +  trees.emplace_back (ProcMacro::TokenStream::make_tokenstream ());    for (auto &token : tokens)      {        auto loc = convert (token->get_locus ()); diff --git a/gcc/rust/util/rust-unwrap-segment.h b/gcc/rust/util/rust-unwrap-segment.h index af3a237..ccc1e61 100644 --- a/gcc/rust/util/rust-unwrap-segment.h +++ b/gcc/rust/util/rust-unwrap-segment.h @@ -16,6 +16,10 @@  // along with GCC; see the file COPYING3.  If not see  // <http://www.gnu.org/licenses/>. +#include "rust-system.h" +#include "optional.h" +#include "rust-lang-item.h" +#include "rust-mapping-common.h"  #include <ast/rust-ast-full-decls.h>  namespace Rust { @@ -115,4 +119,20 @@ unwrap_segment_get_lang_item (const std::unique_ptr<T> &ptr)    return unwrap_segment_get_lang_item (*ptr);  } +/** + * Used to output a path in error messages + */ + +inline static std::string +unwrap_segment_error_string (const AST::TypePath &path) +{ +  return path.make_debug_string (); +} + +inline static std::string +unwrap_segment_error_string (const AST::PathInExpression &path) +{ +  return path.as_simple_path ().as_string (); +} +  } // namespace Rust  | 
